Remove the legend on a matplotlib figure

Olivier Verdier picture Olivier Verdier · Apr 20, 2011 · Viewed 174k times · Source

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

Answer

naitsirhc picture naitsirhc · Nov 10, 2014

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.