Skip to content

Commit

Permalink
Make color validation more forgiving (pandas-dev#29122)
Browse files Browse the repository at this point in the history
  • Loading branch information
AllenDowney authored and TomAugspurger committed Nov 10, 2019
1 parent 9c25055 commit 0f15046
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ Plotting
- Bug in the ``xticks`` argument being ignored for :meth:`DataFrame.plot.bar` (:issue:`14119`)
- :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`)
- :meth:`DataFrame.plot` now allow a ``backend`` keyword arugment to allow changing between backends in one session (:issue:`28619`).
- Bug in color validation incorrectly raising for non-color styles (:issue:`29122`).

Groupby/resample/rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
17 changes: 10 additions & 7 deletions pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ def __init__(
self._validate_color_args()

def _validate_color_args(self):
import matplotlib.colors

if "color" not in self.kwds and "colors" in self.kwds:
warnings.warn(
(
Expand Down Expand Up @@ -234,13 +236,14 @@ def _validate_color_args(self):
styles = [self.style]
# need only a single match
for s in styles:
if re.match("^[a-z]+?", s) is not None:
raise ValueError(
"Cannot pass 'style' string with a color "
"symbol and 'color' keyword argument. Please"
" use one or the other or pass 'style' "
"without a color symbol"
)
for char in s:
if char in matplotlib.colors.BASE_COLORS:
raise ValueError(
"Cannot pass 'style' string with a color "
"symbol and 'color' keyword argument. Please"
" use one or the other or pass 'style' "
"without a color symbol"
)

def _iter_data(self, data=None, keep_index=False, fillna=None):
if data is None:
Expand Down
5 changes: 5 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -931,3 +931,8 @@ def test_plot_no_numeric_data(self):
df = pd.Series(["a", "b", "c"])
with pytest.raises(TypeError):
df.plot()

def test_style_single_ok(self):
s = pd.Series([1, 2])
ax = s.plot(style="s", color="C3")
assert ax.lines[0].get_color() == ["C3"]

0 comments on commit 0f15046

Please sign in to comment.