Pandas plot() without a legend


Using the pandas library in python and using


on a dataframe, how do I display the plot without a legend?

Asked By: Bilal Syed Hussain



There is a parameter in the function corresponding to legend; by default it is True


Following is the definition of the .plot() method

Definition: df.plot(frame=None, x=None, y=None, subplots=False, sharex=True, sharey=False, use_index=True, figsize=None, grid=None, legend=True, rot=None, ax=None, style=None, title=None, xlim=None, ylim=None, logx=False, logy=False, xticks=None, yticks=None, kind=’line’, sort_columns=False, fontsize=None, secondary_y=False, **kwds)

Answered By: Nipun Batra

In order to remove a legend that has once been drawn, use


assuming that you have imported matplotlib.pyplot as plt or


if ax is the axes where the legend resides.

Alternatively, see Nipun Batras answer if there is some choice to turn the legend off from the beginning in which case one can simply use


I prefer the set_visible(False) syntax because it aligns nicely with other syntaxes that hide stuff, such as hiding spines.

import pandas as pd
df = pd.DataFrame({'A': range(11), 'B': 2, 'C': 5})

ax = df.plot()
ax.spines[['right', 'top']].set_visible(False)


Answered By: cottontail
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.