diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 17de01f388d59..055303b90ef1a 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1597,6 +1597,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:`DataFrame.plot.bar` caused bars to use multiple colors instead of a single one (:issue:`20585`) - 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 diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index c55952085f8c5..3ba06c0638317 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -2172,7 +2172,9 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None, column = 'x' def _get_colors(): - return _get_standard_colors(color=kwds.get('color'), num_colors=1) + # num_colors=3 is required as method maybe_color_bp takes the colors + # in positions 0 and 2. + return _get_standard_colors(color=kwds.get('color'), num_colors=3) def maybe_color_bp(bp): if 'color' not in kwds: diff --git a/pandas/plotting/_style.py b/pandas/plotting/_style.py index d50fa48c92cf5..d9da34e008763 100644 --- a/pandas/plotting/_style.py +++ b/pandas/plotting/_style.py @@ -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 diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 5d9ece09c9dcc..de9e2a16cd15e 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -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 = \ @@ -310,6 +310,33 @@ def test_get_standard_colors_random_seed(self): 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 + + # Make sure the default color_types returns the specified amount + 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 + + def test_plot_single_color(self): + # Example from #20585. All 3 bars should have the same color + 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) + def test_get_standard_colors_no_appending(self): # GH20726