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
TYP/DOC: fix typing, add docs
  • Loading branch information
ivanovmg committed Oct 8, 2020
commit aa14f33eddd2b8ee8b926587d1c6eb191fe595bf
23 changes: 17 additions & 6 deletions pandas/plotting/_matplotlib/style.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# being a bit too dynamic
from typing import Sequence, Union
import warnings

import matplotlib.cm as cm
Expand Down Expand Up @@ -79,24 +78,36 @@ def random_color(column):

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)])
cn_colors = ["C" + str(x) for x in range(10)]
Copy link
Contributor

Choose a reason for hiding this comment

The 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(colors: Union[str, Sequence[str]]) -> bool:
"""Check if ``colors`` is a single color.
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(colors):
if _is_cn_color(color):
Copy link
Contributor

Choose a reason for hiding this comment

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

to_rgba handles the "Cn" style, so why is this separate check needed?

Copy link
Member Author

Choose a reason for hiding this comment

The 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(colors)
conv.to_rgba(color)
except ValueError:
return False
else:
Expand Down
0