From 06bb9fccd965c0f996c957b1dd7280fafe9c4830 Mon Sep 17 00:00:00 2001 From: Gianluca Ficarelli Date: Wed, 19 Jul 2023 11:33:05 +0200 Subject: [PATCH 1/2] Fix UserWarning in DataFrame.plot.scatter when invoked with c="b" --- doc/source/whatsnew/v2.1.0.rst | 1 + pandas/plotting/_matplotlib/core.py | 9 +++++---- pandas/tests/plotting/frame/test_frame_color.py | 6 ++++++ 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v2.1.0.rst b/doc/source/whatsnew/v2.1.0.rst index 839870cb18a0b..7024e790265ce 100644 --- a/doc/source/whatsnew/v2.1.0.rst +++ b/doc/source/whatsnew/v2.1.0.rst @@ -547,6 +547,7 @@ Period Plotting ^^^^^^^^ - Bug in :meth:`Series.plot` when invoked with ``color=None`` (:issue:`51953`) +- Fixed UserWarning in :meth:`DataFrame.plot.scatter` when invoked with ``c="b"`` (:issue:`53908`) - Groupby/resample/rolling diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index b2a2e1c59a175..c62f73271577d 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1235,11 +1235,12 @@ def _make_plot(self): if self.colormap is not None: cmap = mpl.colormaps.get_cmap(self.colormap) - # cmap is only used if c_values are integers, otherwise UserWarning - elif is_integer_dtype(c_values): + # cmap is only used if c_values are integers, otherwise UserWarning. + # GH-53908: additionally call isinstance() because is_integer_dtype + # returns True for "b" (meaning "blue" and not int8 in this context) + elif not isinstance(c_values, str) and is_integer_dtype(c_values): # pandas uses colormap, matplotlib uses cmap. - cmap = "Greys" - cmap = mpl.colormaps[cmap] + cmap = mpl.colormaps["Greys"] else: cmap = None diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index 3d041fdbb5de6..c435d8e28ab08 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -212,6 +212,12 @@ def test_scatter_colors(self): with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"): df.plot.scatter(x="a", y="b", c="c", color="green") + def test_scatter_colors_not_raising_warnings(self): + # GH-53908. Do not raise UserWarning: No data for colormapping + # provided via 'c'. Parameters 'cmap' will be ignored + df = DataFrame({"x": [1, 2, 3], "y": [1, 2, 3]}) + df.plot.scatter(x="x", y="y", c="b") + def test_scatter_colors_default(self): df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]}) default_colors = _unpack_cycler(mpl.pyplot.rcParams) From 80cc0d6773f67963a3d37beac4f0fe557edae2e5 Mon Sep 17 00:00:00 2001 From: Gianluca Ficarelli <26835404+GianlucaFicarelli@users.noreply.github.com> Date: Wed, 19 Jul 2023 18:27:39 +0200 Subject: [PATCH 2/2] Use tm.assert_produces_warning(None) Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/tests/plotting/frame/test_frame_color.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index c435d8e28ab08..7738aa1cb7900 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -216,7 +216,8 @@ def test_scatter_colors_not_raising_warnings(self): # GH-53908. Do not raise UserWarning: No data for colormapping # provided via 'c'. Parameters 'cmap' will be ignored df = DataFrame({"x": [1, 2, 3], "y": [1, 2, 3]}) - df.plot.scatter(x="x", y="y", c="b") + with tm.assert_produces_warning(None): + df.plot.scatter(x="x", y="y", c="b") def test_scatter_colors_default(self): df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})