My graphic goes from y=-1
to y=10
I want to write a small piece of text in an arbitrary position, say at x=2000
, y=5
:
ax.annotate('MgII', xy=(2000.0, 5.0), xycoords='data')
Now I want the same, but this time the piece of text must be outside the graphic, but in the exact position I mark in data coordinates:
ax.annotate('MgII', xy=(2000.0, 10.5), xycoords='data')
But it then disappears (remember my graphic goes from -1 to 10). There is plenty of space free on top of the graphic.
And, if I specify
xy=(2000.0, 9.999)
then the label appears nearly where I want it, only it is too close to the top border of the picture. I want it at y=10.5
, specifically.
The annotate() function in pyplot module of matplotlib library is used to annotate the point xy with text s. Parameters: This method accept the following parameters that are described below: s: This parameter is the text of the annotation. xy: This parameter is the point (x, y) to annotate.
Annotating with Arrow. The annotate() function in the pyplot module (or annotate method of the Axes class) is used to draw an arrow connecting two points on the plot. This annotates a point at xy in the given coordinate ( xycoords ) with the text at xytext given in textcoords .
ax.annotate('MgII', xy=(2000.0, 10.5), xycoords='data', annotation_clip=False)
By default in data units the annotation is only drawn if it is in axes.
You might be better off using a blended transform:
trans = ax.get_xaxis_transform() # x in data untis, y in axes fraction ann = ax.annotate('MgII', xy=(2000, 1.05 ), xycoords=trans)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With