Skip to content

Fixing _get_standard_colors to honor num_colors arguments (#20585) #22439

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,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 colors selection in :func:`DataFrame.plot.bar` which causes different colors to be used when no color is specified (:issue:`20585`)

Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
10 changes: 10 additions & 0 deletions pandas/plotting/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,16 @@ def _plot(cls, ax, x, y, w, start=0, log=False, **kwds):
def _start_base(self):
return self.bottom

def _get_colors(self, num_colors=None, color_kwds='color'):
color = self.kwds.get(color_kwds)
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a doc-string here

if color is not None:
return color
else:
num_colors = self.nseries
return _get_standard_colors(num_colors=num_colors,
colormap=self.colormap,
color=self.kwds.get(color_kwds))

def _make_plot(self):
import matplotlib as mpl

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

if len(colors) != num_colors:
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a comment here

if len(colors) > num_colors:
colors = colors[:num_colors]
elif len(colors) < num_colors:
try:
multiple = num_colors // len(colors) - 1
except ZeroDivisionError:
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from pandas.tests.plotting.common import (TestPlotBase, _check_plot_works,
_skip_if_no_scipy_gaussian_kde,
_ok_for_gaussian_kde)
from pandas.plotting._style import _get_standard_colors


@td.skip_if_no_mpl
Expand Down Expand Up @@ -268,6 +269,36 @@ def test_bar_user_colors(self):
(1., 0., 0., 1.)]
assert result == expected

def test_bar_user_colors_limited(self):
# GH 20585
s = Series([1, 2, 3, 4])
ax = s.plot.bar(color=['red', 'blue'])
result = [p.get_facecolor() for p in ax.patches]
expected = [(1., 0., 0., 1.),
(0., 0., 1., 1.),
(1., 0., 0., 1.),
(0., 0., 1., 1.)]
assert result == expected

def test_bar_user_colors_more_limited(self):
s = Series([1, 2, 3, 4, 5, 6])
ax = s.plot.bar(color=['red', 'blue'])
result = [p.get_facecolor() for p in ax.patches]
expected = [(1., 0., 0., 1.),
(0., 0., 1., 1.),
(1., 0., 0., 1.),
(0., 0., 1., 1.),
(1., 0., 0., 1.),
(0., 0., 1., 1.)]
assert result == expected

@pytest.mark.parametrize("num_colors",range(0,15))
def test_standard_color(self, num_colors):
# GH 20585
colors = _get_standard_colors(num_colors=num_colors)

assert len(colors) == num_colors

def test_rotation(self):
df = DataFrame(randn(5, 5))
# Default rot 0
Expand Down