Python

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

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

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

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

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

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