Skip to content

BUG: Fix a bug in plotting when using color array. #20726 #20727

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 10 commits into from
Dec 31, 2018
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,7 @@ Plotting

- 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`)
- Bug in plotting a Series with datetimes using :func:`matplotlib.axes.Axes.scatter` (:issue:`22039`)
- 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`)

Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
5 changes: 4 additions & 1 deletion pandas/plotting/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def _maybe_valid_colors(colors):
# mpl will raise error any of them is invalid
pass

if len(colors) != num_colors:
# Append more colors by cycling if there is not enough color.
# Extra colors will be ignored by matplotlib if there are more colors
# than needed and nothing needs to be done here.
if len(colors) < num_colors:
try:
multiple = num_colors // len(colors) - 1
except ZeroDivisionError:
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,21 @@ def test_get_standard_colors_random_seed(self):
color1 = _get_standard_colors(1, color_type='random')
color2 = _get_standard_colors(1, color_type='random')
assert color1 == color2

def test_get_standard_colors_no_appending(self):
# GH20726

# Make sure not to add more colors so that matplotlib can cycle
# correctly.
from matplotlib import cm
color_before = cm.gnuplot(range(5))
color_after = plotting._style._get_standard_colors(
1, color=color_before)
assert len(color_after) == len(color_before)

df = DataFrame(np.random.randn(48, 4), columns=list("ABCD"))

color_list = cm.gnuplot(np.linspace(0, 1, 16))
p = df.A.plot.bar(figsize=(16, 7), color=color_list)
assert (p.patches[1].get_facecolor()
== p.patches[17].get_facecolor())