Plot legends give meaning to a visualization, assigning meaning to the various plot elements. We previously saw how to create a simple legend; here we'll take a look at customizing the placement and aesthetics of the legend in Matplotlib.
The simplest legend can be created with the
plt.legend() command, which automatically creates a legend for any labeled plot elements:
But there are many ways we might want to customize such a legend. For example, we can specify the location and turn off the frame:
We can use the
ncol command to specify the number of columns in the legend:
We can use a rounded box (
fancybox) or add a shadow, change the transparency (alpha value) of the frame, or change the padding around the text:
For more information on available legend options, see the
As we have already seen, the legend includes all labeled elements by default.
If this is not what is desired, we can fine-tune which elements and labels appear in the legend by using the objects returned by plot commands.
plt.plot() command is able to create multiple lines at once, and returns a list of created line instances.
Passing any of these to
plt.legend() will tell it which to identify, along with the labels we'd like to specify:
I generally find in practice that it is clearer to use the first method, applying labels to the plot elements you'd like to show on the legend:
Notice that by default, the legend ignores all elements without a
label attribute set.
Sometimes the legend defaults are not sufficient for the given visualization. For example, perhaps you're be using the size of points to mark certain features of the data, and want to create a legend reflecting this. Here is an example where we'll use the size of points to indicate populations of California cities. We'd like a legend that specifies the scale of the sizes of the points, and we'll accomplish this by plotting some labeled data with no entries:
The legend will always reference some object that is on the plot, so if we'd like to display a particular shape we need to plot it. In this case, the objects we want (gray circles) are not on the plot, so we fake them by plotting empty lists. Notice too that the legend only lists plot elements that have a label specified.
By plotting empty lists, we create labeled plot objects which are picked up by the legend, and now our legend tells us some useful information. This strategy can be useful for creating more sophisticated visualizations.
Finally, note that for geographic data like this, it would be clearer if we could show state boundaries or other map-specific elements. For this, an excellent choice of tool is Matplotlib's Basemap addon toolkit, which we'll explore in Geographic Data with Basemap.
Sometimes when designing a plot you'd like to add multiple legends to the same axes.
Unfortunately, Matplotlib does not make this easy: via the standard
legend interface, it is only possible to create a single legend for the entire plot.
If you try to create a second legend using
ax.legend(), it will simply override the first one.
We can work around this by creating a new legend artist from scratch, and then using the lower-level
ax.add_artist() method to manually add the second artist to the plot: