Skip to content

BUG: Ensuring that _get_standard_colors returns num_colors (GH #20585) #24504

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
Jan 2, 2019
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 @@ -1593,6 +1593,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 :func:`_get_standard_colors` that returned 10 colors when num_colors < 10 and color_type='default' (:issue:`20585`)

Groupby/Resample/Rolling
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 2 additions & 0 deletions pandas/plotting/_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
list('bgrcmyk')))
if isinstance(colors, compat.string_types):
colors = list(colors)

colors = colors[0:num_colors]
elif color_type == 'random':
import pandas.core.common as com

Expand Down
26 changes: 24 additions & 2 deletions pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ def test_parallel_coordinates_with_sorted_labels(self):

df = DataFrame({"feat": [i for i in range(30)],
"class": [2 for _ in range(10)] +
[3 for _ in range(10)] +
[1 for _ in range(10)]})
[3 for _ in range(10)] +
[1 for _ in range(10)]})
ax = parallel_coordinates(df, 'class', sort_labels=True)
polylines, labels = ax.get_legend_handles_labels()
color_label_tuples = \
Expand Down Expand Up @@ -309,3 +309,25 @@ 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_default_num_colors(self):
from pandas.plotting._style import _get_standard_colors

color1 = _get_standard_colors(1, color_type='default')
color2 = _get_standard_colors(9, color_type='default')
color3 = _get_standard_colors(20, color_type='default')
assert len(color1) == 1
assert len(color2) == 9
assert len(color3) == 20

# Example from #20585
df = DataFrame({'account-start': ['2017-02-03', '2017-03-03', '2017-01-01'],
'client': ['Alice Anders', 'Bob Baker', 'Charlie Chaplin'],
'balance': [-1432.32, 10.43, 30000.00],
'db-id': [1234, 2424, 251],
'proxy-id': [525, 1525, 2542],
'rank': [52, 525, 32],
})
ax = df.client.value_counts().plot.bar()
colors = lmap(lambda rect: rect.get_facecolor(), ax.get_children()[0:3])
assert all(color == colors[0] for color in colors)