Skip to content

Commit 179822a

Browse files
nokutujreback
authored andcommitted
BUG: Ensuring that _get_standard_colors returns num_colors (GH #20585) (#24504)
1 parent e7dbb76 commit 179822a

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1628,6 +1628,7 @@ Plotting
16281628

16291629
- 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`)
16301630
- Bug in plotting a Series with datetimes using :func:`matplotlib.axes.Axes.scatter` (:issue:`22039`)
1631+
- Bug in :func:`DataFrame.plot.bar` caused bars to use multiple colors instead of a single one (:issue:`20585`)
16311632
- 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`)
16321633

16331634
Groupby/Resample/Rolling

pandas/plotting/_core.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,9 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None,
21722172
column = 'x'
21732173

21742174
def _get_colors():
2175-
return _get_standard_colors(color=kwds.get('color'), num_colors=1)
2175+
# num_colors=3 is required as method maybe_color_bp takes the colors
2176+
# in positions 0 and 2.
2177+
return _get_standard_colors(color=kwds.get('color'), num_colors=3)
21762178

21772179
def maybe_color_bp(bp):
21782180
if 'color' not in kwds:

pandas/plotting/_style.py

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def _get_standard_colors(num_colors=None, colormap=None, color_type='default',
4242
list('bgrcmyk')))
4343
if isinstance(colors, compat.string_types):
4444
colors = list(colors)
45+
46+
colors = colors[0:num_colors]
4547
elif color_type == 'random':
4648
import pandas.core.common as com
4749

pandas/tests/plotting/test_misc.py

+29-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ def test_parallel_coordinates_with_sorted_labels(self):
215215

216216
df = DataFrame({"feat": [i for i in range(30)],
217217
"class": [2 for _ in range(10)] +
218-
[3 for _ in range(10)] +
219-
[1 for _ in range(10)]})
218+
[3 for _ in range(10)] +
219+
[1 for _ in range(10)]})
220220
ax = parallel_coordinates(df, 'class', sort_labels=True)
221221
polylines, labels = ax.get_legend_handles_labels()
222222
color_label_tuples = \
@@ -310,6 +310,33 @@ def test_get_standard_colors_random_seed(self):
310310
color2 = _get_standard_colors(1, color_type='random')
311311
assert color1 == color2
312312

313+
def test_get_standard_colors_default_num_colors(self):
314+
from pandas.plotting._style import _get_standard_colors
315+
316+
# Make sure the default color_types returns the specified amount
317+
color1 = _get_standard_colors(1, color_type='default')
318+
color2 = _get_standard_colors(9, color_type='default')
319+
color3 = _get_standard_colors(20, color_type='default')
320+
assert len(color1) == 1
321+
assert len(color2) == 9
322+
assert len(color3) == 20
323+
324+
def test_plot_single_color(self):
325+
# Example from #20585. All 3 bars should have the same color
326+
df = DataFrame({'account-start': ['2017-02-03', '2017-03-03',
327+
'2017-01-01'],
328+
'client': ['Alice Anders', 'Bob Baker',
329+
'Charlie Chaplin'],
330+
'balance': [-1432.32, 10.43, 30000.00],
331+
'db-id': [1234, 2424, 251],
332+
'proxy-id': [525, 1525, 2542],
333+
'rank': [52, 525, 32],
334+
})
335+
ax = df.client.value_counts().plot.bar()
336+
colors = lmap(lambda rect: rect.get_facecolor(),
337+
ax.get_children()[0:3])
338+
assert all(color == colors[0] for color in colors)
339+
313340
def test_get_standard_colors_no_appending(self):
314341
# GH20726
315342

0 commit comments

Comments
 (0)