From 36329dc4e9ac48c1403e112f672efa41fd516cfd Mon Sep 17 00:00:00 2001 From: Haochen Wu Date: Tue, 17 Apr 2018 16:43:39 -0700 Subject: [PATCH 1/7] fix a bug in plotting when using color array. --- pandas/plotting/_style.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/plotting/_style.py b/pandas/plotting/_style.py index 426b29a8840f4..a1242157467a9 100644 --- a/pandas/plotting/_style.py +++ b/pandas/plotting/_style.py @@ -91,7 +91,7 @@ def _maybe_valid_colors(colors): # mpl will raise error any of them is invalid pass - if len(colors) != num_colors: + if len(colors) < num_colors: try: multiple = num_colors // len(colors) - 1 except ZeroDivisionError: From 9c7e4e65fae9dd497f60892e26ec40129de580f4 Mon Sep 17 00:00:00 2001 From: Haochen Wu Date: Sat, 21 Apr 2018 18:04:32 -0700 Subject: [PATCH 2/7] add test --- pandas/tests/plotting/test_misc.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index c82c939584dc7..b16d8c4edc358 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -313,3 +313,14 @@ 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_no_appending(self): + # GH20726 + + # Make sure not to add more colors so that matplotlib can cycle + # correctly. + from pandas.plotting._style import _get_standard_colors + from matplotlib import cm + color_before = cm.gnuplot(range(5)) + color_after = _get_standard_colors(1, color=color_before) + assert len(color_after) == len(color_before) From 76fb3477d5415cca3cd8b85336c59465b258656d Mon Sep 17 00:00:00 2001 From: Haochen Wu Date: Sat, 21 Apr 2018 18:17:14 -0700 Subject: [PATCH 3/7] Add whatsnew --- doc/source/whatsnew/v0.23.0.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index 641214550a3b7..8ac9d8888ca69 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -1159,6 +1159,7 @@ Plotting - Bug in formatting tick labels with ``datetime.time()`` and fractional seconds (:issue:`18478`). - :meth:`Series.plot.kde` has exposed the args ``ind`` and ``bw_method`` in the docstring (:issue:`18461`). The argument ``ind`` may now also be an integer (number of sample points). - :func:`DataFrame.plot` now supports multiple columns to the ``y`` argument (:issue:`19699`) +- Bug in providing color array as color for a single plot (:issue:`20726`) Groupby/Resample/Rolling From 337bdea0770dbde1881f0d049b9abc3689214bb8 Mon Sep 17 00:00:00 2001 From: Haochen Wu Date: Tue, 14 Aug 2018 12:15:23 -0700 Subject: [PATCH 4/7] Move whatsnew and add more test --- doc/source/whatsnew/v0.23.0.txt | 1 - doc/source/whatsnew/v0.24.0.txt | 2 ++ pandas/tests/plotting/test_misc.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.23.0.txt b/doc/source/whatsnew/v0.23.0.txt index efea2a9e9614d..473a4bb72e6d9 100644 --- a/doc/source/whatsnew/v0.23.0.txt +++ b/doc/source/whatsnew/v0.23.0.txt @@ -1342,7 +1342,6 @@ Plotting - Bug in formatting tick labels with ``datetime.time()`` and fractional seconds (:issue:`18478`). - :meth:`Series.plot.kde` has exposed the args ``ind`` and ``bw_method`` in the docstring (:issue:`18461`). The argument ``ind`` may now also be an integer (number of sample points). - :func:`DataFrame.plot` now supports multiple columns to the ``y`` argument (:issue:`19699`) -- Bug in providing color array as color for a single plot (:issue:`20726`) Groupby/Resample/Rolling diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index cf12759c051fc..37df314ee7aa3 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -680,6 +680,8 @@ 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 validating color parameter caused extra color to be appended to the given color array. This happened to multiple plotting functions that plotting with matplotlib. (:issue:`20726`) + Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index b00b3b1ada873..4363814907974 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -323,3 +323,15 @@ def test_get_standard_colors_no_appending(self): color_before = cm.gnuplot(range(5)) color_after = _get_standard_colors(1, color=color_before) assert len(color_after) == len(color_before) + + # Original bug report example. + import numpy as np + import pandas as pd + + df = pd.DataFrame(np.abs(np.random.randn(48, 4)), + columns=list("ABCD")) + + color_list = cm.gnuplot(np.linspace(0, 1, 16)) + p = df.A.plot.bar(figsize=(16, 7), color=color_list) + assert (p.patches[1].get_facecolor() + == p.patches[17].get_facecolor()) From 0aa529f61e6996c51d37d0eb28ed8342259aa83f Mon Sep 17 00:00:00 2001 From: Haochen Wu Date: Mon, 26 Nov 2018 10:37:35 -0800 Subject: [PATCH 5/7] Move whatsnew --- 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 6fd0a224b81b2..8b2e515e03489 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1406,6 +1406,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 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 ^^^^^^^^^^^^^^^^^^^^^^^^ From 958aea6fec2cb8d4f55c937970bb81e16d129437 Mon Sep 17 00:00:00 2001 From: Haochen Wu Date: Thu, 20 Dec 2018 11:51:51 -0800 Subject: [PATCH 6/7] move imports and add more comments --- pandas/plotting/_style.py | 3 +++ pandas/tests/plotting/test_misc.py | 11 ++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/plotting/_style.py b/pandas/plotting/_style.py index 89d0a3ba66225..0bad2b42dc310 100644 --- a/pandas/plotting/_style.py +++ b/pandas/plotting/_style.py @@ -80,6 +80,9 @@ def _maybe_valid_colors(colors): # mpl will raise error any of them is invalid pass + # Append more colors by cycling if there is not enough color. + # Extra colors will be ignored by matplotlib if there are more colors + # than needed and nothing needs to be done here. if len(colors) < num_colors: try: multiple = num_colors // len(colors) - 1 diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 2422434208e75..5fa7d6f841711 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -4,6 +4,7 @@ import pytest +import pandas as pd from pandas import DataFrame from pandas.compat import lmap import pandas.util.testing as tm @@ -314,17 +315,13 @@ def test_get_standard_colors_no_appending(self): # Make sure not to add more colors so that matplotlib can cycle # correctly. - from pandas.plotting._style import _get_standard_colors from matplotlib import cm color_before = cm.gnuplot(range(5)) - color_after = _get_standard_colors(1, color=color_before) + color_after = plotting._style._get_standard_colors( + 1, color=color_before) assert len(color_after) == len(color_before) - # Original bug report example. - import numpy as np - import pandas as pd - - df = pd.DataFrame(np.abs(np.random.randn(48, 4)), + df = pd.DataFrame(np.random.randn(48, 4), columns=list("ABCD")) color_list = cm.gnuplot(np.linspace(0, 1, 16)) From a26f8e92c356e2f38e2f86bfe8bec712301e06a9 Mon Sep 17 00:00:00 2001 From: Matt Roeschke Date: Sun, 30 Dec 2018 16:11:08 -0800 Subject: [PATCH 7/7] Adjust test --- pandas/tests/plotting/test_misc.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/tests/plotting/test_misc.py b/pandas/tests/plotting/test_misc.py index 5fa7d6f841711..b369cce3e6c12 100644 --- a/pandas/tests/plotting/test_misc.py +++ b/pandas/tests/plotting/test_misc.py @@ -4,7 +4,6 @@ import pytest -import pandas as pd from pandas import DataFrame from pandas.compat import lmap import pandas.util.testing as tm @@ -321,8 +320,7 @@ def test_get_standard_colors_no_appending(self): 1, color=color_before) assert len(color_after) == len(color_before) - df = pd.DataFrame(np.random.randn(48, 4), - columns=list("ABCD")) + df = DataFrame(np.random.randn(48, 4), columns=list("ABCD")) color_list = cm.gnuplot(np.linspace(0, 1, 16)) p = df.A.plot.bar(figsize=(16, 7), color=color_list)