Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib text only in plot area

I use the matplotlib library for plotting data in python. In my figure I also have some text to distinguish the data. The problem is that the text goes over the border in the figure window. Is it possible to make the border of the plot cut off the text at the corresponding position and only when I pan inside the plot the the rest of the text gets visible (but only when inside plot area). I use the text() function to display the text

[EDIT:]

The code looks like this:

fig = plt.figure()
ax = fig.add_subplot(111)
# ...
txt = ax.text(x, y, n, fontsize=10)
txt.set_clip_on(False) # I added this due to the answer from tcaswell
like image 313
Miguellissimo Avatar asked Feb 17 '14 15:02

Miguellissimo


People also ask

How do I show text in Matplotlib?

Basic text commandsAdd an annotation, with an optional arrow, at an arbitrary location of the Axes . Add a label to the Axes 's x-axis. Add a label to the Axes 's y-axis. Add a title to the Axes .

What is BBOX in Matplotlib?

BboxTransformTo is a transformation that linearly transforms points from the unit bounding box to a given Bbox. In your case, the transform itself is based upon a TransformedBBox which again has a Bbox upon which it is based and a transform - for this nested instance an Affine2D transform.


1 Answers

I think that your text goes over the border because you didn't set the limits of your plot. Why don't you try this?

 fig=figure()
 ax=fig.add_subplot(1,1,1)
 text(0.1, 0.85,'dummy text',horizontalalignment='left',verticalalignment='center',transform = ax.transAxes)

This way your text will always be inside the plot and its left corner will be at point (0.1,0.85) in units of your plot.

like image 70
Brian Avatar answered Sep 30 '22 10:09

Brian