Skip to content

ENH: allow legend='reverse' in df.plot() GH6014 #6118

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
1 commit merged into from Feb 7, 2014
Merged
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
17 changes: 16 additions & 1 deletion pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,15 @@ def _post_plot_logic(self):
if self.legend:
for ax in self.axes:
ax.legend(loc='best')
leg = self.axes[0].get_legend()
if leg is not None:
lines = leg.get_lines()
labels = [x.get_text() for x in leg.get_texts()]

if self.legend == 'reverse':
lines = reversed(lines)
labels = reversed(labels)
ax.legend(lines, labels, loc='best', title=self.legend_title)

class ScatterPlot(MPLPlot):
def __init__(self, data, x, y, **kwargs):
Expand Down Expand Up @@ -1411,6 +1420,9 @@ def _make_legend(self, lines, labels):
ax.legend(ext_lines, ext_labels, loc='best',
title=self.legend_title)
elif self.legend:
if self.legend == 'reverse':
lines = reversed(lines)
labels = reversed(labels)
ax.legend(lines, labels, loc='best', title=self.legend_title)

def _get_ax_legend(self, ax):
Expand Down Expand Up @@ -1567,6 +1579,9 @@ def _make_plot(self):

if self.legend and not self.subplots:
patches = [r[0] for r in rects]
if self.legend == 'reverse':
patches = reversed(patches)
labels = reversed(labels)
self.axes[0].legend(patches, labels, loc='best',
title=self.legend_title)

Expand Down Expand Up @@ -1639,7 +1654,7 @@ def plot_frame(frame=None, x=None, y=None, subplots=False, sharex=True,
Title to use for the plot
grid : boolean, default None (matlab style default)
Axis grid lines
legend : boolean, default True
legend : False/True/'reverse'
Place legend on axis subplots

ax : matplotlib axis object, default None
Expand Down