![]() T = fig.annotate("text", xy=(x, y), xytext=(-10, 0), textcoords='offset points', horizontalalignment='left', verticalalignment='bottom', color=color) I draw the line however I want, then draw the annotation, then bind them with a helper function: line, = fig.plot(xdata, ydata, '-', color=color) I then offset the label by 10 points to the left to make it not overlap. The only way I found to have a label properly next to the line is to align by center in both vertical and horizontal. This example shows what matplotlib does: Some font basics if you're interested about text rendering: It aligns by the bounding box and not by the text baseline. This isn't perfect, because matplotlib's handling of rotated text is all wrong. transform two points along the line to display coordinates.goes from data coordinates to display coordinates) So my solution is to associate lines with annotations, then iterate through them and do this: The rotation must be set manually, but this must be done AFTER draw() or layout. I came up with something that works for me. Label_line(line, "Some Label", x, y, color=color)Įdit: note that this method still needs to be called after the figure layout is finalized, otherwise things will be altered. Line, = ot(xdata, ydata, '-', color=color) Slope_degrees = np.degrees(np.arctan2(rise, run)) Y-position to place center of text (in data coordinates) X-position to place center of text (in data coordinated """Add a label to a line, at the proper angle. This is the exact same process and basic code as given by - it's just restructured to be (hopefully) a little more convenient.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |