diff --git a/doc/source/whatsnew/v0.15.2.txt b/doc/source/whatsnew/v0.15.2.txt index 58dc1da214c05..9707cac00c05a 100644 --- a/doc/source/whatsnew/v0.15.2.txt +++ b/doc/source/whatsnew/v0.15.2.txt @@ -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: diff --git a/pandas/tools/plotting.py b/pandas/tools/plotting.py index b9a96ee262101..a2baad32e0c2d 100644 --- a/pandas/tools/plotting.py +++ b/pandas/tools/plotting.py @@ -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) @@ -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 diff --git a/pandas/tseries/tests/test_plotting.py b/pandas/tseries/tests/test_plotting.py index c4e642ffe43b0..ec8be9d66ae25 100644 --- a/pandas/tseries/tests/test_plotting.py +++ b/pandas/tseries/tests/test_plotting.py @@ -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