Skip to content

Filter null values before plotting #8535

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 7 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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://github.com/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