Entering text in the upper left corner of the matplotlib chart

How can I put text in the upper left (or upper right) corner of a matplotlib shape, for example. where would the upper left legend be, or on top of the plot, but in the upper left corner? For example. if it is plt.scatter (), then something that would be within the scatter square is placed in the upper left corner.

I would like to do this without knowing how much the scale of the graph is scattering, for example, as it will change from a data set to a data set. I just want the text to be roughly in the upper left corner or roughly in the upper right corner. When positioning a legend type, it should not overlap all scatter points.

thank!

+63
python matplotlib plot
Dec 12 '11 at 23:48
source share
2 answers

You can use text .

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

text coordinates can be set relative to the axis, so the position of your text does not depend on the size of the chart:

The default conversion indicates that the text is in the data coordinates, as an alternative, you can specify the text in the axial coordinates (0,0 - lower left and 1,1 - upper right). The example below puts the text in the center of the axis:

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

To prevent text from interfering anywhere in your scatter, afaik is harder. An easier way is to set y_axis (ymax in ylim((ymin,ymax)) ) to a value slightly above the maximum y-coordinate of your points. This way you will always have free space for text.

EDIT: here you have an example:

 In [18]: f = figure() In [19]: ax = f.add_subplot(111) 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]: 

enter image description here

The ha and va options set the alignment of your text relative to the insertion point. i.e. ha = 'left' is a good set to prevent long text coming out of the left axis when the frame is reduced (done already) manually.

+95
Dec 12 '11 at 23:59
source share

One solution would be to use the plt.legend function, even if you don't need the actual legend. You can specify the location of the legend window using the loc keyterm key. More information can be found on this website , but I also gave an example showing how to place the legend:

 ax.scatter(xa,ya, marker='o', s=20, c="lightgreen", alpha=0.9) ax.scatter(xb,yb, marker='o', s=20, c="dodgerblue", alpha=0.9) ax.scatter(xc,yc marker='o', s=20, c="firebrick", alpha=1.0) ax.scatter(xd,xd,xd, marker='o', s=20, c="goldenrod", alpha=0.9) line1 = Line2D(range(10), range(10), marker='o', color="goldenrod") line2 = Line2D(range(10), range(10), marker='o',color="firebrick") line3 = Line2D(range(10), range(10), marker='o',color="lightgreen") line4 = Line2D(range(10), range(10), marker='o',color="dodgerblue") plt.legend((line1,line2,line3, line4),('line1','line2', 'line3', 'line4'),numpoints=1, loc=2) 

Note that since loc=2 , the legend is in the upper left corner of the graph. And if the text overlaps with the graph, you can reduce it using legend.fontsize , which then reduces the conventions.

+7
Dec 13 2018-11-12T00:
source share



All Articles