Remove the legend on a matplotlib figure

MatplotlibLegend

Matplotlib Problem Overview


To add a legend to a matplotlib plot, one simply runs legend().

How to remove a legend from a plot?

(The closest I came to this is to run legend([]) in order to empty the legend from data. But that leaves an ugly white rectangle in the upper right corner.)

Matplotlib Solutions


Solution 1 - Matplotlib

As of matplotlib v1.4.0rc4, a remove method has been added to the legend object.

Usage:

ax.get_legend().remove()

or

legend = ax.legend(...)
...
legend.remove()

See here for the commit where this was introduced.

Solution 2 - Matplotlib

If you want to plot a Pandas dataframe and want to remove the legend, add legend=None as parameter to the plot command.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df2 = pd.DataFrame(np.random.randn(10, 5))
df2.plot(legend=None)
plt.show()

Solution 3 - Matplotlib

You could use the legend's set_visible method:

ax.legend().set_visible(False)
draw()

This is based on a answer provided to me in response to a similar question I had some time ago here

(Thanks for that answer Jouni - I'm sorry I was unable to mark the question as answered... perhaps someone who has the authority can do so for me?)

Solution 4 - Matplotlib

if you call pyplot as plt

frameon=False is to remove the border around the legend

and '' is passing the information that no variable should be in the legend

import matplotlib.pyplot as plt
plt.legend('',frameon=False)

Solution 5 - Matplotlib

you have to add the following lines of code:

ax = gca()
ax.legend_ = None
draw()

gca() returns the current axes handle, and has that property legend_

Solution 6 - Matplotlib

If you are not using fig and ax plot objects you can do it like so:

import matplotlib.pyplot as plt

# do plot specifics
plt.legend('')
plt.show()

Solution 7 - Matplotlib

According to the information from @naitsirhc, I wanted to find the official API documentation. Here are my finding and some sample code.

  1. I created a matplotlib.Axes object by seaborn.scatterplot().
  2. The ax.get_legend() will return a matplotlib.legned.Legend instance.
  3. Finally, you call .remove() function to remove the legend from your plot.
ax = sns.scatterplot(......)
_lg = ax.get_legend()
_lg.remove()

If you check the matplotlib.legned.Legend API document, you won't see the .remove() function.

The reason is that the matplotlib.legned.Legend inherited the matplotlib.artist.Artist. Therefore, when you call ax.get_legend().remove() that basically call matplotlib.artist.Artist.remove().

In the end, you could even simplify the code into two lines.

ax = sns.scatterplot(......)
ax.get_legend().remove()

Solution 8 - Matplotlib

I made a legend by adding it to the figure, not to an axis (matplotlib 2.2.2). To remove it, I set the legends attribute of the figure to an empty list:

import matplotlib.pyplot as plt

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twinx()

ax1.plot(range(10), range(10, 20), label='line 1')
ax2.plot(range(10), range(30, 20, -1), label='line 2')

fig.legend()

fig.legends = []

plt.show()

Solution 9 - Matplotlib

If you are using seaborn you can use the parameter legend. Even if you are ploting more than once in the same figure. Example with some df

import seaborn as sns

# Will display legend
ax1 = sns.lineplot(x='cars', y='miles', hue='brand', data=df)

# No legend displayed
ax2 = sns.lineplot(x='cars', y='miles', hue='brand', data=df, legend=None)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOlivier VerdierView Question on Stackoverflow
Solution 1 - MatplotlibnaitsirhcView Answer on Stackoverflow
Solution 2 - Matplotlibcast42View Answer on Stackoverflow
Solution 3 - MatplotlibERNView Answer on Stackoverflow
Solution 4 - MatplotlibDivingView Answer on Stackoverflow
Solution 5 - MatplotlibfcerutiView Answer on Stackoverflow
Solution 6 - MatplotlibPelonomi MoiloaView Answer on Stackoverflow
Solution 7 - MatplotlibAndrew LiView Answer on Stackoverflow
Solution 8 - Matplotlibboudewijn21View Answer on Stackoverflow
Solution 9 - MatplotlibGonzalo GarciaView Answer on Stackoverflow