-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
VIS: Allow 'C0'-like plotting for plotting colors #15516 #15873
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
Changes from 2 commits
4003d1c
e249722
dcb4cb0
3cb9e0b
9d15e7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,16 @@ def test_plot(self): | |
result = ax.get_axes() # deprecated | ||
self.assertIs(result, axes[0]) | ||
|
||
# GH 15516 | ||
def test_mpl2_color_cycle_str(self): | ||
# test C[0-9] as string | ||
colors = ['C' + str(x) for x in range(10)] | ||
df = DataFrame(randn(10, 3), columns=['a', 'b', 'c']) | ||
for c in colors: | ||
_check_plot_works(df.plot, color=c) | ||
with tm.assertRaises(ValueError): | ||
df.plot(color='') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put this in a separate test? (just give it another name, it is not really related to mpl2) |
||
|
||
def test_color_and_style_arguments(self): | ||
df = DataFrame({'x': [1, 2], 'y': [3, 4]}) | ||
# passing both 'color' and 'style' arguments should be allowed | ||
|
@@ -1600,6 +1610,10 @@ def test_line_colors(self): | |
self._check_colors(ax.get_lines(), linecolors=['red'] * 5) | ||
tm.close() | ||
|
||
ax = df.plot(color='C0') | ||
self._check_colors(ax.get_lines(), linecolors=['C0'] * 5) | ||
tm.close() | ||
|
||
# GH 10299 | ||
custom_colors = ['#FF0000', '#0000FF', '#FFFF00', '#000000', '#FFFFFF'] | ||
ax = df.plot(color=custom_colors) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,10 +217,18 @@ def _maybe_valid_colors(colors): | |
# check whether each character can be convertable to colors | ||
maybe_color_cycle = _maybe_valid_colors(list(colors)) | ||
if maybe_single_color and maybe_color_cycle and len(colors) > 1: | ||
msg = ("'{0}' can be parsed as both single color and " | ||
"color cycle. Specify each color using a list " | ||
"like ['{0}'] or {1}") | ||
raise ValueError(msg.format(colors, list(colors))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment here about what you are doing |
||
# Special case for single str 'CN' match and convert to hex | ||
# for supporting matplotlib < 2.0.0 | ||
if re.match(r'C[0-9]', colors) and len(colors) == 2: | ||
prop_cycler = plt.rcParams['axes.prop_cycle'] | ||
hex_color = prop_cycler.by_key().get('color', ['k']) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this prop_cycler is also only supported for matplotlib versions >= 1.5? See here how it is handled at another location: https://github.com/GuessWhoSamFoo/pandas/blob/e2497222ecf64e07f4bd7873a1b860021b0b605c/pandas/tools/plotting.py#L185-L190 |
||
colors = [hex_color[int(colors[1])]] | ||
else: | ||
# non-default cycle may be parsed as single or many colors | ||
msg = ("'{0}' can be parsed as both single color and " | ||
"color cycle. Specify each color using a list " | ||
"like ['{0}'] or {1}") | ||
raise ValueError(msg.format(colors, list(colors))) | ||
elif maybe_single_color: | ||
colors = [colors] | ||
else: | ||
|
@@ -229,7 +237,10 @@ def _maybe_valid_colors(colors): | |
pass | ||
|
||
if len(colors) != num_colors: | ||
multiple = num_colors // len(colors) - 1 | ||
try: | ||
multiple = num_colors // len(colors) - 1 | ||
except ZeroDivisionError: | ||
raise ValueError("Invalid color argument: ''") | ||
mod = num_colors % len(colors) | ||
|
||
colors += multiple * colors | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add the issue number as a comment