Automatically put text field in matplotlib

Is there a way to tell pyplot.text () as you can with pyplot.legend ()?

Something like a legend argument would be excellent:

plt.legend(loc="upper left") 

I am trying to mark subheadings with different axes using letters (for example, "A", "B"). I believe that this will be a better way than a manual assessment of the situation.

thank

+31
python matplotlib textbox
Aug 12 2018-11-11T00:
source share
2 answers

I'm not sure if this was available when I initially posted the question, but now you can use the loc parameter. The following is an example:

 import numpy as np import matplotlib.pyplot as plt from matplotlib.offsetbox import AnchoredText # make some data x = np.arange(10) y = x # set up figure and axes f, ax = plt.subplots(1,1) # loc works the same as it does with figures (though best doesn't work) # pad=5 will increase the size of padding between the border and text # borderpad=5 will increase the distance between the border and the axes # frameon=False will remove the box around the text anchored_text = AnchoredText("Test", loc=2) ax.plot(x,y) ax.add_artist(anchored_text) plt.show() 

enter image description here

+19
Oct 29 '15 at 2:59
source share

Just use annotate and specify the axis coordinates. For example, "top left" would be:

 plt.annotate('Something', xy=(0.05, 0.95), xycoords='axes fraction') 

You can also get fancier and indicate a constant offset at points:

 plt.annotate('Something', xy=(0, 1), xytext=(12, -12), va='top' xycoords='axes fraction', textcoords='offset points') 

See examples here for more details and more detailed examples here .

+31
Aug 12 '11 at 20:13
source share



All Articles