
Legends and annotations
Legends are an important element that is used to identify the plot
elements in a figure. The easiest way to show a legend inside a figure is to use the label
argument of the plot
function, and show the labels by calling the plt.legend()
method:
>>> x = np.linspace(0, 1, 20) >>> y1 = np.sin(x) >>> y2 = np.cos(x) >>> y3 = np.tan(x) >>> plt.plot(x, y1, 'c', label='y=sin(x)') >>> plt.plot(x, y2, 'y', label='y=cos(x)') >>> plt.plot(x, y3, 'r', label='y=tan(x)') >>> plt.lengend(loc='upper left') >>> plt.show()
The output for the preceding command as follows:

The loc
argument in the legend command is used to figure out the position of the label box. There are several valid location options: lower left
, right
, upper left
, lower center
, upper right
, center
, lower right
, upper right
, center right
, best
, upper center
, and center left
. The default position setting is upper right
. However, when we set an invalid location option that does not exist in the above list, the function automatically falls back to the best option.
If we want to split the legend into multiple boxes in a figure, we can manually set our expected labels for plot lines, as shown in the following image:

The output for the preceding command is as follows:
>>> p1 = plt.plot(x, y1, 'c', label='y=sin(x)') >>> p2 = plt.plot(x, y2, 'y', label='y=cos(x)') >>> p3 = plt.plot(x, y3, 'r', label='y=tan(x)') >>> lsin = plt.legend(handles=p1, loc='lower right') >>> lcos = plt.legend(handles=p2, loc='upper left') >>> ltan = plt.legend(handles=p3, loc='upper right') >>> # with above code, only 'y=tan(x)' legend appears in the figure >>> # fix: add lsin, lcos as separate artists to the axes >>> plt.gca().add_artist(lsin) >>> plt.gca().add_artist(lcos) >>> # automatically adjust subplot parameters to specified padding >>> plt.tight_layout() >>> plt.show()
The other element in a figure that we want to introduce is the annotations which can consist of text, arrows, or other shapes to explain parts of the figure in detail, or to emphasize some special data points. There are different methods for showing annotations, such as text
, arrow
, and annotation
.
- The
text
method draws text at the given coordinates(x, y)
on the plot; optionally with custom properties. There are some common arguments in the function:x
,y
, label text, and font-related properties that can be passed in viafontdict
, such asfamily
,fontsize
, andstyle
. - The
annotate
method can draw both text and arrows arranged appropriately. Arguments of this function ares
(label text),xy
(the position of element to annotation),xytext
(the position of the labels
),xycoords
(the string that indicates what type of coordinatexy
is), andarrowprops
(the dictionary of line properties for the arrow that connects the annotation).
Here is a simple example to illustrate the annotate
and text
functions:
>>> x = np.linspace(-2.4, 0.4, 20) >>> y = x*x + 2*x + 1 >>> plt.plot(x, y, 'c', linewidth=2.0) >>> plt.text(-1.5, 1.8, 'y=x^2 + 2*x + 1', fontsize=14, style='italic') >>> plt.annotate('minima point', xy=(-1, 0), xytext=(-1, 0.3), horizontalalignment='center', verticalalignment='top', arrowprops=dict(arrowstyle='->', connectionstyle='arc3')) >>> plt.show()
The output for the preceding command is as follows:
