Skip to content

Commit 0f28ef7

Browse files
joooeeyKevin D Smith
authored and
Kevin D Smith
committed
BUG: repair 'style' kwd handling in DataFrame.plot (pandas-dev#21003) (pandas-dev#33821)
1 parent 52c0d12 commit 0f28ef7

File tree

4 files changed

+36
-13
lines changed

4 files changed

+36
-13
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ I/O
299299
Plotting
300300
^^^^^^^^
301301

302-
-
302+
- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes causes a ``ValueError`` (:issue:`21003`)
303303
-
304304

305305
Groupby/resample/rolling

pandas/plotting/_matplotlib/core.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import re
21
from typing import TYPE_CHECKING, List, Optional, Tuple
32
import warnings
43

@@ -55,6 +54,15 @@
5554
from matplotlib.axis import Axis
5655

5756

57+
def _color_in_style(style: str) -> bool:
58+
"""
59+
Check if there is a color letter in the style string.
60+
"""
61+
from matplotlib.colors import BASE_COLORS
62+
63+
return not set(BASE_COLORS).isdisjoint(style)
64+
65+
5866
class MPLPlot:
5967
"""
6068
Base class for assembling a pandas plot using matplotlib
@@ -200,8 +208,6 @@ def __init__(
200208
self._validate_color_args()
201209

202210
def _validate_color_args(self):
203-
import matplotlib.colors
204-
205211
if (
206212
"color" in self.kwds
207213
and self.nseries == 1
@@ -233,13 +239,12 @@ def _validate_color_args(self):
233239
styles = [self.style]
234240
# need only a single match
235241
for s in styles:
236-
for char in s:
237-
if char in matplotlib.colors.BASE_COLORS:
238-
raise ValueError(
239-
"Cannot pass 'style' string with a color symbol and "
240-
"'color' keyword argument. Please use one or the other or "
241-
"pass 'style' without a color symbol"
242-
)
242+
if _color_in_style(s):
243+
raise ValueError(
244+
"Cannot pass 'style' string with a color symbol and "
245+
"'color' keyword argument. Please use one or the "
246+
"other or pass 'style' without a color symbol"
247+
)
243248

244249
def _iter_data(self, data=None, keep_index=False, fillna=None):
245250
if data is None:
@@ -739,7 +744,7 @@ def _apply_style_colors(self, colors, kwds, col_num, label):
739744
style = self.style
740745

741746
has_color = "color" in kwds or self.colormap is not None
742-
nocolor_style = style is None or re.match("[a-z]+", style) is None
747+
nocolor_style = style is None or not _color_in_style(style)
743748
if (has_color or self.subplots) and nocolor_style:
744749
if isinstance(colors, dict):
745750
kwds["color"] = colors[label]

pandas/tests/plotting/test_frame.py

+18
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,24 @@ def test_color_and_style_arguments(self):
205205
with pytest.raises(ValueError):
206206
df.plot(color=["red", "black"], style=["k-", "r--"])
207207

208+
@pytest.mark.parametrize(
209+
"color, expected",
210+
[
211+
("green", ["green"] * 4),
212+
(["yellow", "red", "green", "blue"], ["yellow", "red", "green", "blue"]),
213+
],
214+
)
215+
def test_color_and_marker(self, color, expected):
216+
# GH 21003
217+
df = DataFrame(np.random.random((7, 4)))
218+
ax = df.plot(color=color, style="d--")
219+
# check colors
220+
result = [i.get_color() for i in ax.lines]
221+
assert result == expected
222+
# check markers and linestyles
223+
assert all(i.get_linestyle() == "--" for i in ax.lines)
224+
assert all(i.get_marker() == "d" for i in ax.lines)
225+
208226
def test_nonnumeric_exclude(self):
209227
df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]})
210228
ax = df.plot()

pandas/tests/plotting/test_series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ def test_plot_no_numeric_data(self):
958958
def test_style_single_ok(self):
959959
s = pd.Series([1, 2])
960960
ax = s.plot(style="s", color="C3")
961-
assert ax.lines[0].get_color() == ["C3"]
961+
assert ax.lines[0].get_color() == "C3"
962962

963963
@pytest.mark.parametrize(
964964
"index_name, old_label, new_label",

0 commit comments

Comments
 (0)