25

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.

1
  • @nordev Not convinced that clip_on shouldn't work...the annotation code is a bit finicky (I think it got written first, then artist got re-factored, and annotation never got update)
    – tacaswell
    Aug 30, 2013 at 17:03

2 Answers 2

48
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)
2
  • 1
    Hey, thanks very much! (+1) This answer came so quick, that I cannot accept it yet (there is a red warning that says something like: you can accept the answer in 3 minutes). I love StackExchange.
    – Mephisto
    Aug 30, 2013 at 17:01
  • I see it. Thanks, very useful, even more than the first option.
    – Mephisto
    Aug 30, 2013 at 17:03
3

I just had the same problem and found another,very simple solution. Option of the annotate method:

annotation_clip=False

https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.annotate.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.