Skip to content

Commit 75cf35c

Browse files
author
y-p
committed
Merge pull request pandas-dev#6118 from y-p/PR_reverse_legend
ENH: allow legend='reverse' in df.plot() GH6014
2 parents 1e9c0a6 + 2389121 commit 75cf35c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pandas/tools/plotting.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,15 @@ def _post_plot_logic(self):
12271227
if self.legend:
12281228
for ax in self.axes:
12291229
ax.legend(loc='best')
1230+
leg = self.axes[0].get_legend()
1231+
if leg is not None:
1232+
lines = leg.get_lines()
1233+
labels = [x.get_text() for x in leg.get_texts()]
1234+
1235+
if self.legend == 'reverse':
1236+
lines = reversed(lines)
1237+
labels = reversed(labels)
1238+
ax.legend(lines, labels, loc='best', title=self.legend_title)
12301239

12311240
class ScatterPlot(MPLPlot):
12321241
def __init__(self, data, x, y, **kwargs):
@@ -1411,6 +1420,9 @@ def _make_legend(self, lines, labels):
14111420
ax.legend(ext_lines, ext_labels, loc='best',
14121421
title=self.legend_title)
14131422
elif self.legend:
1423+
if self.legend == 'reverse':
1424+
lines = reversed(lines)
1425+
labels = reversed(labels)
14141426
ax.legend(lines, labels, loc='best', title=self.legend_title)
14151427

14161428
def _get_ax_legend(self, ax):
@@ -1567,6 +1579,9 @@ def _make_plot(self):
15671579

15681580
if self.legend and not self.subplots:
15691581
patches = [r[0] for r in rects]
1582+
if self.legend == 'reverse':
1583+
patches = reversed(patches)
1584+
labels = reversed(labels)
15701585
self.axes[0].legend(patches, labels, loc='best',
15711586
title=self.legend_title)
15721587

@@ -1642,7 +1657,7 @@ def plot_frame(frame=None, x=None, y=None, subplots=False, sharex=True,
16421657
Title to use for the plot
16431658
grid : boolean, default None (matlab style default)
16441659
Axis grid lines
1645-
legend : boolean, default True
1660+
legend : False/True/'reverse'
16461661
Place legend on axis subplots
16471662
16481663
ax : matplotlib axis object, default None

0 commit comments

Comments
 (0)