-
-
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
Merged
TomAugspurger
merged 5 commits into
pandas-dev:master
from
GuessWhoSamFoo:str_color_cycle
Apr 12, 2017
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4003d1c
VIS: Allow 'C0'-like plotting for plotting colors
GuessWhoSamFoo e249722
Added case color='' and support for mpl < 2.0
GuessWhoSamFoo dcb4cb0
Merge branch 'master' into str_color_cycle
GuessWhoSamFoo 3cb9e0b
Updated prop_cycle references to be compatible with matplotlib 1.5 an…
GuessWhoSamFoo 9d15e7d
Separated test; Used more consise regex
GuessWhoSamFoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,6 +141,21 @@ def test_plot(self): | |
result = ax.get_axes() # deprecated | ||
self.assertIs(result, axes[0]) | ||
|
||
# GH 15516 | ||
def test_mpl2_color_cycle_str(self): | ||
# test CN mpl 2.0 color cycle | ||
import matplotlib.pyplot as plt | ||
# mpl 1.5 color cycle is 'bgrcmyk' and | ||
# tests up to 'C6' for compatibility | ||
colors = ['C' + str(x) for x in range(7)] | ||
hex_color = [c['color'] | ||
for c in list(plt.rcParams['axes.prop_cycle'])] | ||
df = DataFrame(randn(10, 3), columns=['a', 'b', 'c']) | ||
for c in colors: | ||
_check_plot_works(df.plot, color=hex_color[int(c[1])]) | ||
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 +1615,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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,10 +225,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: | ||
hex_color = [c['color'] | ||
for c in list(plt.rcParams['axes.prop_cycle'])] | ||
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: | ||
|
@@ -237,7 +245,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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You should test here with the actual 'C0', .. instead of with the converted to hex colors ?