Skip to content

Commit e7302e0

Browse files
phoflnoatamir
authored andcommitted
REGR: Raise on invalid colormap for scatter plot (pandas-dev#48734)
* REGR: Raise on invalid colormap for scatter plot * Fix test
1 parent 3ca661b commit e7302e0

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
@@ -1231,16 +1231,22 @@ def _make_plot(self):
12311231
else:
12321232
c_values = c
12331233

1234-
# cmap is only used if c_values are integers, otherwise UserWarning
1235-
if is_integer_dtype(c_values):
1236-
# pandas uses colormap, matplotlib uses cmap.
1237-
cmap = self.colormap or "Greys"
1234+
if self.colormap is not None:
12381235
if mpl_ge_3_6_0():
1239-
cmap = mpl.colormaps[cmap]
1236+
cmap = mpl.colormaps[self.colormap]
12401237
else:
1241-
cmap = self.plt.cm.get_cmap(cmap)
1238+
cmap = self.plt.cm.get_cmap(self.colormap)
12421239
else:
1243-
cmap = None
1240+
# cmap is only used if c_values are integers, otherwise UserWarning
1241+
if is_integer_dtype(c_values):
1242+
# pandas uses colormap, matplotlib uses cmap.
1243+
cmap = "Greys"
1244+
if mpl_ge_3_6_0():
1245+
cmap = mpl.colormaps[cmap]
1246+
else:
1247+
cmap = self.plt.cm.get_cmap(cmap)
1248+
else:
1249+
cmap = None
12441250

12451251
if color_by_categorical:
12461252
from matplotlib import colors

pandas/tests/plotting/frame/test_frame.py

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

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

0 commit comments

Comments
 (0)