Skip to content

Use .to_numpy() for quantified facetgrids #5886

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

Merged
merged 10 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 7 additions & 7 deletions xarray/plot/facetgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def __init__(
)

# Set up the lists of names for the row and column facet variables
col_names = list(data[col].values) if col else []
row_names = list(data[row].values) if row else []
col_names = list(data[col].to_numpy()) if col else []
row_names = list(data[row].to_numpy()) if row else []

if single_group:
full = [{single_group: x} for x in data[single_group].values]
full = [{single_group: x} for x in data[single_group].to_numpy()]
empty = [None for x in range(nrow * ncol - len(full))]
name_dicts = full + empty
else:
Expand Down Expand Up @@ -253,7 +253,7 @@ def map_dataarray(self, func, x, y, **kwargs):
raise ValueError("cbar_ax not supported by FacetGrid.")

cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
func, self.data.values, **kwargs
func, self.data.to_numpy(), **kwargs
)

self._cmap_extend = cmap_params.get("extend")
Expand Down Expand Up @@ -349,7 +349,7 @@ def map_dataset(

if hue and meta_data["hue_style"] == "continuous":
cmap_params, cbar_kwargs = _process_cmap_cbar_kwargs(
func, self.data[hue].values, **kwargs
func, self.data[hue].to_numpy(), **kwargs
)
kwargs["meta_data"]["cmap_params"] = cmap_params
kwargs["meta_data"]["cbar_kwargs"] = cbar_kwargs
Expand Down Expand Up @@ -425,7 +425,7 @@ def _adjust_fig_for_guide(self, guide):
def add_legend(self, **kwargs):
self.figlegend = self.fig.legend(
handles=self._mappables[-1],
labels=list(self._hue_var.values),
labels=list(self._hue_var.to_numpy()),
title=self._hue_label,
loc="center right",
**kwargs,
Expand Down Expand Up @@ -625,7 +625,7 @@ def map(self, func, *args, **kwargs):
if namedict is not None:
data = self.data.loc[namedict]
plt.sca(ax)
innerargs = [data[a].values for a in args]
innerargs = [data[a].to_numpy() for a in args]
maybe_mappable = func(*innerargs, **kwargs)
# TODO: better way to verify that an artist is mappable?
# https://stackoverflow.com/questions/33023036/is-it-possible-to-detect-if-a-matplotlib-artist-is-a-mappable-suitable-for-use-w#33023522
Expand Down
11 changes: 6 additions & 5 deletions xarray/plot/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,10 +1152,6 @@ def newplotfunc(
else:
dims = (yval.dims[0], xval.dims[0])

# better to pass the ndarrays directly to plotting functions
xval = xval.to_numpy()
yval = yval.to_numpy()

# May need to transpose for correct x, y labels
# xlab may be the name of a coord, we have to check for dim names
if imshow_rgb:
Expand All @@ -1168,8 +1164,13 @@ def newplotfunc(
if dims != darray.dims:
darray = darray.transpose(*dims, transpose_coords=True)

# better to pass the ndarrays directly to plotting functions
xval = xval.to_numpy()
yval = yval.to_numpy()
zarray = darray.as_numpy()

# Pass the data as a masked ndarray too
zval = darray.to_masked_array(copy=False)
zval = zarray.to_masked_array(copy=False)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure what needs to be done here with regards to converting between quantified arrays and masked arrays

Copy link
Member Author

@TomNicholas TomNicholas Oct 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think it's already been done in this previous PR, which means .to_masked_array already dequantifies

https://github.com/pydata/xarray/pull/5561/files#diff-386f35dda14ca12d315f2eb71f0ab35d7fdc73419b5dc5175ea5720f3206903bR2787

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... which means that calling .as_numpy() and then also calling .to_masked_array() unneccessarily repeats calling .values, so I got rid of the former.


# Replace pd.Intervals if contained in xval or yval.
xplt, xlab_extra = _resolve_intervals_2dplot(xval, plotfunc.__name__)
Expand Down