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

plotting facet grid with singleton dimension should create a facet grid with size 1 #7552

Closed
liorella-qm opened this issue Feb 23, 2023 · 5 comments · Fixed by #8174
Closed

Comments

@liorella-qm
Copy link

What is your issue?

In my application, I want to plot facet grids of datasets, and the dimension I want to facet sometimes has a length of 1 and sometimes more than 1. When the length of the dimension is > 1, the plot(col=....) method of DataArray works as expected. However, when it is 1, I get an error and my code fails, although it would have been natrual for the plot method to create a FacetGrid with length of one facet.

As a minimal example, this code:

import xarray as xr
da = xr.DataArray(np.random.rand(3,1), dims=('x', 'y'))
da.plot(col='y')

fails with an error:

KeyError: 'y'

This example works as expected:

import xarray as xr
da = xr.DataArray(np.random.rand(3,2), dims=('x', 'y'))
da.plot(col='y')
@liorella-qm liorella-qm added the needs triage Issue that has not been reviewed by xarray team member label Feb 23, 2023
@headtr1ck headtr1ck added enhancement topic-plotting and removed needs triage Issue that has not been reviewed by xarray team member labels Feb 24, 2023
@headtr1ck
Copy link
Collaborator

headtr1ck commented Feb 27, 2023

This is caused by the squeeze() here:

darray = darray.squeeze().compute()

@Illviljan do you know why this is required? I assume you want to remove singleton dimensions to better choose the correct plot method?

Maybe we should exclude col, row dims from the squeeze?
something like:
darray.squeeze(d for d, s in darray.sizes.items() if s == 1 and d not in (row, col, hue))

@headtr1ck
Copy link
Collaborator

@liorella-qm in the meantime you can use da.plot.line(col='y') as a workaround

@dcherian
Copy link
Contributor

See #620 (!)

Maybe we should exclude col, row dims from the squeeze?

This seems sensible.

@akanshajais

This comment was marked as off-topic.

@krokosik
Copy link
Contributor

Since I didn't see anyone objecting to the solution provided by @headtr1ck and I also found this behaviour annoying, I hope that nobody minds that I created a PR with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants