Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Series.plot() ignores label keyword #10119

Closed
goyodiaz opened this issue May 13, 2015 · 9 comments · Fixed by #10131
Closed

Series.plot() ignores label keyword #10119

goyodiaz opened this issue May 13, 2015 · 9 comments · Fixed by #10131
Labels
Regression Functionality that used to work in a prior pandas version Visualization plotting
Milestone

Comments

@goyodiaz
Copy link
Contributor

Using pandas 0.16.1, tested in several environments:

>>> import pandas as pd
>>> s = pd.Series([0, 1])
>>> ax = s.plot(label='stuff')
>>> assert ax.lines[0].get_label() == 'stuff'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError

This is a regression since 0.16.0. I think the problem is in this line in pandas.tools.plotting.MPLPlot._compute_plot_data():
label = self.kwds.pop('label', None)
It should read:
label = self.label
because the label has been popped before and stored in an attribute.

I want to make a pull request but it will take some time to install git, set up a development environment and get my fingers to relearn their way through it all so it will be a lot of opportunities to beat me.

@jreback jreback added the Visualization plotting label May 13, 2015
@TomAugspurger TomAugspurger added this to the 0.17.0 milestone May 13, 2015
@TomAugspurger
Copy link
Contributor

Yep I noticed that last night.

For people looking for a workaround, the index name is still used if it's set.

s.name = 'stuff'
s.plot()

@sinhrks sinhrks added the Regression Functionality that used to work in a prior pandas version label May 13, 2015
@jorisvandenbossche
Copy link
Member

Hmm, this is really an annoying regression. I use the following pattern rather commonly:

df1 = pd.DataFrame({'var':[1,2,3]})
df2 = pd.DataFrame({'var':[3,1,2]})

fig, ax = plt.subplots()
df1['var'].plot(ax=ax, label='df1')
df2['var'].plot(ax=ax, label='df2')
ax.legend()

which now gives a legend with two times 'var', and which is also not that easily to fix with the workaround of above

@TomAugspurger
Copy link
Contributor

Yes extremely annoying :/ I'll rebase my PR fixing it today.

@ispmarin
Copy link

👍 same problem with 0.16.1.

@goyodiaz
Copy link
Contributor Author

Sorry for spamming a closed bug but I thought a couple of suggestions could be useful.

@jorisvandenbossche, not very straightforward but you can do this:
pd.Series(df1['var'], name='df1').plot(ax=ax)

Or monkey patch Series.plot: copy this code into a module and import it at the beginning of yours:

# Monkey patch to work around pandas 0.16.1 issue #10119.
# https://github.com/pydata/pandas/issues/10119
import pandas as pd

if pd.__version__ == '0.16.1':
    _old_plot = pd.Series.plot
    def _plot(self, *args, label=None, **kwargs):
        return _old_plot(pd.Series(self, name=label), *args, **kwargs)
    pd.Series.plot = _plot

Or just patch your local pandas (but this won't help other people using your code).

@ispmarin
Copy link

Small fix (note the label declaration)

if pd.__version__ == '0.16.1':
    _old_plot = pd.Series.plot
    def _plot(self, label=None, *args,  **kwargs):
        return _old_plot(pd.Series(self, name=label), *args, **kwargs)
    pd.Series.plot = _plot

And I can't get it working in ipytho notebook.

@schmohlio
Copy link

@TomAugspurger @jorisvandenbossche

In #9574 I had removed:
label = self.kwds.pop('label', None)
and hadn't reintroduced it in _compute_plot_data. Was it added somewhere else? This was tied back to the initial issue and wan't to see if I introduced regression. Thanks,
Matt

@sinhrks
Copy link
Member

sinhrks commented Jun 1, 2015

_compute_plot_data is changed in #9852 (by me...) , and it moved the previous logic before you've fixed. We should have test cases to cover possible cases.

@TomAugspurger
Copy link
Contributor

I added tests in my PR so it should be tested now.

@jorisvandenbossche jorisvandenbossche modified the milestones: 0.17.0, 0.16.2 Jun 2, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version Visualization plotting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants