Skip to content

Commit 98b8aa0

Browse files
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 <[email protected]>
1 parent a10837b commit 98b8aa0

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

doc/source/whatsnew/v1.5.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed Regression in :meth:`DataFrame.loc` when setting values as a :class:`DataFrame` with all ``True`` indexer (:issue:`48701`)
1818
- Regression in :func:`.read_csv` causing an ``EmptyDataError`` when using an UTF-8 file handle that was already read from (:issue:`48646`)
19+
- Fixed regression in :meth:`DataFrame.plot` ignoring invalid ``colormap`` for ``kind="scatter"`` (:issue:`48726`)
1920
- Fixed performance regression in :func:`factorize` when ``na_sentinel`` is not ``None`` and ``sort=False`` (:issue:`48620`)
2021
-
2122

pandas/plotting/_matplotlib/core.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1221,16 +1221,22 @@ def _make_plot(self):
12211221
else:
12221222
c_values = c
12231223

1224-
# cmap is only used if c_values are integers, otherwise UserWarning
1225-
if is_integer_dtype(c_values):
1226-
# pandas uses colormap, matplotlib uses cmap.
1227-
cmap = self.colormap or "Greys"
1224+
if self.colormap is not None:
12281225
if mpl_ge_3_6_0():
1229-
cmap = mpl.colormaps[cmap]
1226+
cmap = mpl.colormaps[self.colormap]
12301227
else:
1231-
cmap = self.plt.cm.get_cmap(cmap)
1228+
cmap = self.plt.cm.get_cmap(self.colormap)
12321229
else:
1233-
cmap = None
1230+
# cmap is only used if c_values are integers, otherwise UserWarning
1231+
if is_integer_dtype(c_values):
1232+
# pandas uses colormap, matplotlib uses cmap.
1233+
cmap = "Greys"
1234+
if mpl_ge_3_6_0():
1235+
cmap = mpl.colormaps[cmap]
1236+
else:
1237+
cmap = self.plt.cm.get_cmap(cmap)
1238+
else:
1239+
cmap = None
12341240

12351241
if color_by_categorical:
12361242
from matplotlib import colors

pandas/tests/plotting/frame/test_frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,12 @@ def _check_errorbar_color(containers, expected, has_err="has_xerr"):
16801680
self._check_has_errorbars(ax, xerr=0, yerr=1)
16811681
_check_errorbar_color(ax.containers, "green", has_err="has_yerr")
16821682

1683+
def test_scatter_unknown_colormap(self):
1684+
# GH#48726
1685+
df = DataFrame({"a": [1, 2, 3], "b": 4})
1686+
with pytest.raises((ValueError, KeyError), match="'unknown' is not a"):
1687+
df.plot(x="a", y="b", colormap="unknown", kind="scatter")
1688+
16831689
def test_sharex_and_ax(self):
16841690
# https://github.com/pandas-dev/pandas/issues/9737 using gridspec,
16851691
# the axis in fig.get_axis() are sorted differently than pandas

0 commit comments

Comments
 (0)