Skip to content

Commit

Permalink
Backport PR #51470 on branch 2.0.x (DEPS: Partial Matplotlib 3.7 supp…
Browse files Browse the repository at this point in the history
…ort) (#51510)

Backport PR #51470: DEPS: Partial Matplotlib 3.7 support

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 21, 2023
1 parent 8773f4a commit fbb9b34
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
15 changes: 0 additions & 15 deletions pandas/plotting/_matplotlib/compat.py

This file was deleted.

8 changes: 6 additions & 2 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion pandas/tests/plotting/frame/test_frame_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
TestPlotBase,
_check_plot_works,
)
from pandas.util.version import Version


@td.skip_if_no_mpl
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 13 additions & 2 deletions pandas/tests/plotting/frame/test_frame_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
date_range,
)
from pandas.tests.plotting.common import TestPlotBase
from pandas.util.version import Version


class TestFrameLegend(TestPlotBase):
Expand All @@ -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

Expand All @@ -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

Expand Down

0 comments on commit fbb9b34

Please sign in to comment.