From cf9b89280435389dfa01601348b51fb607d54e3a Mon Sep 17 00:00:00 2001 From: joooeey Date: Mon, 27 Apr 2020 15:55:08 +0200 Subject: [PATCH 01/15] BUG: repair 'style' kwd handling in DataFrame.plot (#21003) --- pandas/plotting/_matplotlib/core.py | 28 +++++++++++++++++++--------- pandas/tests/plotting/test_frame.py | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 46941e437a4ce..038767e8fcd13 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -193,7 +193,6 @@ def __init__( self._validate_color_args() def _validate_color_args(self): - import matplotlib.colors if ( "color" in self.kwds @@ -226,13 +225,24 @@ def _validate_color_args(self): styles = [self.style] # need only a single match for s in styles: - for char in s: - if char in matplotlib.colors.BASE_COLORS: - raise ValueError( - "Cannot pass 'style' string with a color symbol and " - "'color' keyword argument. Please use one or the other or " - "pass 'style' without a color symbol" - ) + if self._color_in_style(s): + raise ValueError( + "Cannot pass 'style' string with a color symbol and " + "'color' keyword argument. Please use one or the other" + "or pass 'style' without a color symbol" + ) + + @staticmethod + def _color_in_style(style): + """ + Is there a color letter in the style string? + + Returns + ------- + bool + """ + from matplotlib.colors import BASE_COLORS + return not set(BASE_COLORS).isdisjoint(style) def _iter_data(self, data=None, keep_index=False, fillna=None): if data is None: @@ -723,7 +733,7 @@ def _apply_style_colors(self, colors, kwds, col_num, label): style = self.style has_color = "color" in kwds or self.colormap is not None - nocolor_style = style is None or re.match("[a-z]+", style) is None + nocolor_style = (style is None or not self._color_in_style(style)) if (has_color or self.subplots) and nocolor_style: if isinstance(colors, dict): kwds["color"] = colors[label] diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index c84a09f21f46b..1465acbea62ab 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -204,6 +204,24 @@ def test_color_and_style_arguments(self): with pytest.raises(ValueError): df.plot(color=["red", "black"], style=["k-", "r--"]) + def test_color_and_style_letter(self): + # GH21003 2018-05-10 + df = DataFrame(np.random.random((7, 4))) + # combining color string and marker letter should be allowed + df.plot(color='green', style='d') # d for diamond + + def test_colors_and_style_letter(self): + # GH21003 2018-04-23 + df = DataFrame(np.random.random((7, 4))) + # combining a color list and a marker letter should be allowed + color = ['yellow', 'red', 'green', 'blue'] + ax = df.plot(color=color, style='d') + output_colors = [line.get_color() for line in ax.lines] + assert output_colors == color # each "line" is one of the four colors + # In the Github bug the color list was passed to MPL like this: + # ax.lines[i].get_color() == ['yellow', 'red', 'green', 'blue'] + + def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) ax = df.plot() From 20b8b4ba92eabe104812157fb595ce4f6c5f0db0 Mon Sep 17 00:00:00 2001 From: joooeey Date: Mon, 27 Apr 2020 16:04:16 +0200 Subject: [PATCH 02/15] formatting test_frame.py --- pandas/tests/plotting/test_frame.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 1465acbea62ab..884b78ff123e5 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -207,7 +207,7 @@ def test_color_and_style_arguments(self): def test_color_and_style_letter(self): # GH21003 2018-05-10 df = DataFrame(np.random.random((7, 4))) - # combining color string and marker letter should be allowed + # combining a color string and a marker letter should be allowed df.plot(color='green', style='d') # d for diamond def test_colors_and_style_letter(self): @@ -220,7 +220,6 @@ def test_colors_and_style_letter(self): assert output_colors == color # each "line" is one of the four colors # In the Github bug the color list was passed to MPL like this: # ax.lines[i].get_color() == ['yellow', 'red', 'green', 'blue'] - def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) From fe8dbeb9aad2f90b4fdc172ae9d0c1726f3fb27e Mon Sep 17 00:00:00 2001 From: joooeey Date: Mon, 27 Apr 2020 18:44:32 +0200 Subject: [PATCH 03/15] white space and test issues supposedly fixed --- pandas/plotting/_matplotlib/core.py | 4 ++-- pandas/tests/plotting/test_frame.py | 16 ++++++++-------- pandas/tests/plotting/test_series.py | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 038767e8fcd13..a847c92e33c4d 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -228,8 +228,8 @@ def _validate_color_args(self): if self._color_in_style(s): raise ValueError( "Cannot pass 'style' string with a color symbol and " - "'color' keyword argument. Please use one or the other" - "or pass 'style' without a color symbol" + "'color' keyword argument. Please use one or the " + "other or pass 'style' without a color symbol" ) @staticmethod diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 884b78ff123e5..e55357be24389 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -204,20 +204,20 @@ def test_color_and_style_arguments(self): with pytest.raises(ValueError): df.plot(color=["red", "black"], style=["k-", "r--"]) - def test_color_and_style_letter(self): + def test_color_and_marker(self): # GH21003 2018-05-10 - df = DataFrame(np.random.random((7, 4))) + df = DataFrame(np.random.random((7, 4))) # combining a color string and a marker letter should be allowed - df.plot(color='green', style='d') # d for diamond + df.plot(color="green", style="d") # d for diamond - def test_colors_and_style_letter(self): + def test_color_list_and_marker(self): # GH21003 2018-04-23 - df = DataFrame(np.random.random((7, 4))) + df = DataFrame(np.random.random((7, 4))) # combining a color list and a marker letter should be allowed - color = ['yellow', 'red', 'green', 'blue'] - ax = df.plot(color=color, style='d') + color = ["yellow", "red", "green", "blue"] + ax = df.plot(color=color, style="d") output_colors = [line.get_color() for line in ax.lines] - assert output_colors == color # each "line" is one of the four colors + assert output_colors == color # each "line" is one of the four colors # In the Github bug the color list was passed to MPL like this: # ax.lines[i].get_color() == ['yellow', 'red', 'green', 'blue'] diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 5341878d4986e..fab9a9c331185 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -933,4 +933,4 @@ def test_plot_no_numeric_data(self): def test_style_single_ok(self): s = pd.Series([1, 2]) ax = s.plot(style="s", color="C3") - assert ax.lines[0].get_color() == ["C3"] + assert ax.lines[0].get_color() == "C3" From 12e1d109eb5a772bb689d34f936bfffd327afcb8 Mon Sep 17 00:00:00 2001 From: joooeey Date: Mon, 27 Apr 2020 19:19:41 +0200 Subject: [PATCH 04/15] more style fixes in core.py --- pandas/plotting/_matplotlib/core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index a847c92e33c4d..322bd6830196c 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -1,4 +1,3 @@ -import re from typing import Optional import warnings @@ -236,7 +235,7 @@ def _validate_color_args(self): def _color_in_style(style): """ Is there a color letter in the style string? - + Returns ------- bool From 948ebababb9168a9d2c53493e699d6412ec7fa7e Mon Sep 17 00:00:00 2001 From: joooeey Date: Tue, 28 Apr 2020 09:32:33 +0200 Subject: [PATCH 05/15] whatsnew updated --- doc/source/whatsnew/v1.1.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 07849702c646d..2ca9fc42edde0 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -593,6 +593,7 @@ Plotting - Bug in :meth:`DataFrame.plot.hist` where weights are not working for multiple columns (:issue:`33173`) - Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`) - Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`) +- Bug in :meth:`DataFrame.plot` where valid style kwds were rejected (:issue:`21003`) Groupby/resample/rolling From 40d673ba7fdc2de3759fd3349df1eb79fd5d7e87 Mon Sep 17 00:00:00 2001 From: joooeey Date: Tue, 28 Apr 2020 15:34:07 +0200 Subject: [PATCH 06/15] black manually made edits suggested by `black`. --- pandas/plotting/_matplotlib/core.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 322bd6830196c..7d4b5c214dc8c 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -241,6 +241,7 @@ def _color_in_style(style): bool """ from matplotlib.colors import BASE_COLORS + return not set(BASE_COLORS).isdisjoint(style) def _iter_data(self, data=None, keep_index=False, fillna=None): @@ -732,7 +733,7 @@ def _apply_style_colors(self, colors, kwds, col_num, label): style = self.style has_color = "color" in kwds or self.colormap is not None - nocolor_style = (style is None or not self._color_in_style(style)) + nocolor_style = style is None or not self._color_in_style(style) if (has_color or self.subplots) and nocolor_style: if isinstance(colors, dict): kwds["color"] = colors[label] From 04edda523605db753d79756c8dff8cc2232e91fc Mon Sep 17 00:00:00 2001 From: joooeey Date: Thu, 30 Apr 2020 21:53:59 +0200 Subject: [PATCH 07/15] addressed marco's comments and spelling --- doc/source/whatsnew/v1.1.0.rst | 6 +++--- pandas/plotting/_matplotlib/core.py | 8 ++------ pandas/tests/plotting/test_frame.py | 7 ++++--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 2ca9fc42edde0..555f3422ed9b0 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -589,11 +589,11 @@ I/O Plotting ^^^^^^^^ -- :func:`.plot` for line/bar now accepts color by dictonary (:issue:`8193`). +- :func:`.plot` for line/bar now accepts color by dictionary (:issue:`8193`). - Bug in :meth:`DataFrame.plot.hist` where weights are not working for multiple columns (:issue:`33173`) - Bug in :meth:`DataFrame.boxplot` and :meth:`DataFrame.plot.boxplot` lost color attributes of ``medianprops``, ``whiskerprops``, ``capprops`` and ``medianprops`` (:issue:`30346`) -- Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars alway use the first ``cmap`` (:issue:`33389`) -- Bug in :meth:`DataFrame.plot` where valid style kwds were rejected (:issue:`21003`) +- Bug in :meth:`DataFrame.plot.scatter` that when adding multiple plots with different ``cmap``, colorbars always use the first ``cmap`` (:issue:`33389`) +- Bug in :meth:`DataFrame.plot` where a marker letter in the ``style`` keyword sometimes causes a ``ValueError`` (:issue:`21003`) Groupby/resample/rolling diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 7d4b5c214dc8c..4e594fe961a3e 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -232,13 +232,9 @@ def _validate_color_args(self): ) @staticmethod - def _color_in_style(style): + def _color_in_style(style: str) -> bool: """ - Is there a color letter in the style string? - - Returns - ------- - bool + Check if there is a color letter in the style string. """ from matplotlib.colors import BASE_COLORS diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index e55357be24389..25fc7ba573a26 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -205,21 +205,22 @@ def test_color_and_style_arguments(self): df.plot(color=["red", "black"], style=["k-", "r--"]) def test_color_and_marker(self): - # GH21003 2018-05-10 + # GH21003 - code sample from 2018-05-10 is equivalent to this test df = DataFrame(np.random.random((7, 4))) # combining a color string and a marker letter should be allowed df.plot(color="green", style="d") # d for diamond def test_color_list_and_marker(self): - # GH21003 2018-04-23 + # GH21003 - code sample from 2020-04-23 is equivalent to this test df = DataFrame(np.random.random((7, 4))) # combining a color list and a marker letter should be allowed color = ["yellow", "red", "green", "blue"] ax = df.plot(color=color, style="d") output_colors = [line.get_color() for line in ax.lines] assert output_colors == color # each "line" is one of the four colors - # In the Github bug the color list was passed to MPL like this: + # Before this patch was introduced, the result was like this: # ax.lines[i].get_color() == ['yellow', 'red', 'green', 'blue'] + # which resulted in a ValueError when plt.draw() was called. def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) From 97ec52c9ac45d7c1f21ea3d9f4501350309133aa Mon Sep 17 00:00:00 2001 From: joooeey Date: Sun, 10 May 2020 13:40:43 +0200 Subject: [PATCH 08/15] addressed comments by jreback --- pandas/plotting/_matplotlib/core.py | 20 +++++++++----------- pandas/tests/plotting/test_frame.py | 12 ++++++------ 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 4e594fe961a3e..22b275e07b64d 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -38,6 +38,13 @@ table, ) +def _color_in_style(style: str) -> bool: + """ + Check if there is a color letter in the style string. + """ + from matplotlib.colors import BASE_COLORS + + return not set(BASE_COLORS).isdisjoint(style) class MPLPlot: """ @@ -224,22 +231,13 @@ def _validate_color_args(self): styles = [self.style] # need only a single match for s in styles: - if self._color_in_style(s): + if _color_in_style(s): raise ValueError( "Cannot pass 'style' string with a color symbol and " "'color' keyword argument. Please use one or the " "other or pass 'style' without a color symbol" ) - @staticmethod - def _color_in_style(style: str) -> bool: - """ - Check if there is a color letter in the style string. - """ - from matplotlib.colors import BASE_COLORS - - return not set(BASE_COLORS).isdisjoint(style) - def _iter_data(self, data=None, keep_index=False, fillna=None): if data is None: data = self.data @@ -729,7 +727,7 @@ def _apply_style_colors(self, colors, kwds, col_num, label): style = self.style has_color = "color" in kwds or self.colormap is not None - nocolor_style = style is None or not self._color_in_style(style) + nocolor_style = style is None or not _color_in_style(style) if (has_color or self.subplots) and nocolor_style: if isinstance(colors, dict): kwds["color"] = colors[label] diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 25fc7ba573a26..f60cf36ea189f 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -213,14 +213,14 @@ def test_color_and_marker(self): def test_color_list_and_marker(self): # GH21003 - code sample from 2020-04-23 is equivalent to this test df = DataFrame(np.random.random((7, 4))) - # combining a color list and a marker letter should be allowed - color = ["yellow", "red", "green", "blue"] - ax = df.plot(color=color, style="d") - output_colors = [line.get_color() for line in ax.lines] - assert output_colors == color # each "line" is one of the four colors - # Before this patch was introduced, the result was like this: + color_list = ["yellow", "red", "green", "blue"] + ax = df.plot(color=color_list, style="d") + # Before this patch was introduced, the previous line of code resulted + # in a plot where each individual line was assigned a list of colors: # ax.lines[i].get_color() == ['yellow', 'red', 'green', 'blue'] # which resulted in a ValueError when plt.draw() was called. + assert [line.get_color() for line in ax.lines] == color_list + def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) From 97ae5ae0bc0678c30ff7ba849a698503117dd59b Mon Sep 17 00:00:00 2001 From: joooeey Date: Sun, 10 May 2020 16:08:06 +0200 Subject: [PATCH 09/15] adjusted number of blank lines --- pandas/plotting/_matplotlib/core.py | 2 ++ pandas/tests/plotting/test_frame.py | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 134cf14eae5e9..15ed67e878195 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -38,6 +38,7 @@ table, ) + def _color_in_style(style: str) -> bool: """ Check if there is a color letter in the style string. @@ -46,6 +47,7 @@ def _color_in_style(style: str) -> bool: return not set(BASE_COLORS).isdisjoint(style) + class MPLPlot: """ Base class for assembling a pandas plot using matplotlib diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index f60cf36ea189f..754793f501896 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -221,7 +221,6 @@ def test_color_list_and_marker(self): # which resulted in a ValueError when plt.draw() was called. assert [line.get_color() for line in ax.lines] == color_list - def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) ax = df.plot() From 219d8d58874edc2f221e3d23966575ff61cf499a Mon Sep 17 00:00:00 2001 From: joooeey Date: Mon, 18 May 2020 21:05:46 +0200 Subject: [PATCH 10/15] assert lines are correct --- pandas/tests/plotting/test_frame.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 754793f501896..0b0a1a7dd71e7 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -207,19 +207,19 @@ def test_color_and_style_arguments(self): def test_color_and_marker(self): # GH21003 - code sample from 2018-05-10 is equivalent to this test df = DataFrame(np.random.random((7, 4))) - # combining a color string and a marker letter should be allowed - df.plot(color="green", style="d") # d for diamond + ax = df.plot(color="green", style="d") # d for diamond + for line in ax.lines: + assert line.get_color() == "green" + assert line.get_marker() == "d" def test_color_list_and_marker(self): # GH21003 - code sample from 2020-04-23 is equivalent to this test df = DataFrame(np.random.random((7, 4))) color_list = ["yellow", "red", "green", "blue"] - ax = df.plot(color=color_list, style="d") - # Before this patch was introduced, the previous line of code resulted - # in a plot where each individual line was assigned a list of colors: - # ax.lines[i].get_color() == ['yellow', 'red', 'green', 'blue'] - # which resulted in a ValueError when plt.draw() was called. + ax = df.plot(color=color_list, style="d") # d for diamond assert [line.get_color() for line in ax.lines] == color_list + for line in ax.lines: + assert line.get_marker() == "d" def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) From 18c1f2072c5e726e4048cd96c0fe643f406f46e1 Mon Sep 17 00:00:00 2001 From: joooeey Date: Tue, 19 May 2020 21:14:26 +0200 Subject: [PATCH 11/15] combined my tests into one --- pandas/tests/plotting/test_frame.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 0b0a1a7dd71e7..362b9516aa998 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -203,23 +203,18 @@ def test_color_and_style_arguments(self): # if there is a color symbol in the style strings: with pytest.raises(ValueError): df.plot(color=["red", "black"], style=["k-", "r--"]) + # k for black, r for red - def test_color_and_marker(self): - # GH21003 - code sample from 2018-05-10 is equivalent to this test - df = DataFrame(np.random.random((7, 4))) - ax = df.plot(color="green", style="d") # d for diamond - for line in ax.lines: - assert line.get_color() == "green" - assert line.get_marker() == "d" - - def test_color_list_and_marker(self): - # GH21003 - code sample from 2020-04-23 is equivalent to this test + @pytest.mark.parametrize( + "color", ['green', ['yellow', 'red', 'green', 'blue']]) + def test_color_and_marker(self, color): + # GH 21003 df = DataFrame(np.random.random((7, 4))) - color_list = ["yellow", "red", "green", "blue"] - ax = df.plot(color=color_list, style="d") # d for diamond - assert [line.get_color() for line in ax.lines] == color_list - for line in ax.lines: - assert line.get_marker() == "d" + ax = df.plot(color=color, style="d--") # d for diamond + green_line = ax.lines[2] + assert green_line.get_color() == 'green' + assert green_line.get_marker() == 'd' + assert green_line.get_linestyle() == '--' def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) From 2c45b97fb6580fe13c5973516849007b2d785315 Mon Sep 17 00:00:00 2001 From: joooeey Date: Sat, 11 Jul 2020 22:20:35 +0200 Subject: [PATCH 12/15] correct wrong merge conflict solution --- pandas/tests/plotting/test_series.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/test_series.py b/pandas/tests/plotting/test_series.py index 64da98f57676f..816d815837074 100644 --- a/pandas/tests/plotting/test_series.py +++ b/pandas/tests/plotting/test_series.py @@ -933,7 +933,7 @@ def test_plot_no_numeric_data(self): def test_style_single_ok(self): s = pd.Series([1, 2]) ax = s.plot(style="s", color="C3") - assert ax.lines[0].get_color() == ["C3"] + assert ax.lines[0].get_color() == "C3" @pytest.mark.parametrize( "index_name, old_label, new_label", From ca39eb590decbfe4480aa85bfb82b447e8af5742 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 5 Sep 2020 11:27:16 +0100 Subject: [PATCH 13/15] :pencil2: --- pandas/plotting/_matplotlib/core.py | 1 - pandas/tests/plotting/test_frame.py | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index d809341081111..def4a1dc3f5c4 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -208,7 +208,6 @@ def __init__( self._validate_color_args() def _validate_color_args(self): - if ( "color" in self.kwds and self.nseries == 1 diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index deea36cad260b..54d6a7e29d717 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -204,13 +204,12 @@ def test_color_and_style_arguments(self): # if there is a color symbol in the style strings: with pytest.raises(ValueError): df.plot(color=["red", "black"], style=["k-", "r--"]) - # k for black, r for red @pytest.mark.parametrize("color", ["green", ["yellow", "red", "green", "blue"]]) def test_color_and_marker(self, color): # GH 21003 df = DataFrame(np.random.random((7, 4))) - ax = df.plot(color=color, style="d--") # d for diamond + ax = df.plot(color=color, style="d--") green_line = ax.lines[2] assert green_line.get_color() == "green" assert green_line.get_marker() == "d" From 59ff00bc2f4a204b20bfc1365a45f606347c70be Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 5 Sep 2020 11:36:29 +0100 Subject: [PATCH 14/15] check all lines in test --- pandas/tests/plotting/test_frame.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 54d6a7e29d717..57f23485108c2 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -205,15 +205,25 @@ def test_color_and_style_arguments(self): with pytest.raises(ValueError): df.plot(color=["red", "black"], style=["k-", "r--"]) - @pytest.mark.parametrize("color", ["green", ["yellow", "red", "green", "blue"]]) - def test_color_and_marker(self, color): + @pytest.mark.parametrize( + "color, expected", + [ + ("green", ["green"] * 4), + (["yellow", "red", "green", "blue"], ["yellow", "red", "green", "blue"]), + ], + ) + def test_color_and_marker(self, color, expected): # GH 21003 df = DataFrame(np.random.random((7, 4))) ax = df.plot(color=color, style="d--") - green_line = ax.lines[2] - assert green_line.get_color() == "green" - assert green_line.get_marker() == "d" - assert green_line.get_linestyle() == "--" + + # check colors + result = [i.get_color() for i in ax.lines] + assert result == expected + + # check markers and linestyles + assert all(i.get_linestyle() == "--" for i in ax.lines) + assert all(i.get_marker() == "d" for i in ax.lines) def test_nonnumeric_exclude(self): df = DataFrame({"A": ["x", "y", "z"], "B": [1, 2, 3]}) From 5d63c46d44ba2074289cff20c5524e6ed732ddab Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Sat, 5 Sep 2020 11:36:53 +0100 Subject: [PATCH 15/15] :art: --- pandas/tests/plotting/test_frame.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/tests/plotting/test_frame.py b/pandas/tests/plotting/test_frame.py index 57f23485108c2..56c7cb4785668 100644 --- a/pandas/tests/plotting/test_frame.py +++ b/pandas/tests/plotting/test_frame.py @@ -216,11 +216,9 @@ def test_color_and_marker(self, color, expected): # GH 21003 df = DataFrame(np.random.random((7, 4))) ax = df.plot(color=color, style="d--") - # check colors result = [i.get_color() for i in ax.lines] assert result == expected - # check markers and linestyles assert all(i.get_linestyle() == "--" for i in ax.lines) assert all(i.get_marker() == "d" for i in ax.lines)