Skip to content

BUG: Fixed plot label shows as None. #8905 #8945

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.15.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Bug Fixes
- Bug in ``merge`` where ``how='left'`` and ``sort=False`` would not preserve left frame order (:issue:`7331`)

- Fix negative step support for label-based slices (:issue:`8753`)
- Fixed given label showing as None (:issue:`#8905`)

Old behavior:

Expand Down
6 changes: 4 additions & 2 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,9 @@ def _make_plot(self):
errors = self._get_errorbars(label=label, index=i)
kwds = dict(kwds, **errors)

label = com.pprint_thing(label) # .encode('utf-8')
if label is not None or self.legend:
label = com.pprint_thing(label) # .encode('utf-8')

kwds['label'] = label

newlines = plotf(ax, x, y, style=style, column_num=i, **kwds)
Expand Down Expand Up @@ -2283,7 +2285,7 @@ def _plot(data, x=None, y=None, subplots=False,
if com.is_integer(y) and not data.columns.holds_integer():
y = data.columns[y]
label = x if x is not None else data.index.name
label = kwds.pop('label', label)
label = kwds.get('label', label)
series = data[y].copy() # Don't modify
series.index.name = label

Expand Down
14 changes: 14 additions & 0 deletions pandas/tseries/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ def test_nonnumeric_exclude(self):

self.assertRaises(TypeError, df['A'].plot)

@slow
def test_legend_added(self):
df = DataFrame(np.random.rand(2, 4), columns=['a', 'b', 'c', 'd'])
a_plot = df.plot(kind='scatter', x='a', y='b',
color='DarkBlue', label='Label')
b_plot = df.plot(kind='line', x='a', y='b',
color='DarkBlue', label="foo")
c_plot = df.plot(kind='line', x='a', y='b',
color='DarkBlue', label=None, legend=False)

self.assertEqual(a_plot.legend().texts[0].get_text(), 'Label')
self.assertEqual(b_plot.legend().texts[0].get_text(), 'foo')
self.assertEqual(c_plot.legend(), None)

@slow
def test_tsplot(self):
from pandas.tseries.plotting import tsplot
Expand Down