Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix Ability to set both color and style in pandas plotting #59574

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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