Skip to content

Commit 1e2cab9

Browse files
authored
Backport PR #49377 on branch 1.5.x (BUG: Fix passing Colormap instance to plot methods with mpl >= 3.6) (#49453)
Backport PR #49377: BUG: Fix passing `Colormap` instance to plot methods with mpl >= 3.6
1 parent e8b037e commit 1e2cab9

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

doc/source/whatsnew/v1.5.2.rst

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ including other versions of pandas.
1414
Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression in :meth:`Series.replace` raising ``RecursionError`` with numeric dtype and when specifying ``value=None`` (:issue:`45725`)
17+
- Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance
18+
from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`)
1719
-
1820

1921
.. ---------------------------------------------------------------------------

pandas/io/formats/style.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -3927,7 +3927,15 @@ def _background_gradient(
39273927
rng = smax - smin
39283928
# extend lower / upper bounds, compresses color range
39293929
norm = mpl.colors.Normalize(smin - (rng * low), smax + (rng * high))
3930-
rgbas = plt.cm.get_cmap(cmap)(norm(gmap))
3930+
from pandas.plotting._matplotlib.compat import mpl_ge_3_6_0
3931+
3932+
if mpl_ge_3_6_0():
3933+
if cmap is None:
3934+
rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap))
3935+
else:
3936+
rgbas = mpl.colormaps.get_cmap(cmap)(norm(gmap))
3937+
else:
3938+
rgbas = plt.cm.get_cmap(cmap)(norm(gmap))
39313939

39323940
def relative_luminance(rgba) -> float:
39333941
"""

pandas/plotting/_matplotlib/core.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ def _make_plot(self):
12221222

12231223
if self.colormap is not None:
12241224
if mpl_ge_3_6_0():
1225-
cmap = mpl.colormaps[self.colormap]
1225+
cmap = mpl.colormaps.get_cmap(self.colormap)
12261226
else:
12271227
cmap = self.plt.cm.get_cmap(self.colormap)
12281228
else:
@@ -1302,7 +1302,7 @@ def _make_plot(self):
13021302
# pandas uses colormap, matplotlib uses cmap.
13031303
cmap = self.colormap or "BuGn"
13041304
if mpl_ge_3_6_0():
1305-
cmap = mpl.colormaps[cmap]
1305+
cmap = mpl.colormaps.get_cmap(cmap)
13061306
else:
13071307
cmap = self.plt.cm.get_cmap(cmap)
13081308
cb = self.kwds.pop("colorbar", True)

pandas/tests/io/formats/style/test_matplotlib.py

+14
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,17 @@ def test_bar_color_raises(df):
284284
msg = "`color` and `cmap` cannot both be given"
285285
with pytest.raises(ValueError, match=msg):
286286
df.style.bar(color="something", cmap="something else").to_html()
287+
288+
289+
@pytest.mark.parametrize(
290+
"plot_method",
291+
["scatter", "hexbin"],
292+
)
293+
def test_pass_colormap_instance(df, plot_method):
294+
# https://github.com/pandas-dev/pandas/issues/49374
295+
cmap = mpl.colors.ListedColormap([[1, 1, 1], [0, 0, 0]])
296+
df["c"] = df.A + df.B
297+
kwargs = dict(x="A", y="B", c="c", colormap=cmap)
298+
if plot_method == "hexbin":
299+
kwargs["C"] = kwargs.pop("c")
300+
getattr(df.plot, plot_method)(**kwargs)

0 commit comments

Comments
 (0)