Adding a Legend to Pyplot in Matplotlib in the Simplest Manner

PyPlotLegendsFeaturedImage

A Legend in a Graph is considered a guide to understanding the graph. When a graph has more than one output, the outputs are usually classified by visually differentiable features such as colors, lines, etc. A legend helps the viewer understand what the graph is implying.

Graphical Visualisation is very important in fields such as Data Analysis and Machine Learning. It helps users classify data, help select features that are important to model, and chart out relations between different features. Graphical visualization helps users to remove redundant features, train models on relationships deduced from the graphs, analyze outliers and remove them.

In Python, the PyPlot module of the Matplotlib library does the task. It allows the user to not only plot the graph but also helps them plot the legends, plotting different graphs such as Line charts, Scatter plots, Bargraph, Histograms, etc., and assigning different colors to the graphs. PyPlot helps in the complete customization of the graph. The plot() method plots the graph based on X Axis and Y Axis inputs, and the show() method visually shows the graph to the user.

Also Read: Python Matplotlib Tutorial

Adding Legend Using Label Parameter of Plot()

This is the simplest method to add a legend to a graph in PyPlot. The plot() method takes a parameter called ‘label,’ which allows the user to define a name for the graph they are plotting. It will be described further, but first, the values to be plotted on X Axis and Y Axis must be defined. They are defined in the code segment below:

import matplotlib.pyplot as plt
X=[i for i in range(-10,10)]
Y1=[(9*x+23) for x in X]
Y2=[((3*x*x)+(8*x)+9) for x in X]

The code above initializes three lists: X, Y1, and Y2. X is a list of integers from -10 to 9. Y1 is a list of inputs from X for a linear equation, while Y2 is a list of outputs based on inputs from X for a quadratic equation. The equations are as shown below:

Linear Equation:

y=9x+23

Quadratic Equation:

y=3x2 + 8x + 9

Now, these inputs can be given to the plot() function of PyPlot, and we can see the plotted graphs. The following piece of code does the same:

plt.plot(X,Y1,'-r',label='Linear Equation')
plt.plot(X,Y2,'-b',label='Quadratic Equation')
plt.legend(loc='upper right')
plt.show()

In the above code, plt is an alias of PyPlot, which was imported in the code snippet above. The plot () method takes input for X Axis and Y Axis as the first two parameters. The ‘-r’ string in the code tells the plot() function to plot a line graph with red color. While the ‘-b’ string tells the plot() function to plot a line graph with blue color. The label parameter of the plot() function lets us name the graphs in our code. We call the graph plotting the linear equation a ‘Linear Equation’ and the graph plotting outputs of a quadratic equation ‘Quadratic Equation.’ The legend will be visible on the graph using the legend() function of the PyPlot. The loc parameter given to the legend() function describes the location of the legend in the graph, which in this case is the upper right corner. plt.show() is used to show the graph visually. The code produces the following output:

Legends Using Label Parameter Of Plot 1
Legends Using Label Parameter Of Plot()

Customized Legends in PyPlot

PyPlot allows the users to add customized graph names, which are names that can be imported from iterables like lists and tuples inside the program. This allows a more dynamic approach to the plotting of graphs and is another simpler way of adding legends to a graph. Customized legends are usually used when the data is more complex and detailed. Instead of labeling the graph every time we call the plot() function, the labels are induced by the legend() function through an iterable.

The following code demonstrates customized legends:

plt.plot(X,Y1,'-r')
plt.plot(X,Y2,'-g')
lst=['Linear Equation','Quadratic Equation']
plt.legend(lst,loc='upper right')

This code snippet takes the values of X, Y1, and Y2 from the code snippets above. But here, the plot() function simply takes X-axis values and Y-axis values. The ‘-g’ in Line 10 asks the plot() function to plot the line graph in Green color. The lst variable is a list iterable, which is passed as a reference to the legend() function. The legend() function takes the names from the list iterable and assigns them to plotted graphs. The code produces the following output:

Customised Legends In PyPlot
Customized Legends In PyPlot

It should be noted here that we get a notification saying the graph is a matplotlib.legend.Legend object and not a graph object of PyPlot. This is because it is the legend() function that assigns the name to the graphs and not the plot() function.

But what happens if the iterable has either more or fewer values than the number of plots in the graph? Customized legends work on a First Come, First Serve basis. The legend() function will take the first N number of values from the iterable, where N is the total number of graphs. If there are more values in the iterable, the rest of the values would be ignored. If there are lesser values in the iterable than the number of plots, then only the first M number of graphs will receive a name, where M is the length of the iterable. The outputs would be as shown:

Customised Legends In PyPlot 1
Customized Legends when Number of values in Iterable is more than number of Graphs
Customised Legends When The Iterable Has Less Values Than Plots
Customized Legends When The Number of values in Iterable is less than the number of Graphs

Legends in a Pie Chart

A pie chart is one of the most widely used graphs to represent data. But using normal legends in a Pie Chart will not be favorable as the legends will overlap the chart and not allow the user to properly understand the chart. The pie chart is plotted using the pie() function of PyPlot. It is demonstrated below:

clubs=['Arsenal','Manchester City','Newcastle United','Manchester United','Tottenham Hotspurs','Liverpool','Chelsea']
percentages=['75','15','5','3','1','0.5','0.5']
plt.pie(percentages)
plt.legend(clubs,loc='upper right')
plt.show()

The above code, clubs, is a list that contains a list of clubs playing in the Premier League right now. The percentages is a list that determines the possibility of the club winning the Premier League respectively. The pie chart is plotted based on the percentages list, and the name of each portion in the pie chart is given using the legend() function as the club name. The legend() function provides customized legends by taking clubs as the iterable. The location is also set to the upper right. But the code will not work as it will produce a messy and uninterpretable output. The output is shown below:

Pie Chart With Normal Legends
Pie Chart With Normal Legends

To remove this kind of error, a parameter in legend() function is used. The bbox_to_anchor parameter is used to instruct the legend() function on where it should produce the legends on the graphic. This provides a neat and interpretable output. It is demonstrated below:

colors=['#FF0000','#87CEEB','#000000','#FFFF00','#FFFFFF','#AA4A44','#00008B']
plt.pie(percentages,colors=colors)
plt.legend(clubs,loc='center left',bbox_to_anchor=(1,0.5))
plt.show()

Here, the colors parameter of the pie() function is also used to assign colors to the pie chart as per the club colors. The colors have been initialized in their hexadecimal format in the list of colors. The location in the legend() function has been changed to produce a clear output, and the bbox_to_anchor parameter instructs the legend() function to push the legends away from the graph based on the input given to it. The input is given in an (x,y) format where the legend is pushed x units on the X axis and y units on the Y axis away from the graph. It produces the following output:

Pie Chart With Customized Colors And Legends
Pie Chart With Customized Colors And Legends

Also read: How to Plot and Customize a Pie Chart in Python?

Conclusion

Customized legends in PyPlot can be considered the easiest method to implement legends in a graph. It allows a dynamic approach, unlike the ‘label’ parameter of the plot() function, which needs to be called every time the graph is plotted. Customized legends are also useful in other graph types, such as Pie Charts and Bar Graphs.

The legend() function will work fine with a Line Graph or a Scatter Plot, but it will intercept with the graph in a Pie Chart or a Bar Graph. Hence, the bbox_to_anchor parameter shall always be used when using legend() function with a pie chart or a bar graph.

References

https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.legend.html