-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: fix matplotlib warning on CN color #36981
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 7 commits
e163302
ff57346
29ea95e
a9d125d
d489a10
aa14f33
749d63a
25c0df0
58cebb2
b4937b6
f003217
4f1178d
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 |
---|---|---|
|
@@ -57,28 +57,8 @@ def random_color(column): | |
raise ValueError("color_type must be either 'default' or 'random'") | ||
|
||
if isinstance(colors, str): | ||
conv = matplotlib.colors.ColorConverter() | ||
|
||
def _maybe_valid_colors(colors): | ||
try: | ||
[conv.to_rgba(c) for c in colors] | ||
return True | ||
except ValueError: | ||
return False | ||
|
||
# check whether the string can be convertible to single color | ||
maybe_single_color = _maybe_valid_colors([colors]) | ||
# check whether each character can be convertible to colors | ||
maybe_color_cycle = _maybe_valid_colors(list(colors)) | ||
if maybe_single_color and maybe_color_cycle and len(colors) > 1: | ||
hex_color = [c["color"] for c in list(plt.rcParams["axes.prop_cycle"])] | ||
colors = [hex_color[int(colors[1])]] | ||
elif maybe_single_color: | ||
if _is_single_color(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. maybe put it in one line? `if isinstance(colors, str) and _is_single_color(colors)` also nice to put an issue number below this line for future reference! 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. Done |
||
colors = [colors] | ||
else: | ||
# ``colors`` is regarded as color cycle. | ||
# mpl will raise error any of them is invalid | ||
pass | ||
|
||
# Append more colors by cycling if there is not enough color. | ||
# Extra colors will be ignored by matplotlib if there are more colors | ||
|
@@ -94,3 +74,41 @@ def _maybe_valid_colors(colors): | |
colors += colors[:mod] | ||
|
||
return colors | ||
|
||
|
||
def _is_cn_color(color: str) -> bool: | ||
"""Check if color string is CN color, like 'C0', 'C1', etc.""" | ||
cn_colors = ["C" + str(x) for x in range(10)] | ||
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. The default matplotlib color cycle has 10 colors, but if set differently this test won't be valid. |
||
return bool(color in cn_colors) | ||
|
||
|
||
def _is_single_color(color: str) -> bool: | ||
"""Check if ``color`` is a single color. | ||
|
||
Examples of single colors: | ||
- 'r' | ||
- 'g' | ||
- 'red' | ||
- 'green' | ||
- 'C3' | ||
|
||
Parameters | ||
---------- | ||
color : string | ||
Color string. | ||
|
||
Returns | ||
------- | ||
bool | ||
True if ``color`` looks like a valid color. | ||
False otherwise. | ||
""" | ||
conv = matplotlib.colors.ColorConverter() | ||
if _is_cn_color(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.
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. Correct! It turns out that we can make it even cleaner. |
||
return True | ||
try: | ||
conv.to_rgba(color) | ||
except ValueError: | ||
return False | ||
else: | ||
return True |
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.
are these not relevant / texted? (e.g. the axes.prop_cycle) or is this handled by conv.to_rbga now?
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.
I guess that this was the way of handling compatibility with matplotlib <= 2.0.
If the color 'CN' is passed, then it would pick N-th color from "axes.prop_cycle".
Before matplotlib 2.0 there were no CN colors, so this is probably why this construction is here.