automatically position text box in matplotlib

Anake picture Anake · Aug 12, 2011 · Viewed 54.5k times · Source

Is there a way of telling pyplot.text() a location like you can with pyplot.legend()?

Something like the legend argument would be excellent:

plt.legend(loc="upper left")

I am trying to label subplots with different axes using letters (e.g. "A","B"). I figure there's got to be a better way than manually estimating the position.

Thanks

Answer

Joe Kington picture Joe Kington · Aug 12, 2011

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

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

You could also get fancier and specify a constant offset in points:

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

For more explanation see the examples here and the more detailed examples here.