In this tutorial, we will learn how to use Matplotlib to add legend to an existing plot. We can use Matplotlib to visualize data in different forms such as bar plots, charts, lines etc. However, none of these plots will be meaningful untill a legend is added to them. So, we need to first learn what a legend is. Why it is useful in a Matplotlib plot. And finally, we will learn how to write a simple Python program to achieve this.
What is a legend in Matplotlib plot?
A legend in a Matplotlib plot is a small infobox, whcih helps us in understanding what the plot is representing.
For example, let us take a look at an existing plot from our previous tutorial. It looks like this:
We can see that it is a two dimensional plot. It’s axes labels also tells us what data is used to plot it. However, there is one thing that is still not clear. We are seeing three lines drawn in the plot above. But what does each of these lines represent? That is the question that a legend of a plot will answer.
Each of the above three lines for example could be representing the acceleration of a metro line. So the orange line in the plot could be representing an orange metro line train’s acceleration. Green line in the plot corresponds to a green metro line’s acceleration and so on.
So to represent this information on the plot, we need to make use of legends.
Now that we understand what a legend in a plot is, let us learn how to add one to the above plot.
How to add legend to an existing Matplotlib plot
So let us go back to the Python code from our previous tutorial. It looked like this:
import matplotlib.pyplot as plt x = range(1, 10) plt.plot(x, [xi*1 for xi in x]) plt.plot(x, [xi*2 for xi in x]) plt.plot(x, [xi*3 for xi in x]) plt.grid() plt.axis([0, 20, 0 , 40]) plt.xlabel('This is the X axis label') plt.ylabel('This is the Y axis label') plt.title('Dummy Plot') plt.show()
So we had added axes labels and title for the plot in our previous tutorials. It is now time to add legends to the plot above. From the code, we can see that the three lines where generated by multiplying their x-axis values by 1, 2 & 3 respectively. In other words, we got the blue line by multiplying x-axis values with 1. Similarly, orange line by multiplying x-axis values with 2. And finally, green line by multiplying with 3.
So our Matplotlib plot should have a legend that shows Blue=1x, Orange=2x and Green=3x. Do you agree?
Now, to draw a legend in the output of a Matplotlib plot, we will make use of a special function called legend(). Go through theMatplotlib legend function’s documentation. We can see that we can add legend to a plot by simply passing legend’s texts as a list argument to this function. So with this in mind, if we add a line of code like this:
plt.legend(['Blue=1x', 'Orange=2x', 'Green=3x'])
then we should be getting our desired output.
So, our final code to add legend to Matplotlib plot will look something like this:
import matplotlib.pyplot as plt x = range(1, 10) plt.plot(x, [xi*1 for xi in x]) plt.plot(x, [xi*2 for xi in x]) plt.plot(x, [xi*3 for xi in x]) plt.legend(['Blue=1x', 'Orange=2x', 'Green=3x']) plt.grid() plt.axis([0, 20, 0 , 40]) plt.xlabel('This is the X axis label') plt.ylabel('This is the Y axis label') plt.title('Dummy Plot') plt.show()
Notice that we added our legend() function call right after plotting the three lines. The legend values in legend() is also passed in the same order as to how they are plotted. That is we are plotting the lines in the order blue, orange and green. So our legend function’s parameter also lists legends for blue, orange and green in that same order.
With this, our final output plot looks like this:
Using Matplotlib to add legend to an existing plot is not difficult. It is as simple as calling Matplotlib library’s legend() function. However, we have to ensure that we are passing the legend parameters in the correct order. So that is all for this tutorial. If you still have any questions about this, do let us know in the comment below. So until next time, ciao! 🙂