How to put text inside a box on a plot in matplotlib

I would like to put the text inside the box on the matplotlib chart, but the documentation only gives an example on how to put it in the upper right corner (and choosing a different angle is not quite straightforward).

+5
python matplotlib
Dec 30 '14 at 15:38
source share
1 answer

Here is the code from the example :

# these are matplotlib.patch.Patch properties props = dict(boxstyle='round', facecolor='wheat', alpha=0.5) # place a text box in upper left in axes coords ax.text(0.05, 0.95, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='top', bbox=props) 

Matplotlib coordinates

Using transform=ax.transAxes , we can place the elements inside the graph in the coordinate system at which the point (0, 0) is in the lower left corner, (0, 1) in the upper left corner (1, 1) in the upper right corner, and etc.

To be specific: if we place a text field using the position (0, 0), a certain point with the name anchor will be placed in the lower left corner. To change the binding, you need to add two arguments to the function call: verticalalignment (possible values: center , top , bottom , baseline ) and horizontalalignment (possible values: center , right , left ).

So, to place the field in the lower left corner, you need to put the lower left corner of the field in the lower left corner of the picture:

 # place a text box in lower left in axes coords ax.text(0.05, 0.05, textstr, transform=ax.transAxes, fontsize=14, verticalalignment='bottom', bbox=props) 

In any case, there is a link to ipython-notebook with an example for all placements .

+6
Dec 30 '14 at 15:38
source share



All Articles