question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pandas.DataFrame.plot(): Labels do not appear in legend

See original GitHub issue

The following code plots two lines. The column names appear in the legend.

x=np.linspace(-10,10,201)
y,z=np.sin(x),np.cos(x)
x,y,z=pd.Series(x),pd.Series(y),pd.Series(z)
df=pd.concat([x,y,z],axis=1)
df.columns=['x','sin(x)','cos(x)']
df=df.set_index('x')
df.plot()
plt.show()
plt.clf();plt.close()

figure_1

However, the following equivalent code shows None as legend, even though the labels are explicitly set.

ax=df.plot(y='sin(x)',label='sin(x)')
df.plot(y='cos(x)',label='cos(x)',ax=ax)
plt.show()

Of course there are alternative ways to make this work, but it appears to me that passing the labels should suffice. In particular, I don’t think their values should be replaced with None, or printed as x-label.

figure_2

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
MaxGheniscommented, Dec 13, 2019

Similar to https://github.com/pandas-dev/pandas/issues/9542#issuecomment-438580042, label is ignored when plotting a one-column DataFrame:

import pandas as pd
df = pd.DataFrame({'a': [2, 3]}, index=[0, 1])
df.plot(label='b')

image

df.plot(label=['b']) also doesn’t work. I’m on 0.25.3.

2reactions
zohimchandanicommented, Oct 18, 2021

Having the same issue as previously noted. Using

df[["Training Cost"]].plot(ax=axes[0,0], title = 'Training set', label = 'ADAM')

but the output plot has ‘Training cost’ as the label instead of the specified ‘ADAM’. Wondering what the fix is?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Legend only shows one label when plotting with pandas
When multiple series are plotted then the legend is not displayed by default. The easy way to display custom legends is just to...
Read more >
pandas.DataFrame.plot — pandas 1.5.2 documentation
When using a secondary_y axis, automatically mark the column labels with “(right)” in the legend. If True, boolean values can be plotted.
Read more >
Pandas: How to Create and Customize Plot Legends - Statology
This tutorial explains how to create and customize a plot legend in pandas, including several examples.
Read more >
Matplotlib Examples: Displaying and Configuring Legends
While you can just pass a list with multiple texts to plt.legend(), it's better to label each plot individually so there are no...
Read more >
Customizing Plot Legends | Python Data Science Handbook
As we have already seen, the legend includes all labeled elements by default. If this is not what is desired, we can fine-tune...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found