diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 136049d9300..36c84e24ee5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -61,6 +61,8 @@ Bug fixes now reopens the file from scratch for h5netcdf and scipy netCDF backends, rather than reusing a cached version (:issue:`4240`, :issue:`4862`). By `Stephan Hoyer `_. +- Raise a TypeError when trying to plot empty data (:issue:`7156`, :pull:`7228`). + By `Michael Niklas `_. Documentation ~~~~~~~~~~~~~ diff --git a/xarray/plot/dataarray_plot.py b/xarray/plot/dataarray_plot.py index ae22f23fec4..ac23f7dc96d 100644 --- a/xarray/plot/dataarray_plot.py +++ b/xarray/plot/dataarray_plot.py @@ -280,7 +280,10 @@ def plot( ndims = len(plot_dims) plotfunc: Callable - if ndims in [1, 2]: + + if ndims == 0 or darray.size == 0: + raise TypeError("No numeric data to plot.") + if ndims in (1, 2): if row or col: kwargs["subplot_kws"] = subplot_kws kwargs["row"] = row @@ -483,6 +486,9 @@ def line( return _easy_facetgrid(darray, line, kind="line", **allargs) ndims = len(darray.dims) + if ndims == 0 or darray.size == 0: + # TypeError to be consistent with pandas + raise TypeError("No numeric data to plot.") if ndims > 2: raise ValueError( "Line plots are for 1- or 2-dimensional DataArrays. " @@ -699,6 +705,10 @@ def hist( """ assert len(args) == 0 + if darray.ndim == 0 or darray.size == 0: + # TypeError to be consistent with pandas + raise TypeError("No numeric data to plot.") + ax = get_axis(figsize, size, aspect, ax) no_nan = np.ravel(darray.to_numpy()) @@ -899,6 +909,10 @@ def newplotfunc( return _easy_facetgrid(darray, kind="plot1d", **allargs) + if darray.ndim == 0 or darray.size == 0: + # TypeError to be consistent with pandas + raise TypeError("No numeric data to plot.") + # The allargs dict passed to _easy_facetgrid above contains args if args == (): args = kwargs.pop("args", ()) @@ -1496,6 +1510,10 @@ def newplotfunc( allargs["plotfunc"] = globals()[plotfunc.__name__] return _easy_facetgrid(darray, kind="dataarray", **allargs) + if darray.ndim == 0 or darray.size == 0: + # TypeError to be consistent with pandas + raise TypeError("No numeric data to plot.") + plt = import_matplotlib_pyplot() if ( diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 7e1c8a0b330..01f616f92ba 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -3180,6 +3180,31 @@ def test_assert_valid_xy() -> None: _assert_valid_xy(darray=darray, xy="error_now", name="x") +@requires_matplotlib +@pytest.mark.parametrize( + "val", [pytest.param([], id="empty"), pytest.param(0, id="scalar")] +) +@pytest.mark.parametrize( + "method", + [ + "__call__", + "line", + "step", + "contour", + "contourf", + "hist", + "imshow", + "pcolormesh", + "scatter", + "surface", + ], +) +def test_plot_empty_raises(val: list | float, method: str) -> None: + da = xr.DataArray(val) + with pytest.raises(TypeError, match="No numeric data"): + getattr(da.plot, method)() + + @requires_matplotlib def test_facetgrid_axes_raises_deprecation_warning(): with pytest.warns(