From 82eb3127ca3003129a50668523f21e164a0c8535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Sun, 30 Dec 2018 21:20:39 +0100 Subject: [PATCH 1/8] Fixing #20585. Ensuring that _get_standard_colors returns exactly num_colors when num_colors < 10. --- pandas/plotting/_style.py | 2 ++ pandas/tests/plotting/test_misc.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/pandas/plotting/_style.py b/pandas/plotting/_style.py index 930c3d1775ad8..e16098640d6df 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 9ae3e7fc423f4..4ad1395a351da 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -309,3 +309,13 @@ 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 From 148454ee69636539e622ef69f9e21a5d0a9a1799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Sun, 30 Dec 2018 21:49:20 +0100 Subject: [PATCH 2/8] Added example from #20585 --- pandas/tests/plotting/test_misc.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 4ad1395a351da..5695af898e291 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 = \ @@ -319,3 +319,15 @@ def test_get_standard_colors_default_num_colors(self): 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) From 00128e1f788eb3e074dc484742a6bb18c6787d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Sun, 30 Dec 2018 21:55:17 +0100 Subject: [PATCH 3/8] DOC: Fixed issue with _get_standard_colors --- doc/source/whatsnew/v0.24.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 04692c3e31e39..0acb755d5acc7 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -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 ^^^^^^^^^^^^^^^^^^^^^^^^ From 981cd7cc285388366d8cd948d6f4f4d4b76e6072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Sun, 30 Dec 2018 23:25:31 +0100 Subject: [PATCH 4/8] Separated _get_standard_colors test and improved whatsnew documentation --- doc/source/whatsnew/v0.24.0.rst | 2 +- pandas/tests/plotting/test_misc.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 0acb755d5acc7..9fab18b48c59a 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1593,7 +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`) +- Bug in :func:`_get_standard_colors` caused :class:`DataFrame` plots to use multiple colors instead of a single one when plotting without a colormap (:issue:`20585`) Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 5695af898e291..ae7d2e5fa53e6 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -313,6 +313,7 @@ def test_get_standard_colors_random_seed(self): 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') @@ -320,7 +321,8 @@ def test_get_standard_colors_default_num_colors(self): assert len(color2) == 9 assert len(color3) == 20 - # Example from #20585 + def test_plot_single_color(self): + # Example from #20585. All 3 bars in the plot 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], From 9f6e7a48154e0a9a30d16f5934e057dd1bf60317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Mon, 31 Dec 2018 11:13:29 +0100 Subject: [PATCH 5/8] Fixed linting issues --- pandas/tests/plotting/test_misc.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index ae7d2e5fa53e6..d826633b579a7 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -322,14 +322,17 @@ def test_get_standard_colors_default_num_colors(self): assert len(color3) == 20 def test_plot_single_color(self): - # Example from #20585. All 3 bars in the plot 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'], + # 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]) + colors = lmap(lambda rect: rect.get_facecolor(), + ax.get_children()[0:3]) assert all(color == colors[0] for color in colors) From 927c50b9621748acb3c91c2611534adb032d67e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Mon, 31 Dec 2018 12:37:14 +0100 Subject: [PATCH 6/8] Changing plotting core to ask for 3 colors instead of a single one. --- pandas/plotting/_core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index c55952085f8c5..05a62a5001380 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -2172,7 +2172,7 @@ 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) + return _get_standard_colors(color=kwds.get('color'), num_colors=3) def maybe_color_bp(bp): if 'color' not in kwds: From 10beae7592626a8c65a60dd5a4467f5cc7eac014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Mon, 31 Dec 2018 15:35:06 +0100 Subject: [PATCH 7/8] DOC: updated whatsnew --- doc/source/whatsnew/v0.24.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 9fab18b48c59a..bb464515631de 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1593,7 +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` caused :class:`DataFrame` plots to use multiple colors instead of a single one when plotting without a colormap (:issue:`20585`) +- Bug in :func:`DataFrame.plot.bar` caused bars to use multiple colors instead of a single one (:issue:`20585`) Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ From 5a1da48dcb3beba1534158de316cdd0be1d05bf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Lo=CC=81pez?= Date: Tue, 1 Jan 2019 19:19:07 +0100 Subject: [PATCH 8/8] Added comment in boxplot._get_colors --- pandas/plotting/_core.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/plotting/_core.py b/pandas/plotting/_core.py index 05a62a5001380..3ba06c0638317 100644 --- a/pandas/plotting/_core.py +++ b/pandas/plotting/_core.py @@ -2172,6 +2172,8 @@ def boxplot(data, column=None, by=None, ax=None, fontsize=None, column = 'x' def _get_colors(): + # 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):