Customize your Matplotlib Legend

Customize your Matplotlib Legend : For a graph to be understandable to everyone, it is important to put a legend on it. In this tutorial we will see how to position the legend of a graph using the Matplotlib python library.
Overview
Graphs are very important in data analysis. They provide a visual representation of the data, which improves the understanding of the data. In python, there is a library called Matplotlib that gives us the ability to plot a wide variety of graphs. Without a caption a graph does not really make sense, so it is necessary to incorporate a clear and understandable caption to these graphs. We will therefore see in the rest of this article, how to define a legend in a graph and how to position it according to these preferences
Matplotlib legend
Import Matplotlib and Pandas
We will first define a dataset to illustrate the different examples. We will use the Pandas library to allow reading a csv file. In our example, the dataset will contain the list of the world’s biggest box office hits with the following structure :
Title | Budget | box_office |
Avengers: Endgame | 356000000$ | 2797800564$ |
Avatar | 237000000$ | 2790439000$ |
Titanic | 200000000$ | 2194439542$ |
Star Wars VII | 245000000$ | 2068223624$ |
Avengers: Infinity | 450000000$ | 2048359754$ |
Jurassic World | 150000000$ | 1670400637$ |
The Lion King | 260000000$ | 1656943394$ |
Avengers | 220000000$ | 1518812988$ |
Fast and Furious 7 | 190000000$ | 1515047671$ |
Frozen 2 | 150000000$ | 1450026933$ |
You can see that Disney is a big hit at the worldwide box office 🙂
Now we want to see what the budget represents in the worldwide revenues of these films. Here is the code to get this result :
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('classic')
df = pd.read_csv("box_office.csv", sep="\t")
print(df.head(3))
ax = plt.gca()
df.plot(kind='bar',x='Title',y='box_office',color='blue',ax=ax)
df.plot(kind='bar',x='Title',y='Budget',color='red',ax=ax)
plt.show()
This is what you get :

To obtain this graph, we used two bar graphs using the “kind” parameter of the plot function. Now, we want to change the position of the legend
Matplotlib legend inside
By default the caption is inside the graph so you don’t need to specify an extra parameter to do so:
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('classic')
df = pd.read_csv("box_office.csv", sep="\t")
print(df.head(3))
ax = plt.gca()
ax = plt.subplot(111)
df.plot(kind='bar',x='Title',y='box_office',color='blue',ax=ax)
df.plot(kind='bar',x='Title',y='Budget',color='red',ax=ax)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05))
plt.title('Legend inside')
ax.legend()
plt.show()
The legend() function is used to define the position of the legend in relation to the graph. Here is what we get when we specify no parameter in this function.

Matplotlib legend on bottom
To place the legend at the bottom of the graph, simply fill in the following parameters:
ax.legend(loc='upper center',bbox_to_anchor=(0.5, -0.05), ncol=2)
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('classic')
df = pd.read_csv("box_office.csv", sep="\t")
print(df.head(3))
ax = plt.gca()
ax = plt.subplot(111)
df.plot(kind='bar',x='Title',y='box_office',color='blue',ax=ax)
df.plot(kind='bar',x='Title',y='Budget',color='red',ax=ax)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05))
plt.title('Legend inside')
ax.legend(loc='upper center',bbox_to_anchor=(0.5, -0.05), ncol=2)
plt.show()

Matplotlib legend on top
To do so, just delete the “bbox_to_anchor” parameter, like this :
ax.legend(loc='upper center', ncol=2)

Matplotlib legend Outside
To make the graph more readable, it can be interesting to place the caption outside the graph for more visibility. Here is how to do this:
ax.legend(loc='upper center',bbox_to_anchor=(0.5, 1.0),ncol=2, bbox_transform=plt.gcf().transFigure)
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use('classic')
df = pd.read_csv("box_office.csv", sep="\t")
print(df.head(3))
ax = plt.gca()
ax = plt.subplot(111)
df.plot(kind='bar',x='Title',y='box_office',color='blue',ax=ax)
df.plot(kind='bar',x='Title',y='Budget',color='red',ax=ax)
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.05))
plt.title('Legend outside')
ax.legend(loc='upper center',bbox_to_anchor=(0.5, 1.0),ncol=2, bbox_transform=plt.gcf().transFigure)
plt.show()

Removing legend
If you do not want to display the legend you can use this function:
ax.legend_.remove()
Conclusion
The function is very handy to organize your graphic in a more visible way. There are a lot of options that I have not mentioned in this article, I invite you to visit this site for an exhaustive list of functions:
I hope this tutorial has given you a better understanding of the legends of a matplotlib chart. If you want to know more about matplotlib, check out this book (As an Amazon Partner, I make a profit on qualifying purchases):
Comments
Leave a comment