Skip to content

Commit 7d755ba

Browse files
wuhaochenPingviinituutti
authored andcommitted
BUG: Fix a bug in plotting when using color array. pandas-dev#20726 (pandas-dev#20727)
1 parent ee0a7f1 commit 7d755ba

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,7 @@ Plotting
15971597

15981598
- Bug in :func:`DataFrame.plot.scatter` and :func:`DataFrame.plot.hexbin` caused x-axis label and ticklabels to disappear when colorbar was on in IPython inline backend (:issue:`10611`, :issue:`10678`, and :issue:`20455`)
15991599
- Bug in plotting a Series with datetimes using :func:`matplotlib.axes.Axes.scatter` (:issue:`22039`)
1600+
- Bug in validating color parameter caused extra color to be appended to the given color array. This happened to multiple plotting functions using matplotlib. (:issue:`20726`)
16001601

16011602
Groupby/Resample/Rolling
16021603
^^^^^^^^^^^^^^^^^^^^^^^^

pandas/plotting/_style.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def _maybe_valid_colors(colors):
8181
# mpl will raise error any of them is invalid
8282
pass
8383

84-
if len(colors) != num_colors:
84+
# Append more colors by cycling if there is not enough color.
85+
# Extra colors will be ignored by matplotlib if there are more colors
86+
# than needed and nothing needs to be done here.
87+
if len(colors) < num_colors:
8588
try:
8689
multiple = num_colors // len(colors) - 1
8790
except ZeroDivisionError:

pandas/tests/plotting/test_misc.py

+18
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,21 @@ def test_get_standard_colors_random_seed(self):
309309
color1 = _get_standard_colors(1, color_type='random')
310310
color2 = _get_standard_colors(1, color_type='random')
311311
assert color1 == color2
312+
313+
def test_get_standard_colors_no_appending(self):
314+
# GH20726
315+
316+
# Make sure not to add more colors so that matplotlib can cycle
317+
# correctly.
318+
from matplotlib import cm
319+
color_before = cm.gnuplot(range(5))
320+
color_after = plotting._style._get_standard_colors(
321+
1, color=color_before)
322+
assert len(color_after) == len(color_before)
323+
324+
df = DataFrame(np.random.randn(48, 4), columns=list("ABCD"))
325+
326+
color_list = cm.gnuplot(np.linspace(0, 1, 16))
327+
p = df.A.plot.bar(figsize=(16, 7), color=color_list)
328+
assert (p.patches[1].get_facecolor()
329+
== p.patches[17].get_facecolor())

0 commit comments

Comments
 (0)