diff --git a/pandas/plotting/_matplotlib/compat.py b/pandas/plotting/_matplotlib/compat.py deleted file mode 100644 index 7314f05e9f19c..0000000000000 --- a/pandas/plotting/_matplotlib/compat.py +++ /dev/null @@ -1,15 +0,0 @@ -# being a bit too dynamic -from __future__ import annotations - -from pandas.util.version import Version - - -def _mpl_version(version, op): - def inner(): - try: - import matplotlib as mpl - except ImportError: - return False - return op(Version(mpl.__version__), Version(version)) - - return inner diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 455dd1f75b225..754cc94b6ded6 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -54,6 +54,7 @@ import pandas.core.common as com from pandas.core.frame import DataFrame +from pandas.util.version import Version from pandas.io.formats.printing import pprint_thing from pandas.plotting._matplotlib import tools @@ -784,8 +785,11 @@ def _make_legend(self) -> None: if not self.subplots: if leg is not None: title = leg.get_title().get_text() - # Replace leg.LegendHandles because it misses marker info - handles = leg.legendHandles + # Replace leg.legend_handles because it misses marker info + if Version(mpl.__version__) < Version("3.7"): + handles = leg.legendHandles + else: + handles = leg.legend_handles labels = [x.get_text() for x in leg.get_texts()] if self.legend: diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index 2d8b3b14c8c68..a2ab72ecb690e 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -13,6 +13,7 @@ TestPlotBase, _check_plot_works, ) +from pandas.util.version import Version @td.skip_if_no_mpl @@ -639,11 +640,18 @@ def test_rcParams_bar_colors(self): def test_colors_of_columns_with_same_name(self): # ISSUE 11136 -> https://github.com/pandas-dev/pandas/issues/11136 # Creating a DataFrame with duplicate column labels and testing colors of them. + import matplotlib as mpl + df = DataFrame({"b": [0, 1, 0], "a": [1, 2, 3]}) df1 = DataFrame({"a": [2, 4, 6]}) df_concat = pd.concat([df, df1], axis=1) result = df_concat.plot() - for legend, line in zip(result.get_legend().legendHandles, result.lines): + legend = result.get_legend() + if Version(mpl.__version__) < Version("3.7"): + handles = legend.legendHandles + else: + handles = legend.legend_handles + for legend, line in zip(handles, result.lines): assert legend.get_color() == line.get_color() def test_invalid_colormap(self): diff --git a/pandas/tests/plotting/frame/test_frame_legend.py b/pandas/tests/plotting/frame/test_frame_legend.py index d759d753d4320..bad42ebc85cc8 100644 --- a/pandas/tests/plotting/frame/test_frame_legend.py +++ b/pandas/tests/plotting/frame/test_frame_legend.py @@ -8,6 +8,7 @@ date_range, ) from pandas.tests.plotting.common import TestPlotBase +from pandas.util.version import Version class TestFrameLegend(TestPlotBase): @@ -19,6 +20,7 @@ class TestFrameLegend(TestPlotBase): ) def test_mixed_yerr(self): # https://github.com/pandas-dev/pandas/issues/39522 + import matplotlib as mpl from matplotlib.collections import LineCollection from matplotlib.lines import Line2D @@ -28,20 +30,29 @@ def test_mixed_yerr(self): df.plot("x", "b", c="blue", yerr=None, ax=ax, label="blue") legend = ax.get_legend() - result_handles = legend.legendHandles + if Version(mpl.__version__) < Version("3.7"): + result_handles = legend.legendHandles + else: + result_handles = legend.legend_handles assert isinstance(result_handles[0], LineCollection) assert isinstance(result_handles[1], Line2D) def test_legend_false(self): # https://github.com/pandas-dev/pandas/issues/40044 + import matplotlib as mpl + df = DataFrame({"a": [1, 1], "b": [2, 3]}) df2 = DataFrame({"d": [2.5, 2.5]}) ax = df.plot(legend=True, color={"a": "blue", "b": "green"}, secondary_y="b") df2.plot(legend=True, color={"d": "red"}, ax=ax) legend = ax.get_legend() - result = [handle.get_color() for handle in legend.legendHandles] + if Version(mpl.__version__) < Version("3.7"): + handles = legend.legendHandles + else: + handles = legend.legend_handles + result = [handle.get_color() for handle in handles] expected = ["blue", "green", "red"] assert result == expected