Skip to content

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

Open
@markusReinert

Description

@markusReinert

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions