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
60 changes: 39 additions & 21 deletions pandas/plotting/_matplotlib/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

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?

Copy link
Member Author

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.

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):
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]
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
Expand All @@ -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)]
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(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):
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(color)
except ValueError:
return False
else:
return True
13 changes: 10 additions & 3 deletions pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,17 @@ def test_integer_array_plot(self):

def test_mpl2_color_cycle_str(self):
# GH 15516
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)
colors = ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
with warnings.catch_warnings(record=True) as w:
# GH 36972
warnings.simplefilter("always", "MatplotlibDeprecationWarning")

for color in colors:
_check_plot_works(df.plot, color=color)

no_warning_raised = len(w) == 0
assert no_warning_raised, "MatplotlibDeprecationWarning was raised"

def test_color_single_series_list(self):
# GH 3486
Expand Down