Skip to content

Commit f00d6bb

Browse files
David BROCHARTTomAugspurger
David BROCHART
authored andcommitted
Fixed bug pandas-dev#9671 where 'DataFrame.plot()' raised an error when both 'color' and 'style' keywords were passed and there was no color symbol in the style strings (this should be allowed)
1 parent d8bc7a3 commit f00d6bb

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

doc/source/whatsnew/v0.16.1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,5 @@ Bug Fixes
8282
- Bug in ``Series.quantile`` on empty Series of type ``Datetime`` or ``Timedelta`` (:issue:`9675`)
8383
- Bug in ``where`` causing incorrect results when upcasting was required (:issue:`9731`)
8484
- Bug in ``FloatArrayFormatter`` where decision boundary for displaying "small" floats in decimal format is off by one order of magnitude for a given display.precision (:issue:`9764`)
85+
86+
- Fixed bug where ``DataFrame.plot()`` raised an error when both ``color`` and ``style`` keywords were passed and there was no color symbol in the style strings (:issue:`9671`)

pandas/tests/test_graphics.py

+16
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,22 @@ def test_plot(self):
11541154
self.assertEqual(len(axes), 1)
11551155
self.assertIs(ax.get_axes(), axes[0])
11561156

1157+
def test_color_and_style_arguments(self):
1158+
df = DataFrame({'x': [1, 2], 'y': [3, 4]})
1159+
# passing both 'color' and 'style' arguments should be allowed
1160+
# if there is no color symbol in the style strings:
1161+
ax = df.plot(color = ['red', 'black'], style = ['-', '--'])
1162+
# check that the linestyles are correctly set:
1163+
linestyle = [line.get_linestyle() for line in ax.lines]
1164+
self.assertEqual(linestyle, ['-', '--'])
1165+
# check that the colors are correctly set:
1166+
color = [line.get_color() for line in ax.lines]
1167+
self.assertEqual(color, ['red', 'black'])
1168+
# passing both 'color' and 'style' arguments should not be allowed
1169+
# if there is a color symbol in the style strings:
1170+
with tm.assertRaises(ValueError):
1171+
df.plot(color = ['red', 'black'], style = ['k-', 'r--'])
1172+
11571173
def test_nonnumeric_exclude(self):
11581174
df = DataFrame({'A': ["x", "y", "z"], 'B': [1, 2, 3]})
11591175
ax = df.plot()

pandas/tools/plotting.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -867,12 +867,17 @@ def _validate_color_args(self):
867867
"simultaneously. Using 'color'")
868868

869869
if 'color' in self.kwds and self.style is not None:
870+
if com.is_list_like(self.style):
871+
styles = self.style
872+
else:
873+
styles = [self.style]
870874
# need only a single match
871-
if re.match('^[a-z]+?', self.style) is not None:
872-
raise ValueError("Cannot pass 'style' string with a color "
873-
"symbol and 'color' keyword argument. Please"
874-
" use one or the other or pass 'style' "
875-
"without a color symbol")
875+
for s in styles:
876+
if re.match('^[a-z]+?', s) is not None:
877+
raise ValueError("Cannot pass 'style' string with a color "
878+
"symbol and 'color' keyword argument. Please"
879+
" use one or the other or pass 'style' "
880+
"without a color symbol")
876881

877882
def _iter_data(self, data=None, keep_index=False, fillna=None):
878883
if data is None:

0 commit comments

Comments
 (0)