Skip to content

BUG: fix Series.plot label setting #10131

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

Merged
merged 1 commit into from
May 19, 2015
Merged
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
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,7 @@ Bug Fixes


- Bug in ``DatetimeIndex`` and ``TimedeltaIndex`` names are lost after timedelta arithmetics ( :issue:`9926`)

- Bug in `Series.plot(label="LABEL")` not correctly setting the label (:issue:`10119`)


23 changes: 23 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,29 @@ def test_ts_area_lim(self):
self.assertEqual(xmin, line[0])
self.assertEqual(xmax, line[-1])

def test_label(self):
s = Series([1, 2])
ax = s.plot(label='LABEL', legend=True)
self._check_legend_labels(ax, labels=['LABEL'])
self.plt.close()
ax = s.plot(legend=True)
self._check_legend_labels(ax, labels=['None'])
self.plt.close()
# get name from index
s.name = 'NAME'
ax = s.plot(legend=True)
self._check_legend_labels(ax, labels=['NAME'])
self.plt.close()
# override the default
ax = s.plot(legend=True, label='LABEL')
self._check_legend_labels(ax, labels=['LABEL'])
self.plt.close()
# Add lebel info, but don't draw
ax = s.plot(legend=False, label='LABEL')
self.assertEqual(ax.get_legend(), None) # Hasn't been drawn
ax.legend() # draw it
self._check_legend_labels(ax, labels=['LABEL'])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add here a test for the case where the series has a name, but you provide a label (so add ´s.plot(label='LABEL', legend=True)´ and then check the legend label is LABEL and not NAME)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Updated.

def test_line_area_nan_series(self):
values = [1, 2, np.nan, 3]
s = Series(values)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ def _compute_plot_data(self):
data = self.data

if isinstance(data, Series):
label = self.kwds.pop('label', None)
label = self.label
if label is None and data.name is None:
label = 'None'
data = data.to_frame(name=label)
Expand Down