Home > OS >  Matplotlib annotations: x coordinate constant and y dynamic
Matplotlib annotations: x coordinate constant and y dynamic

Time:09-06

Suppose this is the code:

ax1 = plt.subplot2grid((1, 1), (0, 0))

x = range(100)
y = range(100)
ax1.annotate((y[-1]), (x[-1], y[-1]),  bbox=dict(boxstyle='larrow', fc='0.8', ec='None'))

The arrow is too close to the graph, I would like it more to the rightexample. The proposed solution is to add a number to the x value. for example like this:

ax1.annotate((y[-1]), (x[-1] 4, y[-1]), bbox=dict(boxstyle='larrow', fc='0.8', ec='None'))

The problem with that is that the graph is dynamic, and the value that needs to be added is proportional to the X value.

If there are 100 values in the X value, then you need to add 3 to move the arrow to the right, on the other hand, if there are 10,000 values in the X value, then you need to add 200, etc.

Is there a way to make the arrow stay in the same place for the x coordinate, and for the y coordinate will change according to the last value.

CodePudding user response:

Add this: textcoords=('offset points', 'data') to the annotate parameters, like this:

ax1.annotate((x[-1]), (80, y[-1]), textcoords=('offset points', 'data'),  bbox=dict(boxstyle='larrow', fc='0.8', ec='None'))

Note that the value of the X coordination is a number which results from the position in the graph and not from the data, put any value you want.

  • Related