Skip to content

DataFrame.plot error when both 'color' and 'style' arguments are passed (GH9671) #9674

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pandas/tests/test_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,18 @@ def test_plot(self):
df = DataFrame({'x': [1, 2], 'y': [3, 4]})
with tm.assertRaises(TypeError):
df.plot(kind='line', blarg=True)
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of wrapping the code in try / except plots, just call the function.

ax = df.plot(color=['red', 'black'], style=['-', '--']) and then make sure the color and styles match what's expected.
You can get the actual linestyle with result = [line.get_linestyle() for line in ax.lines]
Then compare that to the expected with self.assertEqual(result, ['-', '--'])

Same thing for the colors, [line.get_color() for line in ax.lines]

df.plot(color = ['red', 'black'], style = ['-', '--'])
df['x'].plot(color = 'red', style = '-')
except:
self.fail("Calling 'plot()' on a dataframe/series and passing both 'color' and 'style' arguments should be allowed if there is no color symbol in the style string(s)")
try:
df.plot(color = ['red', 'black'], style = ['k-', 'r--'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should raise and exception, so you can use the tm.assertRaises context manager

with tm.assertRaises(ValueError):
     df.plot(color = ['red', 'black'], style = ['k-', 'r--'])

so that the test passes if a ValueError is raised, and fails if it doesn't.

df['x'].plot(color = 'red', style = 'k-')
except:
pass
else:
self.fail("Calling 'plot()' on a dataframe/series and passing both 'color' and 'style' arguments should raise an error if there is a color symbol in the style string(s)")

df = DataFrame(np.random.rand(10, 3),
index=list(string.ascii_letters[:10]))
Expand Down
15 changes: 10 additions & 5 deletions pandas/tools/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,12 +871,17 @@ def _validate_color_args(self):
"simultaneously. Using 'color'")

if 'color' in self.kwds and self.style is not None:
if com.is_list_like(self.style):
styles = self.style
else:
styles = [self.style]
# need only a single match
if re.match('^[a-z]+?', self.style) 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 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")

def _iter_data(self, data=None, keep_index=False, fillna=None):
if data is None:
Expand Down