Skip to content

Fix UserWarning in DataFrame.plot.scatter when invoked with c="b" #54189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/plotting/frame/test_frame_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down