A Guide to Customizing Legends in MatPlotLib: Topics in MatPlotLib

An Introduction to Customizing Legends in MatPlotLib

We are now quite deep into our discussion of some rather complex features of MatPlotLib and its various plotting features. In particular, we have discussed in great depths the plotting of simple line plots, scatter plots, plotting density and contour plots, as well as a variety of histogram types. Before moving forward, we advise you check these out, as the contents of the article herein will be meaningless without some basis to apply the knowledge. Herein, we go into great depth discussing customization features applicable to the various MatPlotLib chart types. In particularly, we thoroughly investigate customizing legends in MatPlotLib plots.

Customizing Legends in MatPlotLib

Legends are often overlooked in terms of their importance for data modeling. However, this could not be further from the truth. On the other hand, legends are essential to the process of conveying information to individuals viewing a data plot. MatPlotLib makes controlling these plot attributes rather simple using the plt.legend() function. This legend can be applied to all of the plot types we discuss in our previous articles; however, there is an effective way of modeling legends and an ineffective way.

If all we do is create a plot and specify the ‘plt.legend()’ function, MatPlotLib will automatically create a legend for you. This, of course, is provided that when we plot our data/functions that they possess proper labels. Let’s take a look at the plot produced while using the plt.legend function in its most basic form:

As previously performed, we created a data set using Numpy’s ‘linspace’ function with 10,000 data points. We then create a linear plot using the plt.subplots function, and then plot the sine and cosine functions accordingly. Next, we make sure to include labels for each of these functions. We use the ax.axis function and set the argument to ‘equal’ which makes distance traversed in the ‘y’ dimension equal to distance traversed in the ‘x’ dimension. Finally, we specify the plt.legend function which automatically creates the legend of the plot. Let’s take a look at the plot created by this code:

Note that the plt.legend function automatically takes the labels we assigned to each function and also specifies the color and line type associated with these function labels. This default makes it much easier for encoding simple legends.

Customizing Legend Locations

Had we not utilized the ax.axis function and specified the plot axis as ‘equal’, it would have created a plot of the following form:

Notice when we do this, the functions and the legend are indiscernibly overlapped. Now, we could simply activate the legend frame and set its transparency for 1. The code for doing this looks as follows:

With this code, we create a border around our function and with frame alpha set equal to 1, the legend becomes opaque. The legend, as a result, looks as follows:

As you may observe, the legend is much more visible. However, it may not be in the location we desire. Nevertheless, MatPlotLib offers a variety of locations which we may move our legend to using the location keyword argument. For example, if we wish to change the location of the legend to the bottom right, we can execute the following code:

Setting the loc keyword argument equal to the lower right location, we create a plot of the following form:

The user can explore with their plot and different legend locations to discern where the best location for the structure is.

In addition to controlling legend location, we can also specify for how the legend looks. For example, in its present form, the legend consists of two rows and one column. This was done by default via MatPlotLib. However, what if we want to generate a legend designed such that items are presented in one row and two columns. We can specify this by passing an integer in the ‘ncol’ argument of the legend function. The default for this is an integer value of one, but let’s see the code when we change this to two.

By setting the ‘ncol’ argument to two, this creates the plot that follows:

Customizing Legend Elements

Take a moment to bring to the forefront of your mind the fundamental purpose of the plot legend: to organized all labeled elements that exist within a plot. Because multiple lines can be plotted and labeled simultaneously, the MatPlotLib plt.legend function defaults automatically to presenting all labeled elements associated with the plot. Now, even if the plot elements aren’t already labeled in the plot function, you can specify their labels in the plt.legend function. Let’s take a look at how this might work by removing the function labels then creating labels in the plt.legend function. Let’s first take a look at this code:

We see that the labels in the ax.plot function have been removed but we can specify them in the plt.legend function. It creates the same plot:

Legend for Size of Points

Our previous section focused on simple legends for line plots. However, these are not the only plot types we employ. When workig with scatter plots, markers are typically of a specific size. Suppose that for a given data set, points plot according to their ‘x’ and ‘y’ values, but the size of the marker reflects the area of a city and the color reflects the population. First of all, how might we code this in our program?

We can generate our data using the random.sample function, specifying ranges for ‘x’, ‘y’, city size, and population. We can then encode our scatter plot with the plt.scatter function, ensuring we specify the color content relative to the population, the size of the marker relative to the area, and setting the transparency to .5 with the alpha argument. The code looks like this:

Now, the following code produces a plot that appears as follows:

So, we have effectively created the scatter plot of our data, but note that we really have no idea what the size of the markers or the color of the markers represent. This is where legends and color bars come in.

Adding Customizations

Let’s make our plot a bit more professional firstly by adding axis labels and chart titles. We can do this using the plt.xlabel, plt.ylabel, and plt.title functions. Once we have specified these features, let’s create a color bar with the plt.colorbar function and provide it with its own label. Additionally, let’s provide this color bar with limits. Because population can range from 5,000 to 100,000, let’s set these boundaries with the plt.clim function. The code for stipulating all of these features appears as:

This code produces a plot that looks like this:

So, our color bar efficaciously denotes values associated with a particular hue. However, we don’t really have a concept of the significance of marker size. To allow for this, we can create a legend that associates the size of the marker with a particular size. If you think about it, this legend will be a type of scatter plot in and of itself because it will consist of markers of different sizes. Therefore, we can encode this legend first by using the plt.scatter function. Because the city size may range from 100 to 700, let’s create a legend consisting of three representative sizes, 100, 400, and 700. The code for creating this legend appears as follows:

The plot produced appears as follows:

The Take Away

The MatPlotLib library provides options for creating plot legends. Customizing legends in MatPlotLib is essential for developing professional looking plots as the legend provides key information for interpreting graphs. In addition to legend location, we can also create legends that represent a particular size and shape. These are applicable to all of the plot types we have seen thus far. Our subsequent article goes into detail about customizations of the MatPlotLib color bar. However, if you seek to explore this subject further, check out the MatPlotLib manual which exists here.

Leave a Reply

%d bloggers like this: