Skip to content
Open
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
46 changes: 39 additions & 7 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ class providing the base-class of operations.
SparseArray,
)
from pandas.core.arrays.string_ import StringDtype
from pandas.core.arrays.string_arrow import (
ArrowStringArray,
)
from pandas.core.arrays.string_arrow import ArrowStringArray
from pandas.core.base import (
PandasObject,
SelectionMixin,
Expand Down Expand Up @@ -434,11 +432,45 @@ def __init__(self, groupby: GroupBy) -> None:
self._groupby = groupby

def __call__(self, *args, **kwargs):
def f(self):
return self.plot(*args, **kwargs)
# Patch: assign a unique color from colormap to each group
colormap = kwargs.get("colormap", None)
color = kwargs.get("color", None)
if colormap is not None and color is None:
from pandas.plotting._matplotlib.style import get_standard_colors
Comment on lines +438 to +439
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.


group_keys = list(self._groupby.groups.keys())
colors = get_standard_colors(
num_colors=len(group_keys),
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?

kwargs.pop("colormap", None)

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)
Comment on lines +450 to +456
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?


results = []
for i, (name, group) in enumerate(self._groupby):
results.append(f(group, colors[i], name))
Comment on lines +459 to +460
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))

return results
else:

f.__name__ = "plot"
return self._groupby._python_apply_general(f, self._groupby._selected_obj)
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)

f.__name__ = "plot"
return self._groupby._python_apply_general(f, self._groupby._selected_obj)

def __getattr__(self, name: str):
def attr(*args, **kwargs):
Expand Down
Loading