Python

Customize your Matplotlib Legend

By ayed_amira , on 05/02/2020 , updated on 09/10/2020 - 5 minutes to read
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 :

TitleBudgetbox_office
Avengers: Endgame356000000$2797800564$
Avatar237000000$2790439000$
Titanic200000000$2194439542$
Star Wars VII245000000$2068223624$
Avengers: Infinity450000000$2048359754$
Jurassic World150000000$1670400637$
The Lion King260000000$1656943394$
Avengers220000000$1518812988$
Fast and Furious 7190000000$1515047671$
Frozen 2150000000$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 :

box offfice example

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 box offfice example

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()
 
matplotlmib legend inside bottom

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 top

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:

https://matplotlib.org/

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):

Main menu

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.