8000 BUG: fix matplotlib warning on CN color by ivanovmg · Pull Request #36981 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

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

Merged
merged 12 commits into from
Oct 10, 2020
Prev Previous commit
Next Next commit
REF: move _is_*_color funcs to module level
  • Loading branch information
ivanovmg committed Oct 8, 2020
commit d489a105b7451206dbdc9ccd84f95593c3a2591b
41 changes: 26 additions & 15 deletions pandas/plotting/_matplotlib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,6 @@ def random_color(column):
raise ValueError("color_type must be either 'default' or 'random'")

if isinstance(colors, str):
conv = matplotlib.colors.ColorConverter()

def _is_cn_color(color: str) -> bool:
return bool(color in ["C" + str(x) for x in range(10)])

def _is_single_color(colors: Union[str, Sequence[str]]) -> bool:
if _is_cn_color(colors):
return True
try:
conv.to_rgba(colors)
except ValueError:
return False
else:
return True

if _is_single_color(colors):
Copy link
Member

Choose a reason for hiding this comment

The 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!

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

colors = [colors]

Expand All @@ -90,3 +75,29 @@ def _is_single_color(colors: Union[str, Sequence[str]]) -> bool:
colors += colors[:mod]

return colors


def _is_cn_color(color: str) -> bool:
"""Check if color string is CN color, like 'C0', 'C1', etc."""
return bool(color in ["C" + str(x) for x in range(10)])


def _is_single_color(colors: Union[str, Sequence[str]]) -> bool:
"""Check if ``colors`` is a single color.

Examples of single colors:
- 'r'
- 'g'
- 'red'
- 'green'
- 'C3'
"""
conv = matplotlib.colors.ColorConverter()
if _is_cn_color(colors):
return True
try:
conv.to_rgba(colors)
except ValueError:
return False
else:
return True
< 2954 /div>
0