From b7fa399e16cbf6a75f58067d82e12cba794bf17b Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 21 Sep 2022 14:17:53 -0700 Subject: [PATCH 1/3] TST: Address MPL 3.6 deprecation warnings --- pandas/io/formats/style.py | 7 +++++-- pandas/tests/io/formats/style/test_matplotlib.py | 2 +- pandas/tests/plotting/common.py | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index be8f0e22a5174..6536cee443d65 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3930,7 +3930,10 @@ def _background_gradient( rng = smax - smin # extend lower / upper bounds, compresses color range norm = mpl.colors.Normalize(smin - (rng * low), smax + (rng * high)) - rgbas = plt.cm.get_cmap(cmap)(norm(gmap)) + if cmap is None: + rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap)) + else: + rgbas = mpl.colormaps[cmap](norm(gmap)) def relative_luminance(rgba) -> float: """ @@ -4210,7 +4213,7 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple # use the matplotlib colormap input with _mpl(Styler.bar) as (plt, mpl): cmap = ( - mpl.cm.get_cmap(cmap) + mpl.colormaps[cmap] if isinstance(cmap, str) else cmap # assumed to be a Colormap instance as documented ) diff --git a/pandas/tests/io/formats/style/test_matplotlib.py b/pandas/tests/io/formats/style/test_matplotlib.py index 8d9f075d8674d..abf39b029bddc 100644 --- a/pandas/tests/io/formats/style/test_matplotlib.py +++ b/pandas/tests/io/formats/style/test_matplotlib.py @@ -260,7 +260,7 @@ def test_background_gradient_gmap_wrong_series(styler_blank): styler_blank.background_gradient(gmap=gmap, axis=None)._compute() -@pytest.mark.parametrize("cmap", ["PuBu", mpl.cm.get_cmap("PuBu")]) +@pytest.mark.parametrize("cmap", ["PuBu", mpl.colormaps["PuBu"]]) def test_bar_colormap(cmap): data = DataFrame([[1, 2], [3, 4]]) ctx = data.style.bar(cmap=cmap, axis=None)._compute().ctx diff --git a/pandas/tests/plotting/common.py b/pandas/tests/plotting/common.py index df853770b85f1..55a5473ce7d0f 100644 --- a/pandas/tests/plotting/common.py +++ b/pandas/tests/plotting/common.py @@ -479,12 +479,14 @@ def is_grid_on(): mpl.rc("axes", grid=False) obj.plot(kind=kind, **kws) assert not is_grid_on() + self.plt.clf() self.plt.subplot(1, 4 * len(kinds), spndx) spndx += 1 mpl.rc("axes", grid=True) obj.plot(kind=kind, grid=False, **kws) assert not is_grid_on() + self.plt.clf() if kind not in ["pie", "hexbin", "scatter"]: self.plt.subplot(1, 4 * len(kinds), spndx) @@ -492,12 +494,14 @@ def is_grid_on(): mpl.rc("axes", grid=True) obj.plot(kind=kind, **kws) assert is_grid_on() + self.plt.clf() self.plt.subplot(1, 4 * len(kinds), spndx) spndx += 1 mpl.rc("axes", grid=False) obj.plot(kind=kind, grid=True, **kws) assert is_grid_on() + self.plt.clf() def _unpack_cycler(self, rcParams, field="color"): """ From 201f06891d4df4880f5912ed66f3921d56e041e6 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 21 Sep 2022 16:39:19 -0700 Subject: [PATCH 2/3] Address min build --- pandas/io/formats/style.py | 15 +++++++++++---- pandas/tests/io/formats/style/test_matplotlib.py | 6 +++++- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 6536cee443d65..0d09c8895f2f6 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3930,10 +3930,15 @@ def _background_gradient( rng = smax - smin # extend lower / upper bounds, compresses color range norm = mpl.colors.Normalize(smin - (rng * low), smax + (rng * high)) - if cmap is None: - rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap)) + from pandas.plotting._matplotlib.compat import mpl_ge_3_6_0 + + if mpl_ge_3_6_0(): + if cmap is None: + rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap)) + else: + rgbas = mpl.colormaps[cmap](norm(gmap)) else: - rgbas = mpl.colormaps[cmap](norm(gmap)) + rgbas = plt.cm.get_cmap(cmap)(norm(gmap)) def relative_luminance(rgba) -> float: """ @@ -4212,8 +4217,10 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple if cmap is not None: # use the matplotlib colormap input with _mpl(Styler.bar) as (plt, mpl): + from pandas.plotting._matplotlib.compat import mpl_ge_3_6_0 + cmap = ( - mpl.colormaps[cmap] + (mpl.colormaps[cmap] if mpl_ge_3_6_0 else mpl.cm.get_cmap(cmap)) if isinstance(cmap, str) else cmap # assumed to be a Colormap instance as documented ) diff --git a/pandas/tests/io/formats/style/test_matplotlib.py b/pandas/tests/io/formats/style/test_matplotlib.py index abf39b029bddc..c5b05b4e0d0c1 100644 --- a/pandas/tests/io/formats/style/test_matplotlib.py +++ b/pandas/tests/io/formats/style/test_matplotlib.py @@ -13,6 +13,7 @@ import matplotlib as mpl from pandas.io.formats.style import Styler +from pandas.plotting._matplotlib.compat import mpl_ge_3_6_0 @pytest.fixture @@ -260,7 +261,10 @@ def test_background_gradient_gmap_wrong_series(styler_blank): styler_blank.background_gradient(gmap=gmap, axis=None)._compute() -@pytest.mark.parametrize("cmap", ["PuBu", mpl.colormaps["PuBu"]]) +@pytest.mark.parametrize( + "cmap", + ["PuBu", mpl.colormaps["PuBu"] if mpl_ge_3_6_0() else mpl.cm.get_cmap("PuBu")], +) def test_bar_colormap(cmap): data = DataFrame([[1, 2], [3, 4]]) ctx = data.style.bar(cmap=cmap, axis=None)._compute().ctx From 1ff67c4907edecd408bcf74e15c4dd05a5037ae1 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 22 Sep 2022 09:08:35 -0700 Subject: [PATCH 3/3] missing () --- pandas/io/formats/style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 0d09c8895f2f6..22f4f816a7a74 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -4220,7 +4220,7 @@ def css_calc(x, left: float, right: float, align: str, color: str | list | tuple from pandas.plotting._matplotlib.compat import mpl_ge_3_6_0 cmap = ( - (mpl.colormaps[cmap] if mpl_ge_3_6_0 else mpl.cm.get_cmap(cmap)) + (mpl.colormaps[cmap] if mpl_ge_3_6_0() else mpl.cm.get_cmap(cmap)) if isinstance(cmap, str) else cmap # assumed to be a Colormap instance as documented )