Skip to content

Commit 385ce23

Browse files
AntiKnotmroeschke
andauthored
BUG: Fix Ability to set both color and style in pandas plotting (#59574)
* 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 <[email protected]> * BUG: Chore pre-commit sort whatsnew entries alphabetically --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 9bcc563 commit 385ce23

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ Period
632632
Plotting
633633
^^^^^^^^
634634
- Bug in :meth:`.DataFrameGroupBy.boxplot` failed when there were multiple groupings (:issue:`14701`)
635+
- Bug in :meth:`DataFrame.plot.line` raising ``ValueError`` when set both color and a ``dict`` style (:issue:`59461`)
635636
- Bug in :meth:`DataFrame.plot` that causes a shift to the right when the frequency multiplier is greater than one. (:issue:`57587`)
636637
- Bug in :meth:`Series.plot` with ``kind="pie"`` with :class:`ArrowDtype` (:issue:`59192`)
637638

pandas/plotting/_matplotlib/core.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,9 @@ def _validate_color_args(self, color, colormap):
451451
)
452452

453453
if self.style is not None:
454-
if is_list_like(self.style):
454+
if isinstance(self.style, dict):
455+
styles = [self.style[col] for col in self.columns if col in self.style]
456+
elif is_list_like(self.style):
455457
styles = self.style
456458
else:
457459
styles = [self.style]

pandas/tests/plotting/frame/test_frame_color.py

+12
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,18 @@ def test_color_and_marker(self, color, expected):
9595
assert all(i.get_linestyle() == "--" for i in ax.lines)
9696
assert all(i.get_marker() == "d" for i in ax.lines)
9797

98+
def test_color_and_style(self):
99+
color = {"g": "black", "h": "brown"}
100+
style = {"g": "-", "h": "--"}
101+
expected_color = ["black", "brown"]
102+
expected_style = ["-", "--"]
103+
df = DataFrame({"g": [1, 2], "h": [2, 3]}, index=[1, 2])
104+
ax = df.plot.line(color=color, style=style)
105+
color = [i.get_color() for i in ax.lines]
106+
style = [i.get_linestyle() for i in ax.lines]
107+
assert color == expected_color
108+
assert style == expected_style
109+
98110
def test_bar_colors(self):
99111
default_colors = _unpack_cycler(plt.rcParams)
100112

0 commit comments

Comments
 (0)