-
-
Notifications
You must be signed in to change notification settings - Fork 19.2k
BUG: fix SeriesGroupBy.plot does not respect cmap parameter #62671
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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, | ||||||||||
|
@@ -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 | ||||||||||
|
||||||||||
group_keys = list(self._groupby.groups.keys()) | ||||||||||
colors = get_standard_colors( | ||||||||||
num_colors=len(group_keys), | ||||||||||
colormap=colormap, | ||||||||||
color_type="default", | ||||||||||
) | ||||||||||
kwargs = dict(kwargs) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is the same as the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also rename to something meaningful; perhaps |
||||||||||
|
||||||||||
results = [] | ||||||||||
for i, (name, group) in enumerate(self._groupby): | ||||||||||
results.append(f(group, colors[i], name)) | ||||||||||
Comment on lines
+459
to
+460
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
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): | ||||||||||
|
There was a problem hiding this comment.
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.