Skip to content

Commit

Permalink
Filter null values before plotting (#8535)
Browse files Browse the repository at this point in the history
* Remove nulls when plotting.

* Update test_plot.py

* Update test_plot.py

* Update whats-new.rst

* Update test_plot.py

* only filter on scatter plots as that is safe.
  • Loading branch information
Illviljan authored Dec 13, 2023
1 parent c53c400 commit 2971994
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Documentation
Internal Changes
~~~~~~~~~~~~~~~~

- Remove null values before plotting. (:pull:`8535`).
By `Jimmy Westling <https:/illviljan>`_.

.. _whats-new.2023.12.0:

Expand Down
6 changes: 6 additions & 0 deletions xarray/plot/dataarray_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,12 @@ def newplotfunc(
if plotfunc.__name__ == "scatter":
size_ = kwargs.pop("_size", markersize)
size_r = _MARKERSIZE_RANGE

# Remove any nulls, .where(m, drop=True) doesn't work when m is
# a dask array, so load the array to memory.
# It will have to be loaded to memory at some point anyway:
darray = darray.load()
darray = darray.where(darray.notnull(), drop=True)
else:
size_ = kwargs.pop("_size", linewidth)
size_r = _LINEWIDTH_RANGE
Expand Down
13 changes: 13 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3372,3 +3372,16 @@ def test_plot1d_default_rcparams() -> None:
np.testing.assert_allclose(
ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("k")
)


@requires_matplotlib
def test_plot1d_filtered_nulls() -> None:
ds = xr.tutorial.scatter_example_dataset(seed=42)
y = ds.y.where(ds.y > 0.2)
expected = y.notnull().sum().item()

with figure_context():
pc = y.plot.scatter()
actual = pc.get_offsets().shape[0]

assert expected == actual

0 comments on commit 2971994

Please sign in to comment.