From 4052620528d67e8d59a94ab0b583a92dff3516d3 Mon Sep 17 00:00:00 2001 From: Yansy Date: Wed, 21 Aug 2024 17:21:46 +0800 Subject: [PATCH 1/5] BUG: Fix plotting set color style dict sametime --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/plotting/_matplotlib/core.py | 2 ++ pandas/tests/plotting/frame/test_frame_color.py | 12 ++++++++++++ 3 files changed, 15 insertions(+) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index bdeb9c48990a2..1bc983083a07c 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -634,6 +634,7 @@ Plotting - Bug in :meth:`.DataFrameGroupBy.boxplot` failed when there were multiple groupings (:issue:`14701`) - Bug in :meth:`DataFrame.plot` that causes a shift to the right when the frequency multiplier is greater than one. (:issue:`57587`) - Bug in :meth:`Series.plot` with ``kind="pie"`` with :class:`ArrowDtype` (:issue:`59192`) +- Bug in :meth:`core._validate_color_args` raising ``ValueError`` when set both color and style in plt.plot.line (:issue:`59461`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 9a7e563332a42..23a41063517c4 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -455,6 +455,8 @@ def _validate_color_args(self, color, colormap): styles = self.style else: styles = [self.style] + if isinstance(self.style, dict): + styles = [self.style[col] for col in self.columns if col in self.style] # need only a single match for s in styles: if _color_in_style(s): diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index 4b35e896e1a6c..3150a26cd3ca6 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -95,6 +95,18 @@ def test_color_and_marker(self, color, expected): assert all(i.get_linestyle() == "--" for i in ax.lines) assert all(i.get_marker() == "d" for i in ax.lines) + def test_color_and_style(self): + color = {"g": "black", "h": "brown"} + style = {"g": "-", "h": "--"} + expected_color = ["black", "brown"] + expected_style = ["-", "--"] + df = pd.DataFrame({"g": [1, 2], "h": [2, 3]}, index=[1, 2]) + ax = df.plot.line(color=color, style=style) + color = [i.get_color() for i in ax.lines] + style = [i.get_linestyle() for i in ax.lines] + assert color == expected_color + assert style == expected_style + def test_bar_colors(self): default_colors = _unpack_cycler(plt.rcParams) From 50b0c3351b3bf281bfd33b29f2521dde0f41d80a Mon Sep 17 00:00:00 2001 From: Yansy Date: Wed, 21 Aug 2024 17:37:42 +0800 Subject: [PATCH 2/5] BUG: chore pre-commit inconsistent-namespace-usage --- pandas/tests/plotting/frame/test_frame_color.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index 3150a26cd3ca6..ce198fbff1185 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -100,7 +100,7 @@ def test_color_and_style(self): style = {"g": "-", "h": "--"} expected_color = ["black", "brown"] expected_style = ["-", "--"] - df = pd.DataFrame({"g": [1, 2], "h": [2, 3]}, index=[1, 2]) + df = DataFrame({"g": [1, 2], "h": [2, 3]}, index=[1, 2]) ax = df.plot.line(color=color, style=style) color = [i.get_color() for i in ax.lines] style = [i.get_linestyle() for i in ax.lines] From da34d4691805e9d2f7d0f394fb085fd46ff733fc Mon Sep 17 00:00:00 2001 From: Yansy Date: Thu, 22 Aug 2024 09:11:06 +0800 Subject: [PATCH 3/5] BUG: Chore Conditional block --- pandas/plotting/_matplotlib/core.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/plotting/_matplotlib/core.py b/pandas/plotting/_matplotlib/core.py index 23a41063517c4..ed24e246c5079 100644 --- a/pandas/plotting/_matplotlib/core.py +++ b/pandas/plotting/_matplotlib/core.py @@ -451,12 +451,12 @@ def _validate_color_args(self, color, colormap): ) if self.style is not None: - if is_list_like(self.style): + if isinstance(self.style, dict): + styles = [self.style[col] for col in self.columns if col in self.style] + elif is_list_like(self.style): styles = self.style else: styles = [self.style] - if isinstance(self.style, dict): - styles = [self.style[col] for col in self.columns if col in self.style] # need only a single match for s in styles: if _color_in_style(s): From 87fe105b168e5102c0a725c80efb8aab811ad7f2 Mon Sep 17 00:00:00 2001 From: "xxx.Yan" Date: Thu, 22 Aug 2024 09:11:31 +0800 Subject: [PATCH 4/5] Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 1bc983083a07c..e684b709658b7 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -634,7 +634,7 @@ Plotting - Bug in :meth:`.DataFrameGroupBy.boxplot` failed when there were multiple groupings (:issue:`14701`) - Bug in :meth:`DataFrame.plot` that causes a shift to the right when the frequency multiplier is greater than one. (:issue:`57587`) - Bug in :meth:`Series.plot` with ``kind="pie"`` with :class:`ArrowDtype` (:issue:`59192`) -- Bug in :meth:`core._validate_color_args` raising ``ValueError`` when set both color and style in plt.plot.line (:issue:`59461`) +- Bug in :meth:`DataFrame.plot.line` raising ``ValueError`` when set both color and a ``dict`` style (:issue:`59461`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^ From 43ff2f91ebda33bc60c29421ec1bba6f630a4b08 Mon Sep 17 00:00:00 2001 From: Yansy Date: Thu, 22 Aug 2024 09:27:49 +0800 Subject: [PATCH 5/5] BUG: Chore pre-commit sort whatsnew entries alphabetically --- doc/source/whatsnew/v3.0.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index e684b709658b7..a9049ac9a15b4 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -632,9 +632,9 @@ Period Plotting ^^^^^^^^ - Bug in :meth:`.DataFrameGroupBy.boxplot` failed when there were multiple groupings (:issue:`14701`) +- Bug in :meth:`DataFrame.plot.line` raising ``ValueError`` when set both color and a ``dict`` style (:issue:`59461`) - Bug in :meth:`DataFrame.plot` that causes a shift to the right when the frequency multiplier is greater than one. (:issue:`57587`) - Bug in :meth:`Series.plot` with ``kind="pie"`` with :class:`ArrowDtype` (:issue:`59192`) -- Bug in :meth:`DataFrame.plot.line` raising ``ValueError`` when set both color and a ``dict`` style (:issue:`59461`) Groupby/resample/rolling ^^^^^^^^^^^^^^^^^^^^^^^^