Skip to content

Commit fb07a76

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

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

pandas/core/groupby/groupby.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ class providing the base-class of operations.
109109
SparseArray,
110110
)
111111
from pandas.core.arrays.string_ import StringDtype
112-
from pandas.core.arrays.string_arrow import (
113-
ArrowStringArray,
114-
)
112+
from pandas.core.arrays.string_arrow import ArrowStringArray
115113
from pandas.core.base import (
116114
PandasObject,
117115
SelectionMixin,
@@ -434,11 +432,30 @@ def __init__(self, groupby: GroupBy) -> None:
434432
self._groupby = groupby
435433

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

443460
def __getattr__(self, name: str):
444461
def attr(*args, **kwargs):

0 commit comments

Comments
 (0)