Skip to content

Commit 8401799

Browse files
committed
version gate legend_handles
1 parent 3e9b539 commit 8401799

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

pandas/plotting/_matplotlib/compat.py

-15
This file was deleted.

pandas/plotting/_matplotlib/core.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
import pandas.core.common as com
5656
from pandas.core.frame import DataFrame
57+
from pandas.util.version import Version
5758

5859
from pandas.io.formats.printing import pprint_thing
5960
from pandas.plotting._matplotlib import tools
@@ -785,7 +786,10 @@ def _make_legend(self) -> None:
785786
if leg is not None:
786787
title = leg.get_title().get_text()
787788
# Replace leg.legend_handles because it misses marker info
788-
handles = leg.legend_handles
789+
if Version(mpl.__version__) < Version("3.7"):
790+
handles = leg.legendHandles
791+
else:
792+
handles = leg.legend_handles
789793
labels = [x.get_text() for x in leg.get_texts()]
790794

791795
if self.legend:

pandas/tests/plotting/frame/test_frame_color.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
TestPlotBase,
1414
_check_plot_works,
1515
)
16+
from pandas.util.version import Version
1617

1718

1819
@td.skip_if_no_mpl
@@ -639,11 +640,18 @@ def test_rcParams_bar_colors(self):
639640
def test_colors_of_columns_with_same_name(self):
640641
# ISSUE 11136 -> https://github.com/pandas-dev/pandas/issues/11136
641642
# Creating a DataFrame with duplicate column labels and testing colors of them.
643+
import matplotlib as mpl
644+
642645
df = DataFrame({"b": [0, 1, 0], "a": [1, 2, 3]})
643646
df1 = DataFrame({"a": [2, 4, 6]})
644647
df_concat = pd.concat([df, df1], axis=1)
645648
result = df_concat.plot()
646-
for legend, line in zip(result.get_legend().legend_handles, result.lines):
649+
legend = result.get_legend()
650+
if Version(mpl.__version__) < Version("3.7"):
651+
handles = legend.legendHandles
652+
else:
653+
handles = legend.legend_handles
654+
for legend, line in zip(handles, result.lines):
647655
assert legend.get_color() == line.get_color()
648656

649657
def test_invalid_colormap(self):

pandas/tests/plotting/frame/test_frame_legend.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
date_range,
99
)
1010
from pandas.tests.plotting.common import TestPlotBase
11+
from pandas.util.version import Version
1112

1213

1314
class TestFrameLegend(TestPlotBase):
@@ -19,6 +20,7 @@ class TestFrameLegend(TestPlotBase):
1920
)
2021
def test_mixed_yerr(self):
2122
# https://github.com/pandas-dev/pandas/issues/39522
23+
import matplotlib as mpl
2224
from matplotlib.collections import LineCollection
2325
from matplotlib.lines import Line2D
2426

@@ -28,20 +30,29 @@ def test_mixed_yerr(self):
2830
df.plot("x", "b", c="blue", yerr=None, ax=ax, label="blue")
2931

3032
legend = ax.get_legend()
31-
result_handles = legend.legend_handles
33+
if Version(mpl.__version__) < Version("3.7"):
34+
result_handles = legend.legendHandles
35+
else:
36+
result_handles = legend.legend_handles
3237

3338
assert isinstance(result_handles[0], LineCollection)
3439
assert isinstance(result_handles[1], Line2D)
3540

3641
def test_legend_false(self):
3742
# https://github.com/pandas-dev/pandas/issues/40044
43+
import matplotlib as mpl
44+
3845
df = DataFrame({"a": [1, 1], "b": [2, 3]})
3946
df2 = DataFrame({"d": [2.5, 2.5]})
4047

4148
ax = df.plot(legend=True, color={"a": "blue", "b": "green"}, secondary_y="b")
4249
df2.plot(legend=True, color={"d": "red"}, ax=ax)
4350
legend = ax.get_legend()
44-
result = [handle.get_color() for handle in legend.legend_handles]
51+
if Version(mpl.__version__) < Version("3.7"):
52+
handles = legend.legendHandles
53+
else:
54+
handles = legend.legend_handles
55+
result = [handle.get_color() for handle in handles]
4556
expected = ["blue", "green", "red"]
4657
assert result == expected
4758

0 commit comments

Comments
 (0)