From a895774b64583fed017522c1725a13287900df52 Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 28 Oct 2022 15:53:37 -0600 Subject: [PATCH 1/4] Create failing test for passing Colormap inst to `.plot.scatter` --- pandas/tests/io/formats/style/test_matplotlib.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tests/io/formats/style/test_matplotlib.py b/pandas/tests/io/formats/style/test_matplotlib.py index c5b05b4e0d0c1..5afde41d7e65f 100644 --- a/pandas/tests/io/formats/style/test_matplotlib.py +++ b/pandas/tests/io/formats/style/test_matplotlib.py @@ -288,3 +288,10 @@ 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.xfail(mpl_ge_3_6_0(), reason="passing Colormap instance currently broken") +def test_pass_colormap_instance(df): + # https://github.com/pandas-dev/pandas/issues/49374 + cmap = mpl.colors.ListedColormap([[1, 1, 1], [0, 0, 0]]) + df.plot.scatter(x="A", y="B", colormap=cmap) From dabbdbbd96978ff4ea4d607181c2285e1788008f Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 28 Oct 2022 15:54:14 -0600 Subject: [PATCH 2/4] Use `mpl.colormaps.get_cmap` when a Colormap instance might be passed --- pandas/io/formats/style.py | 2 +- pandas/plotting/_matplotlib/core.py | 4 ++-- pandas/tests/io/formats/style/test_matplotlib.py | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index f3c58ac2ad18d..2ec012210bde9 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -3936,7 +3936,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)) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 9bcb51a7b032a..8292ffd0425f5 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1231,7 +1231,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: @@ -1311,7 +1311,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) diff --git a/pandas/tests/io/formats/style/test_matplotlib.py b/pandas/tests/io/formats/style/test_matplotlib.py index 5afde41d7e65f..e485b39ade671 100644 --- a/pandas/tests/io/formats/style/test_matplotlib.py +++ b/pandas/tests/io/formats/style/test_matplotlib.py @@ -290,8 +290,7 @@ def test_bar_color_raises(df): df.style.bar(color="something", cmap="something else").to_html() -@pytest.mark.xfail(mpl_ge_3_6_0(), reason="passing Colormap instance currently broken") def test_pass_colormap_instance(df): # https://github.com/pandas-dev/pandas/issues/49374 cmap = mpl.colors.ListedColormap([[1, 1, 1], [0, 0, 0]]) - df.plot.scatter(x="A", y="B", colormap=cmap) + df.plot.scatter(x="A", y="B", c=df.A + df.B, colormap=cmap) From bcbd071dfdb65523a71715880ab26413670412ad Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 28 Oct 2022 19:57:38 -0600 Subject: [PATCH 3/4] Test hexbin too --- pandas/tests/io/formats/style/test_matplotlib.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pandas/tests/io/formats/style/test_matplotlib.py b/pandas/tests/io/formats/style/test_matplotlib.py index e485b39ade671..f0c4152e3339b 100644 --- a/pandas/tests/io/formats/style/test_matplotlib.py +++ b/pandas/tests/io/formats/style/test_matplotlib.py @@ -290,7 +290,15 @@ def test_bar_color_raises(df): df.style.bar(color="something", cmap="something else").to_html() -def test_pass_colormap_instance(df): +@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.plot.scatter(x="A", y="B", c=df.A + df.B, colormap=cmap) + 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) From 8f7a852eafc8a2f6318de76277c97142e2537e16 Mon Sep 17 00:00:00 2001 From: zmoon Date: Fri, 28 Oct 2022 20:10:29 -0600 Subject: [PATCH 4/4] Add whatsnew --- doc/source/whatsnew/v1.5.2.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.2.rst b/doc/source/whatsnew/v1.5.2.rst index aaf00804262bb..fb0669b663b63 100644 --- a/doc/source/whatsnew/v1.5.2.rst +++ b/doc/source/whatsnew/v1.5.2.rst @@ -13,7 +13,8 @@ including other versions of pandas. Fixed regressions ~~~~~~~~~~~~~~~~~ -- +- 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`) - .. ---------------------------------------------------------------------------