Skip to content

Commit

Permalink
TST: Address MPL 3.6 deprecation warnings (#48695)
Browse files Browse the repository at this point in the history
* TST: Address MPL 3.6 deprecation warnings

* Address min build

* missing ()
  • Loading branch information
mroeschke committed Sep 22, 2022
1 parent 36a67f6 commit cda0f6b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
14 changes: 12 additions & 2 deletions pandas/io/formats/style.py
Expand Up @@ -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:
"""
Expand Down Expand Up @@ -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
)
Expand Down
6 changes: 5 additions & 1 deletion pandas/tests/io/formats/style/test_matplotlib.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/plotting/common.py
Expand Up @@ -479,25 +479,29 @@ 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)
spndx += 1
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"):
"""
Expand Down

0 comments on commit cda0f6b

Please sign in to comment.