# Putting text in top left corner of matplotlib plot

StackOverflow

How can I put text in the top left (or top right) corner of a matplotlib figure, e.g. where a top left legend would be, or on top of the plot but in the top left corner? E.g. if it"s a plt.scatter(), then something that would be within the square of the scatter, put in the top left most corner.

I"d like to do this without ideally knowing the scale of the scatterplot being plotted for example, since it will change from dataset to data set. I just want it the text to be roughly in the upper left, or roughly in the upper right. With legend type positioning it should not overlap with any scatter plot points anyway.

thanks!

You can use `text`.

``````text(x, y, s, fontsize=12)
``````

`text` coordinates can be given relative to the axis, so the position of your text will be independent of the size of the plot:

The default transform specifies that text is in data coords, alternatively, you can specify text in axis coords (0,0 is lower-left and 1,1 is upper-right). The example below places text in the center of the axes::

``````text(0.5, 0.5,"matplotlib",
horizontalalignment="center",
verticalalignment="center",
transform = ax.transAxes)
``````

To prevent the text to interfere with any point of your scatter is more difficult afaik. The easier method is to set y_axis (ymax in `ylim((ymin,ymax))`) to a value a bit higher than the max y-coordinate of your points. In this way you will always have this free space for the text.

EDIT: here you have an example:

``````In [17]: from pylab import figure, text, scatter, show
In [18]: f = figure()
In [20]: scatter([3,5,2,6,8],[5,3,2,1,5])
Out[20]: <matplotlib.collections.CircleCollection object at 0x0000000007439A90>
In [21]: text(0.1, 0.9,"matplotlib", ha="center", va="center", transform=ax.transAxes)
Out[21]: <matplotlib.text.Text object at 0x0000000007415B38>
In [22]:
``````

The ha and va parameters set the alignment of your text relative to the insertion point. ie. ha="left" is a good set to prevent a long text to go out of the left axis when the frame is reduced (made narrower) manually.