Skip to content

Commit

Permalink
BUG: Fix Ability to set both color and style in pandas plotting (#59574)
Browse files Browse the repository at this point in the history
* BUG: Fix plotting set color style dict sametime

* BUG: chore pre-commit inconsistent-namespace-usage

* BUG: Chore Conditional block

* Update doc/source/whatsnew/v3.0.0.rst

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>

* BUG: Chore pre-commit sort whatsnew entries alphabetically

---------

Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com>
  • Loading branch information
AntiKnot and mroeschke authored Aug 22, 2024
1 parent 9bcc563 commit 385ce23
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ 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`)

Expand Down
4 changes: 3 additions & 1 deletion pandas/plotting/_matplotlib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ 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]
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/plotting/frame/test_frame_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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)

Expand Down

0 comments on commit 385ce23

Please sign in to comment.