I have a pyplot graph where each of the data points is labeled with an arrow pointing at the data point, and I have it formatted so that the data is at a constant offset from the data point it is annotating. This works fine until I get too close to the edge of figure with one of my data points and the annotation is chopped off. I am hoping that there is a way to format my annotation so that it automatically positions its self off of my data and yet stays in my figure. Below is a snippet of how I am formatting my annotation.
for label, x, y in zip(bagOlabels, time, height):
ax.annotate(
label,
xy = (x,y), xytext = (50,-20),
textcoords = 'offset points', ha = 'right', va = 'top',
arrowprops = dict(arrowstyle = '->',
connectionstyle = 'arc3,rad=0')
)
The xytext = (50,-20) is where I set the label offset. I have been doing digging, but I haven’t found what I am looking for yet. If you have any insights into how this could be accomplished I would love to hear it.
Thanks.
There are some strategies that you can try:
If the point is in the right half of the plot, put the annotation with negative offset:
Enlarge the plot so all the annotations fit in the plot.
It is possible that in the first case, the annotations overlap each other.
UPDATE:
The method
get_xlimreturns the limits of your plot in the x-axis: