From 98b8aa0dcef3a609575bb227b0e614d359451b73 Mon Sep 17 00:00:00 2001 From: MeeseeksMachine <39504233+meeseeksmachine@users.noreply.github.com> Date: Fri, 23 Sep 2022 22:05:35 +0200 Subject: [PATCH] Backport PR #48734 on branch 1.5.x (REGR: Raise on invalid colormap for scatter plot) (#48744) Backport PR #48734: REGR: Raise on invalid colormap for scatter plot Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com> --- doc/source/whatsnew/v1.5.1.rst | 1 + pandas/plotting/_matplotlib/core.py | 20 +++++++++++++------- pandas/tests/plotting/frame/test_frame.py | 6 ++++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v1.5.1.rst b/doc/source/whatsnew/v1.5.1.rst index 3f4265245bcb6..0f71814341aba 100644 --- a/doc/source/whatsnew/v1.5.1.rst +++ b/doc/source/whatsnew/v1.5.1.rst @@ -16,6 +16,7 @@ Fixed regressions ~~~~~~~~~~~~~~~~~ - Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`) - Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`) +- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`) - Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`) - diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index bacf19df06205..f93dbb4256323 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1221,16 +1221,22 @@ def _make_plot(self): else: c_values = c - # cmap is only used if c_values are integers, otherwise UserWarning - if is_integer_dtype(c_values): - # pandas uses colormap, matplotlib uses cmap. - cmap = self.colormap or "Greys" + if self.colormap is not None: if mpl_ge_3_6_0(): - cmap = mpl.colormaps[cmap] + cmap = mpl.colormaps[self.colormap] else: - cmap = self.plt.cm.get_cmap(cmap) + cmap = self.plt.cm.get_cmap(self.colormap) else: - cmap = None + # cmap is only used if c_values are integers, otherwise UserWarning + if is_integer_dtype(c_values): + # pandas uses colormap, matplotlib uses cmap. + cmap = "Greys" + if mpl_ge_3_6_0(): + cmap = mpl.colormaps[cmap] + else: + cmap = self.plt.cm.get_cmap(cmap) + else: + cmap = None if color_by_categorical: from matplotlib import colors diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 09d310e5ce060..dc70ef7e3520a 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -1680,6 +1680,12 @@ def _check_errorbar_color(containers, expected, has_err="has_xerr"): self._check_has_errorbars(ax, xerr=0, yerr=1) _check_errorbar_color(ax.containers, "green", has_err="has_yerr") + def test_scatter_unknown_colormap(self): + # GH#48726 + df = DataFrame({"a": [1, 2, 3], "b": 4}) + with pytest.raises((ValueError, KeyError), match="'unknown' is not a"): + df.plot(x="a", y="b", colormap="unknown", kind="scatter") + def test_sharex_and_ax(self): # https://github.com/pandas-dev/pandas/issues/9737 using gridspec, # the axis in fig.get_axis() are sorted differently than pandas