Skip to content

Commit c4a24d2

Browse files
authored
BUG: Fix passing Colormap instance to plot methods with mpl >= 3.6 (#49377)
* Create failing test for passing Colormap inst to `.plot.scatter` * Use `mpl.colormaps.get_cmap` when a Colormap instance might be passed * Test hexbin too * Add whatsnew
1 parent 6122c7d commit c4a24d2

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3610,7 +3610,7 @@ def _background_gradient(
36103610
if cmap is None:
36113611
rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap))
36123612
else:
3613-
rgbas = mpl.colormaps[cmap](norm(gmap))
3613+
rgbas = mpl.colormaps.get_cmap(cmap)(norm(gmap))
36143614
else:
36153615
rgbas = plt.cm.get_cmap(cmap)(norm(gmap))
36163616

pandas/plotting/_matplotlib/core.py

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

12311231
if self.colormap is not None:
12321232
if mpl_ge_3_6_0():
1233-
cmap = mpl.colormaps[self.colormap]
1233+
cmap = mpl.colormaps.get_cmap(self.colormap)
12341234
else:
12351235
cmap = self.plt.cm.get_cmap(self.colormap)
12361236
else:
@@ -1310,7 +1310,7 @@ def _make_plot(self) -> None:
13101310
# pandas uses colormap, matplotlib uses cmap.
13111311
cmap = self.colormap or "BuGn"
13121312
if mpl_ge_3_6_0():
1313-
cmap = mpl.colormaps[cmap]
1313+
cmap = mpl.colormaps.get_cmap(cmap)
13141314
else:
13151315
cmap = self.plt.cm.get_cmap(cmap)
13161316
cb = self.kwds.pop("colorbar", True)

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

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

0 commit comments

Comments
 (0)