Skip to content

Commit c442745

Browse files
ivanovmgKevin D Smith
authored and
Kevin D Smith
committed
BUG: fix matplotlib warning on CN color (pandas-dev#36981)
1 parent bb90e37 commit c442745

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

pandas/plotting/_matplotlib/style.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,9 @@ def random_color(column):
5656
else:
5757
raise ValueError("color_type must be either 'default' or 'random'")
5858

59-
if isinstance(colors, str):
60-
conv = matplotlib.colors.ColorConverter()
61-
62-
def _maybe_valid_colors(colors):
63-
try:
64-
[conv.to_rgba(c) for c in colors]
65-
return True
66-
except ValueError:
67-
return False
68-
69-
# check whether the string can be convertible to single color
70-
maybe_single_color = _maybe_valid_colors([colors])
71-
# check whether each character can be convertible to colors
72-
maybe_color_cycle = _maybe_valid_colors(list(colors))
73-
if maybe_single_color and maybe_color_cycle and len(colors) > 1:
74-
hex_color = [c["color"] for c in list(plt.rcParams["axes.prop_cycle"])]
75-
colors = [hex_color[int(colors[1])]]
76-
elif maybe_single_color:
77-
colors = [colors]
78-
else:
79-
# ``colors`` is regarded as color cycle.
80-
# mpl will raise error any of them is invalid
81-
pass
59+
if isinstance(colors, str) and _is_single_color(colors):
60+
# GH #36972
61+
colors = [colors]
8262

8363
# Append more colors by cycling if there is not enough color.
8464
# Extra colors will be ignored by matplotlib if there are more colors
@@ -94,3 +74,33 @@ def _maybe_valid_colors(colors):
9474
colors += colors[:mod]
9575

9676
return colors
77+
78+
79+
def _is_single_color(color: str) -> bool:
80+
"""Check if ``color`` is a single color.
81+
82+
Examples of single colors:
83+
- 'r'
84+
- 'g'
85+
- 'red'
86+
- 'green'
87+
- 'C3'
88+
89+
Parameters
90+
----------
91+
color : string
92+
Color string.
93+
94+
Returns
95+
-------
96+
bool
97+
True if ``color`` looks like a valid color.
98+
False otherwise.
99+
"""
100+
conv = matplotlib.colors.ColorConverter()
101+
try:
102+
conv.to_rgba(color)
103+
except ValueError:
104+
return False
105+
else:
106+
return True

pandas/tests/plotting/test_frame.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,21 @@ def test_integer_array_plot(self):
170170

171171
def test_mpl2_color_cycle_str(self):
172172
# GH 15516
173-
colors = ["C" + str(x) for x in range(10)]
174173
df = DataFrame(randn(10, 3), columns=["a", "b", "c"])
175-
for c in colors:
176-
_check_plot_works(df.plot, color=c)
174+
colors = ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
175+
with warnings.catch_warnings(record=True) as w:
176+
warnings.simplefilter("always", "MatplotlibDeprecationWarning")
177+
178+
for color in colors:
179+
_check_plot_works(df.plot, color=color)
180+
181+
# if warning is raised, check that it is the exact problematic one
182+
# GH 36972
183+
if w:
184+
match = "Support for uppercase single-letter colors is deprecated"
185+
warning_message = str(w[0].message)
186+
msg = "MatplotlibDeprecationWarning related to CN colors was raised"
187+
assert match not in warning_message, msg
177188

178189
def test_color_single_series_list(self):
179190
# GH 3486

0 commit comments

Comments
 (0)