Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib: Annotate plot with vertical arrow and centered text

I would like to annotate a plot with a vertical arrow, and then center the annotation on the tip of the vertical arrow: enter image description here

Here is the code that I use:

import matplotlib.pyplot as plt
plt.annotate(
# Label and coordinate
'Help here!', xy=(8, 0.4),xytext=(7.05, 0.6) ,
# Custom arrow
arrowprops=dict(arrowstyle='->',lw=1)
)

The xytext coordinates indicate the coordinates at which the text starts. If they are not correctly set, the arros stops being vertical here. For now in order to make the arrow vertical, I need to manually find the correct x coordinate for the variable xytext (for this text it appears to be 7.05). But can I just find a way to indicate what I want as a command?

like image 947
Fringant Avatar asked Sep 02 '25 05:09

Fringant


1 Answers

ImportanceOfBeingErnest gave me the response in comment. Here is the code associated to his response:

import matplotlib.pyplot as plt
plt.annotate(
# Label and coordinate
'Help here!', xy=(8, 0.4),xytext=(8, 0.6) ,
horizontalalignment="center",
# Custom arrow
arrowprops=dict(arrowstyle='->',lw=1)
)
like image 108
Fringant Avatar answered Sep 04 '25 17:09

Fringant