Skip to content

Conversation

zhuoyahuang
Copy link

@zhuoyahuang zhuoyahuang commented Oct 12, 2025

Following screenshot is from running

chunk = pd.DataFrame(index=[7, 8, 2])
df = pd.concat([chunk.assign(y=i, i=i) for i in range(10)])
df.groupby("i")["y"].plot(legend=True, colormap="Pastel1")
image

@zhuoyahuang zhuoyahuang changed the title BUG: SeriesGroupBy.plot does not respect cmap parameter BUG: fix SeriesGroupBy.plot does not respect cmap parameter Oct 12, 2025
@zhuoyahuang zhuoyahuang marked this pull request as ready for review October 12, 2025 19:05
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Can you also add a test.

Comment on lines +450 to +456
def f(obj, color=None, label=None):
plot_kwargs = dict(kwargs)
if color is not None:
plot_kwargs["color"] = color
if label is not None:
plot_kwargs["label"] = label
return obj.plot(*args, **plot_kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the same as the function f below; can you move to the top and just define once.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also rename to something meaningful; perhaps plot_group?

Comment on lines +438 to +439
if colormap is not None and color is None:
from pandas.plotting._matplotlib.style import get_standard_colors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should only do this when get_option("plotting.backend") == "matplotlib". Can you use the previous logic for any other backend.

colormap=colormap,
color_type="default",
)
kwargs = dict(kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Comment on lines +459 to +460
for i, (name, group) in enumerate(self._groupby):
results.append(f(group, colors[i], name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i, (name, group) in enumerate(self._groupby):
results.append(f(group, colors[i], name))
for name, group, color in zip(self._groupby, colors):
results.append(f(group, color, name))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: SeriesGroupBy.plot does not respect cmap parameter

3 participants