Skip to content

Commit 7326eee

Browse files
AllenDowneyproost
authored andcommitted
Make color validation more forgiving (pandas-dev#29122)
1 parent 0e6a1a3 commit 7326eee

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ Plotting
414414
- Bug in the ``xticks`` argument being ignored for :meth:`DataFrame.plot.bar` (:issue:`14119`)
415415
- :func:`set_option` now validates that the plot backend provided to ``'plotting.backend'`` implements the backend when the option is set, rather than when a plot is created (:issue:`28163`)
416416
- :meth:`DataFrame.plot` now allow a ``backend`` keyword arugment to allow changing between backends in one session (:issue:`28619`).
417+
- Bug in color validation incorrectly raising for non-color styles (:issue:`29122`).
417418

418419
Groupby/resample/rolling
419420
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_matplotlib/core.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def __init__(
193193
self._validate_color_args()
194194

195195
def _validate_color_args(self):
196+
import matplotlib.colors
197+
196198
if "color" not in self.kwds and "colors" in self.kwds:
197199
warnings.warn(
198200
(
@@ -234,13 +236,14 @@ def _validate_color_args(self):
234236
styles = [self.style]
235237
# need only a single match
236238
for s in styles:
237-
if re.match("^[a-z]+?", s) is not None:
238-
raise ValueError(
239-
"Cannot pass 'style' string with a color "
240-
"symbol and 'color' keyword argument. Please"
241-
" use one or the other or pass 'style' "
242-
"without a color symbol"
243-
)
239+
for char in s:
240+
if char in matplotlib.colors.BASE_COLORS:
241+
raise ValueError(
242+
"Cannot pass 'style' string with a color "
243+
"symbol and 'color' keyword argument. Please"
244+
" use one or the other or pass 'style' "
245+
"without a color symbol"
246+
)
244247

245248
def _iter_data(self, data=None, keep_index=False, fillna=None):
246249
if data is None:

pandas/tests/plotting/test_series.py

+5
Original file line numberDiff line numberDiff line change
@@ -931,3 +931,8 @@ def test_plot_no_numeric_data(self):
931931
df = pd.Series(["a", "b", "c"])
932932
with pytest.raises(TypeError):
933933
df.plot()
934+
935+
def test_style_single_ok(self):
936+
s = pd.Series([1, 2])
937+
ax = s.plot(style="s", color="C3")
938+
assert ax.lines[0].get_color() == ["C3"]

0 commit comments

Comments
 (0)