Skip to content

Commit

Permalink
BUG: Fix passing Colormap instance to plot methods with mpl >= 3.6 (p…
Browse files Browse the repository at this point in the history
…andas-dev#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
  • Loading branch information
zmoon authored and jbrockmendel committed Nov 1, 2022
1 parent 89366b8 commit 2614609
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.5.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ including other versions of pandas.
Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression in :meth:`Series.replace` raising ``RecursionError`` with numeric dtype and when specifying ``value=None`` (:issue:`45725`)
- Fixed regression in :meth:`DataFrame.plot` preventing :class:`~matplotlib.colors.Colormap` instance
from being passed using the ``colormap`` argument if Matplotlib 3.6+ is used (:issue:`49374`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/io/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -3610,7 +3610,7 @@ def _background_gradient(
if cmap is None:
rgbas = mpl.colormaps[mpl.rcParams["image.cmap"]](norm(gmap))
else:
rgbas = mpl.colormaps[cmap](norm(gmap))
rgbas = mpl.colormaps.get_cmap(cmap)(norm(gmap))
else:
rgbas = plt.cm.get_cmap(cmap)(norm(gmap))

Expand Down
4 changes: 2 additions & 2 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,7 @@ def _make_plot(self):

if self.colormap is not None:
if mpl_ge_3_6_0():
cmap = mpl.colormaps[self.colormap]
cmap = mpl.colormaps.get_cmap(self.colormap)
else:
cmap = self.plt.cm.get_cmap(self.colormap)
else:
Expand Down Expand Up @@ -1310,7 +1310,7 @@ def _make_plot(self) -> None:
# pandas uses colormap, matplotlib uses cmap.
cmap = self.colormap or "BuGn"
if mpl_ge_3_6_0():
cmap = mpl.colormaps[cmap]
cmap = mpl.colormaps.get_cmap(cmap)
else:
cmap = self.plt.cm.get_cmap(cmap)
cb = self.kwds.pop("colorbar", True)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/io/formats/style/test_matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,17 @@ def test_bar_color_raises(df):
msg = "`color` and `cmap` cannot both be given"
with pytest.raises(ValueError, match=msg):
df.style.bar(color="something", cmap="something else").to_html()


@pytest.mark.parametrize(
"plot_method",
["scatter", "hexbin"],
)
def test_pass_colormap_instance(df, plot_method):
# https://github.com/pandas-dev/pandas/issues/49374
cmap = mpl.colors.ListedColormap([[1, 1, 1], [0, 0, 0]])
df["c"] = df.A + df.B
kwargs = dict(x="A", y="B", c="c", colormap=cmap)
if plot_method == "hexbin":
kwargs["C"] = kwargs.pop("c")
getattr(df.plot, plot_method)(**kwargs)

0 comments on commit 2614609

Please sign in to comment.