Skip to content

Commit d0064af

Browse files
committed
remove extra legend handles
1 parent 64f0844 commit d0064af

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

doc/source/whatsnew/v1.3.0.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,9 @@ Plotting
694694

695695
- Bug in :func:`scatter_matrix` raising when 2d ``ax`` argument passed (:issue:`16253`)
696696
- Prevent warnings when matplotlib's ``constrained_layout`` is enabled (:issue:`25261`)
697-
-
697+
- Bug in :func:`DataFrame.plot` was showing the wrong colors in the legend if the function was called repeatedly and some calls used ``yerr`` while others didn't (:issue:`39522`)
698+
- Bug in :func:`DataFrame.plot` was showing the wrong colors in the legend if the function was called repeatedly and some calls used ``secondary_y`` and others use ``legend=False`` (:issue:`40044`)
699+
698700

699701
Groupby/resample/rolling
700702
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_matplotlib/core.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ def _append_legend_handles_labels(self, handle: Artist, label: str) -> None:
599599
self.legend_labels.append(label)
600600

601601
def _make_legend(self):
602-
ax, leg, handle = self._get_ax_legend_handle(self.axes[0])
602+
ax, leg = self._get_ax_legend(self.axes[0])
603603

604604
handles = []
605605
labels = []
@@ -609,7 +609,7 @@ def _make_legend(self):
609609
if leg is not None:
610610
title = leg.get_title().get_text()
611611
# Replace leg.LegendHandles because it misses marker info
612-
handles.extend(handle)
612+
handles = leg.legendHandles
613613
labels = [x.get_text() for x in leg.get_texts()]
614614

615615
if self.legend:
@@ -640,22 +640,20 @@ def _make_legend(self):
640640
if ax.get_visible():
641641
ax.legend(loc="best")
642642

643-
def _get_ax_legend_handle(self, ax: Axes):
643+
def _get_ax_legend(self, ax: Axes):
644644
"""
645-
Take in axes and return ax, legend and handle under different scenarios
645+
Take in axes and return ax and legend under different scenarios
646646
"""
647647
leg = ax.get_legend()
648648

649-
# Get handle from axes
650-
handle, _ = ax.get_legend_handles_labels()
651649
other_ax = getattr(ax, "left_ax", None) or getattr(ax, "right_ax", None)
652650
other_leg = None
653651
if other_ax is not None:
654652
other_leg = other_ax.get_legend()
655653
if leg is None and other_leg is not None:
656654
leg = other_leg
657655
ax = other_ax
658-
return ax, leg, handle
656+
return ax, leg
659657

660658
@cache_readonly
661659
def plt(self):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from matplotlib.collections import LineCollection
2+
from matplotlib.lines import Line2D
3+
4+
from pandas import DataFrame
5+
6+
7+
def test_mixed_yerr():
8+
# https://github.com/pandas-dev/pandas/issues/39522
9+
df = DataFrame([{"x": 1, "a": 1, "b": 1}, {"x": 2, "a": 2, "b": 3}])
10+
11+
ax = df.plot("x", "a", c="orange", yerr=0.1, label="orange")
12+
df.plot("x", "b", c="blue", yerr=None, ax=ax, label="blue")
13+
14+
legend = ax.get_legend()
15+
result_handles = legend.legendHandles
16+
17+
assert isinstance(result_handles[0], LineCollection)
18+
assert isinstance(result_handles[1], Line2D)
19+
20+
21+
def test_legend_false():
22+
# https://github.com/pandas-dev/pandas/issues/40044
23+
df = DataFrame({"a": [1, 1], "b": [2, 3]})
24+
df2 = DataFrame({"d": [2.5, 2.5]})
25+
26+
ax = df.plot(legend=True, color={"a": "blue", "b": "green"}, secondary_y="b")
27+
df2.plot(legend=True, color={"d": "red"}, ax=ax)
28+
legend = ax.get_legend()
29+
result = [handle.get_color() for handle in legend.legendHandles]
30+
expected = ["blue", "green", "red"]
31+
assert result == expected

0 commit comments

Comments
 (0)