pandas.DataFrame.plot(): Labels do not appear in legend
See original GitHub issueThe 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()
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.
Issue Analytics
- State:
- Created 9 years ago
- Comments:22 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Similar to https://github.com/pandas-dev/pandas/issues/9542#issuecomment-438580042,
label
is ignored when plotting a one-column DataFrame:df.plot(label=['b'])
also doesn’t work. I’m on 0.25.3.Having the same issue as previously noted. Using
but the output plot has ‘Training cost’ as the label instead of the specified ‘ADAM’. Wondering what the fix is?