Skip to content

Commit 9be48ef

Browse files
Fix UserWarning in DataFrame.plot.scatter when invoked with c="b" (#54189)
* Fix UserWarning in DataFrame.plot.scatter when invoked with c="b" * Use tm.assert_produces_warning(None) Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 19618ba commit 9be48ef

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ Period
549549
Plotting
550550
^^^^^^^^
551551
- Bug in :meth:`Series.plot` when invoked with ``color=None`` (:issue:`51953`)
552+
- Fixed UserWarning in :meth:`DataFrame.plot.scatter` when invoked with ``c="b"`` (:issue:`53908`)
552553
-
553554

554555
Groupby/resample/rolling

pandas/plotting/_matplotlib/core.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1235,11 +1235,12 @@ def _make_plot(self):
12351235

12361236
if self.colormap is not None:
12371237
cmap = mpl.colormaps.get_cmap(self.colormap)
1238-
# cmap is only used if c_values are integers, otherwise UserWarning
1239-
elif is_integer_dtype(c_values):
1238+
# cmap is only used if c_values are integers, otherwise UserWarning.
1239+
# GH-53908: additionally call isinstance() because is_integer_dtype
1240+
# returns True for "b" (meaning "blue" and not int8 in this context)
1241+
elif not isinstance(c_values, str) and is_integer_dtype(c_values):
12401242
# pandas uses colormap, matplotlib uses cmap.
1241-
cmap = "Greys"
1242-
cmap = mpl.colormaps[cmap]
1243+
cmap = mpl.colormaps["Greys"]
12431244
else:
12441245
cmap = None
12451246

pandas/tests/plotting/frame/test_frame_color.py

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ def test_scatter_colors(self):
212212
with pytest.raises(TypeError, match="Specify exactly one of `c` and `color`"):
213213
df.plot.scatter(x="a", y="b", c="c", color="green")
214214

215+
def test_scatter_colors_not_raising_warnings(self):
216+
# GH-53908. Do not raise UserWarning: No data for colormapping
217+
# provided via 'c'. Parameters 'cmap' will be ignored
218+
df = DataFrame({"x": [1, 2, 3], "y": [1, 2, 3]})
219+
with tm.assert_produces_warning(None):
220+
df.plot.scatter(x="x", y="y", c="b")
221+
215222
def test_scatter_colors_default(self):
216223
df = DataFrame({"a": [1, 2, 3], "b": [1, 2, 3], "c": [1, 2, 3]})
217224
default_colors = _unpack_cycler(mpl.pyplot.rcParams)

0 commit comments

Comments
 (0)