Skip to content
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

facets and hue with hist #4868

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
54 changes: 49 additions & 5 deletions xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from ..core.formatting import format_item
from .utils import (
_infer_hist_labels,
_infer_xy_labels,
_process_cmap_cbar_kwargs,
import_matplotlib_pyplot,
Expand Down Expand Up @@ -285,10 +286,46 @@ def map_dataarray(self, func, x, y, **kwargs):

return self

def map_dataarray_hist(self, func, hue, add_legend=True, add_labels=None, **kwargs):
from .plot import _infer_1d_data

for d, ax in zip(self.name_dicts.flat, self.axes.flat):
# None is the sentinel value
if d is not None:
subset = self.data.loc[d]
mappable = func(
subset,
ax=ax,
hue=hue,
add_legend=False,
add_labels=False,
**kwargs,
)
self._mappables.append(mappable[-1])

_, yplt, hueplt, huelabel = _infer_1d_data(
darray=self.data.loc[self.name_dicts.flat[0]],
x=None,
y=None,
hue=hue,
funcname="hist",
)

xlabel, ylabel = _infer_hist_labels(yplt, kwargs)

self._hue_var = hueplt
self._hue_label = huelabel
self._finalize_grid(xlabel, ylabel)

if add_legend and hueplt is not None and huelabel is not None:
self.add_legend()

return self

def map_dataarray_line(
self, func, x, y, hue, add_legend=True, _labels=None, **kwargs
self, func, x, y, hue, add_legend=True, add_labels=None, **kwargs
):
from .plot import _infer_line_data
from .plot import _infer_1d_data

for d, ax in zip(self.name_dicts.flat, self.axes.flat):
# None is the sentinel value
Expand All @@ -301,13 +338,17 @@ def map_dataarray_line(
ax=ax,
hue=hue,
add_legend=False,
_labels=False,
add_labels=False,
**kwargs,
)
self._mappables.append(mappable)

xplt, yplt, hueplt, huelabel = _infer_line_data(
darray=self.data.loc[self.name_dicts.flat[0]], x=x, y=y, hue=hue
xplt, yplt, hueplt, huelabel = _infer_1d_data(
darray=self.data.loc[self.name_dicts.flat[0]],
x=x,
y=y,
hue=hue,
funcname="line",
)
xlabel = label_from_attrs(xplt)
ylabel = label_from_attrs(yplt)
Expand Down Expand Up @@ -641,6 +682,9 @@ def _easy_facetgrid(
subplot_kws=subplot_kws,
)

if kind == "hist":
return g.map_dataarray_hist(plotfunc, **kwargs)

if kind == "line":
return g.map_dataarray_line(plotfunc, x, y, **kwargs)

Expand Down
Loading