Pandas plot() without a legend

PythonPandasPlot

Python Problem Overview


Using the pandas library in python and using

.plot()

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

Python Solutions


Solution 1 - Python

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

df.plot(legend=False)

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)

Solution 2 - Python

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

plt.gca().get_legend().remove()

assuming that you have imported matplotlib.pyplot as plt or

ax.get_legend().remove()

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

df.plot(legend=False)

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
QuestionBilal Syed HussainView Question on Stackoverflow
Solution 1 - PythonNipun BatraView Answer on Stackoverflow
Solution 2 - PythonImportanceOfBeingErnestView Answer on Stackoverflow