Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Address MPL 3.6 deprecation warnings #48695

Merged
merged 4 commits into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions pandas/io/formats/style.py
Expand Up @@ -3930,7 +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))
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 @@ -4209,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.cm.get_cmap(cmap)
(mpl.colormaps[cmap] if mpl_ge_3_6_0 else mpl.cm.get_cmap(cmap))
mroeschke marked this conversation as resolved.
Show resolved Hide resolved
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