Skip to content

Commit 2971994

Browse files
authored
Filter null values before plotting (#8535)
* 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.
1 parent c53c400 commit 2971994

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/whats-new.rst

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Documentation
4646
Internal Changes
4747
~~~~~~~~~~~~~~~~
4848

49+
- Remove null values before plotting. (:pull:`8535`).
50+
By `Jimmy Westling <https://github.com/illviljan>`_.
4951

5052
.. _whats-new.2023.12.0:
5153

xarray/plot/dataarray_plot.py

+6
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,12 @@ def newplotfunc(
944944
if plotfunc.__name__ == "scatter":
945945
size_ = kwargs.pop("_size", markersize)
946946
size_r = _MARKERSIZE_RANGE
947+
948+
# Remove any nulls, .where(m, drop=True) doesn't work when m is
949+
# a dask array, so load the array to memory.
950+
# It will have to be loaded to memory at some point anyway:
951+
darray = darray.load()
952+
darray = darray.where(darray.notnull(), drop=True)
947953
else:
948954
size_ = kwargs.pop("_size", linewidth)
949955
size_r = _LINEWIDTH_RANGE

xarray/tests/test_plot.py

+13
Original file line numberDiff line numberDiff line change
@@ -3372,3 +3372,16 @@ def test_plot1d_default_rcparams() -> None:
33723372
np.testing.assert_allclose(
33733373
ax.collections[0].get_edgecolor(), mpl.colors.to_rgba_array("k")
33743374
)
3375+
3376+
3377+
@requires_matplotlib
3378+
def test_plot1d_filtered_nulls() -> None:
3379+
ds = xr.tutorial.scatter_example_dataset(seed=42)
3380+
y = ds.y.where(ds.y > 0.2)
3381+
expected = y.notnull().sum().item()
3382+
3383+
with figure_context():
3384+
pc = y.plot.scatter()
3385+
actual = pc.get_offsets().shape[0]
3386+
3387+
assert expected == actual

0 commit comments

Comments
 (0)