Skip to content

Commit b7fadae

Browse files
committed
fix passing colormap in GroupByPlot
1 parent e209a35 commit b7fadae

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

pandas/core/groupby/groupby.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,30 @@ def __init__(self, groupby: GroupBy) -> None:
434434
self._groupby = groupby
435435

436436
def __call__(self, *args, **kwargs):
437-
def f(self):
438-
return self.plot(*args, **kwargs)
439-
440-
f.__name__ = "plot"
441-
return self._groupby._python_apply_general(f, self._groupby._selected_obj)
437+
# Patch: assign a unique color from colormap to each group if colormap is given and color is not
438+
colormap = kwargs.get("colormap", None)
439+
color = kwargs.get("color", None)
440+
if colormap is not None and color is None:
441+
from pandas.plotting._matplotlib.style import get_standard_colors
442+
group_keys = list(self._groupby.groups.keys())
443+
colors = get_standard_colors(
444+
num_colors=len(group_keys),
445+
colormap=colormap,
446+
color_type="default",
447+
)
448+
kwargs = dict(kwargs)
449+
kwargs.pop("colormap", None)
450+
def f(group, color, label):
451+
return group.plot(*args, color=color, label=label, **kwargs)
452+
results = []
453+
for i, (name, group) in enumerate(self._groupby):
454+
results.append(f(group, colors[i], name))
455+
return results
456+
else:
457+
def f(self):
458+
return self.plot(*args, **kwargs)
459+
f.__name__ = "plot"
460+
return self._groupby._python_apply_general(f, self._groupby._selected_obj)
442461

443462
def __getattr__(self, name: str):
444463
def attr(*args, **kwargs):

0 commit comments

Comments
 (0)