This legend guide is an extension of the documentation available at legend()—please ensure you are familiar with contents of the documentation (https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.legend) before proceeding with this guide.
xlabel() and ylabel() function parameters are listed in following tables:
Axes.set_xlabel(xlabel, fontdict=None, labelpad=None, **kwargs)
Set the label for the x axis.
Parameters |
xlabel: string, x label labelpad: scalar, optional, default: None Spacing in points between the label and the x axis |
Axes.set_ylabel(ylabel, fontdict=None, labelpad=None, **kwargs)
Set the label for the y axis:
Parameters |
ylabel: string, y label labelpad: scalar, optional, default: None Spacing in points between the label and the x axis |
This guide makes use of some common terms, which are documented here for clarity:
Legend, entry: A legend is made up of one or more legend entries. An entry is made up of exactly one key and one label.legend key. The colored/patterned marker to the left of each legend label.legend label. The text which describes the handle represented by the key.legend handle.
The original object which is used to generate an appropriate entry in the legend.
To change the text used for each data series in the legend (the default is the column name from DataFrame), capture the ax object returned from the .plot() method and use its .legend() method. This object is an AxesSubplot object and can be used to change various aspects of the plot before it is generated:
The location of the legend can be set using the loc parameter of .legend(). By default, pandas sets the location to best, which tells Matplotlib to examine the data and determine the best place it thinks to put the legend. However, you can also specify any of the following to position the legend more specifically (you can use either the string or the numeric code):
Text |
Code |
best |
0 |
upper right |
1 |
upper left |
2 |
lower left |
3 |
lower right |
4 |
'right |
5 |
center left |
6 |
center right |
7 |
lower center |
8 |
upper center |
9 |
center |
10 |
The following example demonstrates placing the legend in the upper-center portion of the graph: