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..7738aa1cb7900 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -212,6 +212,13 @@ 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]}) + 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]}) default_colors = _unpack_cycler(mpl.pyplot.rcParams)