Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write annotation outside the drawing in data coords

Tags:

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.

like image 658
Mephisto Avatar asked Aug 30 '13 16:08

Mephisto


People also ask

How do you annotate a PLT plot?

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.

How do I annotate text in Matplotlib?

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 .


1 Answers

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) 
like image 114
tacaswell Avatar answered Sep 27 '22 19:09

tacaswell