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

Unexpected empty spots in xarray quiver plot with hue of masked data #8928

Open
5 tasks done
markusReinert opened this issue Apr 11, 2024 · 1 comment
Open
5 tasks done

Comments

@markusReinert
Copy link

What happened?

When creating a quiver plot of an xarray Dataset using ds.plot.quiver, there are empty spots in the wrong places. This problem only seems to occur when a hue array is given to quiver and the data is masked, i.e., there are NaN values in the array. The empty spots are not only in the masked locations, but seem to appear in arbitrary locations.

What did you expect to happen?

Plotting the same data with the matplotlib function plt.quiver, the empty spots are not there, no matter if there is a hue (called color in matplotlib) or NaN values. This is the result I expected, and I expect that the xarray plot method gives the same result.

Minimal Complete Verifiable Example

import xarray as xr
import matplotlib.pyplot as plt

ds = xr.Dataset(coords={"x": range(20), "y": range(15)})
ds["c"] = xr.ones_like(xr.broadcast(ds.y, ds.x)[0])

# Mask out the central band
# When the next line is commented out, the problem does not occur
ds["c"] = ds.c.where((ds.x - 10)**2 > 1)

fig, axs = plt.subplots(2, 2, sharex=True, sharey=True)

# Plot the data with the xarray method
ds.plot.quiver("x", "y", "c", "c", add_guide=False, ax=axs[0, 0])  # this works as expected
ds.plot.quiver("x", "y", "c", "c", "c", add_guide=False, ax=axs[0, 1])  # this does not

# Make the same plot directly with matplotlib to see the expected result
axs[1, 0].quiver(ds.x, ds.y, ds.c, ds.c)  # this works as expected
axs[1, 1].quiver(ds.x, ds.y, ds.c, ds.c, ds.c)  # this works, too

axs[0, 0].set_title("xarray plot, no hue")
axs[0, 1].set_title("xarray plot, with hue")
axs[1, 0].set_title("manual plot, no color")
axs[1, 1].set_title("manual plot, with color")
for ax in axs.flatten():
    ax.set(xlabel="", ylabel="")

plt.show()

MVCE confirmation

  • Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • Complete example — the example is self-contained, including all data and the text of any traceback.
  • Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • New issue — a search of GitHub Issues suggests this is not a duplicate.
  • Recent environment — the issue occurs with the latest version of xarray and its dependencies.

Relevant log output

No response

Anything else we need to know?

This is the figure that is created with the example code. The problems are the empty spots in the top right panel. I expected this panel to look like the bottom right panel.
The left column shows that there are no problems when there is no hue/color argument. The quiver plot created with xarray looks just like the one created manually with matplotlib.

output

Environment

INSTALLED VERSIONS

commit: None
python: 3.8.10 (default, Nov 22 2023, 10:22:35)
[GCC 9.4.0]
python-bits: 64
OS: Linux
OS-release: 5.4.0-174-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: ('en_GB', 'UTF-8')
libhdf5: 1.12.0
libnetcdf: 4.7.4

xarray: 2022.12.0
pandas: 1.3.0
numpy: 1.23.5
scipy: 1.9.3
netCDF4: 1.5.8
pydap: None
h5netcdf: 0.7.1
h5py: 2.10.0
Nio: None
zarr: 2.11.3
cftime: 1.5.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: 0.9.10.1
iris: None
bottleneck: 1.2.1
dask: 2022.05.0
distributed: None
matplotlib: 3.6.2
cartopy: 0.21.1
seaborn: 0.13.0
numbagg: None
fsspec: 2022.3.0
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: 45.2.0
pip: 23.2.1
conda: None
pytest: 4.6.9
mypy: None
IPython: 8.12.3
sphinx: 3.5.3

@markusReinert markusReinert added bug needs triage Issue that has not been reviewed by xarray team member labels Apr 11, 2024
Copy link

welcome bot commented Apr 11, 2024

Thanks for opening your first issue here at xarray! Be sure to follow the issue template!
If you have an idea for a solution, we would really welcome a Pull Request with proposed changes.
See the Contributing Guide for more.
It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better.
Thank you!

@dcherian dcherian added topic-plotting and removed needs triage Issue that has not been reviewed by xarray team member labels Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants