From cda0f6bee2a6520729247f92eef9f722ad2487ec Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 22 Sep 2022 13:00:33 -0700 Subject: [PATCH] TST: Address MPL 3.6 deprecation warnings (#48695) * TST: Address MPL 3.6 deprecation warnings * Address min build * missing () --- pandas/io/formats/style.py | 14 ++++++++++++-- pandas/tests/io/formats/style/test_matplotlib.py | 6 +++++- pandas/tests/plotting/common.py | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 0d8c970c414f0..c03cbf2ac60c3 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3934,7 +3934,15 @@ 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)) + 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 = plt.cm.get_cmap(cmap)(norm(gmap)) def relative_luminance(rgba) -> float: """ @@ -4213,8 +4221,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.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 ) diff --git a/pandas/tests/io/formats/style/test_matplotlib.py b/pandas/tests/io/formats/style/test_matplotlib.py index 8d9f075d8674d..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.cm.get_cmap("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 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"): """