How can a legend be removed in Matplotlib? 2

How can a legend be removed in Matplotlib?

Matplotlib is a widely used data visualization library in Python that allows users to create various types of graphs and charts. Legends, which are small boxes that provide information about the elements in a graph, are often included in these visualizations. However, there are times when a legend may not be necessary or desired. In such cases, the legend can be easily removed using the “legend” function in Matplotlib. By setting the “legend” argument to “None”, the legend will be removed from the graph. This simple and straightforward approach allows users to customize their graphs and charts according to their specific needs and preferences.

Remove a Legend in Matplotlib (With Examples)


You can use the following basic syntax to remove a legend from a plot in Matplotlib:

import matplotlib.pyplotas plt

plt.legend('', frameon=False)

The quotation marks ‘ ‘ tell Matplotlib to place no variables in the legend and the frameon argument tells Matplotlib to remove the frame around the legend.

The following example shows how to use this syntax in practice.

Example: Remove a Legend in Matplotlib

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'position': ['G', 'G', 'F', 'F', 'G', 'F', 'F', 'F'],
                   'points': [5, 7, 7, 9, 12, 9, 9, 4]})

#view DataFrame
print(df)

  team position  points
0    A        G       5
1    A        G       7
2    A        F       7
3    A        F       9
4    B        G      12
5    B        F       9
6    B        F       9
7    B        F       4

We can use the following code to create a stacked bar chart to visualize the total points scored by players on each team and each position:

import matplotlib.pyplotas plt#create stacked bar chart
df.groupby(['team', 'position']).size().unstack().plot(kind='bar', stacked=True)

Notice that Matplotlib places a legend in the top right corner by default.

To create this same plot without the legend, we can use the following code:

import matplotlib.pyplotas plt#create stacked bar chart
df.groupby(['team', 'position']).size().unstack().plot(kind='bar', stacked=True)

#remove legend
plt.legend('', frameon=False)

remove legend in Matplotlib

This stacked bar chart matches the previous chart, but the legend has been removed.

Also note that we can use the same syntax to remove a legend from any plot in Matplotlib.

For example, the following code shows how to create a pie chart in Matplotlib and remove the legend:

import matplotlib.pyplot as plt

#create pie chart that shows total points scored by team
df.groupby(['team']).sum().plot(kind='pie', y='points')

#remove legend
plt.legend('', frameon=False)

The result is a pie chart with no legend.

The following tutorials explain how to perform other common tasks in Matplotlib:

How to Remove Ticks from Matplotlib Plots
How to Change Font Sizes on a Matplotlib Plot

Cite this article

stats writer (2024). How can a legend be removed in Matplotlib?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-legend-be-removed-in-matplotlib/

stats writer. "How can a legend be removed in Matplotlib?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-a-legend-be-removed-in-matplotlib/.

stats writer. "How can a legend be removed in Matplotlib?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-legend-be-removed-in-matplotlib/.

stats writer (2024) 'How can a legend be removed in Matplotlib?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-legend-be-removed-in-matplotlib/.

[1] stats writer, "How can a legend be removed in Matplotlib?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can a legend be removed in Matplotlib?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top