Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Matplotlib annotations: x coordinate constant and y dynamic

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'))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading