Repository: scipp/plopp

Issue:
Cannot plot data with bin edges left over from slicing
Example:

```Py
import plopp  as pp

da = pp.data.data2d(binedges=True)
f = da.fold(dim='x', sizes={'x': 25, 'pulse': 2})
f['pulse', 0].plot()
```
fails with
```
File [~/code/path/plopp/backends/matplotlib/image.py:119](http://localhost:8889/home/nvaytet/code/path/plopp/backends/matplotlib/image.py#line=118), in Image.__init__(self, canvas, colormapper, data, uid, shading, rasterized, **kwargs)
    114 to_dim_search[k] = {
    115     'dim': self._data.dims[i],
    116     'var': self._data.coords[self._data.dims[i]],
    117 }
    118 bin_edge_coords[k] = coord_as_bin_edges(self._data, self._data.dims[i])
--> 119 self._data_with_bin_edges.coords[self._data.dims[i]] = bin_edge_coords[k]
    120 if self._data.coords[self._data.dims[i]].dtype == str:
    121     string_labels[k] = self._data.coords[self._data.dims[i]]

DimensionError: Cannot add coord 'x' of dims (x: 25, pulse: 2) to DataArray with dims (y: 40, x: 25)
```

Discussion:

Patch:
diff --git a/src/plopp/plotting/common.py b/src/plopp/plotting/common.py
index 4c2c47c..8ea7869 100644
--- a/src/plopp/plotting/common.py
+++ b/src/plopp/plotting/common.py
@@ -98,6 +98,16 @@ def to_data_array(
     for dim, size in out.sizes.items():
         if dim not in out.coords:
             out.coords[dim] = sc.arange(dim, size, unit=None)
+        if not out.coords[dim].dims:
+            raise ValueError(
+                "Input data cannot be plotted: it has a scalar coordinate along "
+                f"dimension {dim}. Consider dropping this coordinate before plotting. "
+                f"Use ``data.drop_coords('{dim}').plot()``."
+            )
+    for name in out.coords:
+        other_dims = [dim for dim in out.coords[name].dims if dim not in out.dims]
+        for dim in other_dims:
+            out.coords[name] = out.coords[name].mean(dim)
     return out
 
 


Test patch:
diff --git a/tests/plotting/plot_2d_test.py b/tests/plotting/plot_2d_test.py
index 0055689..953e077 100644
--- a/tests/plotting/plot_2d_test.py
+++ b/tests/plotting/plot_2d_test.py
@@ -333,3 +333,16 @@ def test_figure_has_only_unit_on_colorbar_for_multiple_images(linspace):
     assert str(b.unit) in ylabel
     assert a.name not in ylabel
     assert b.name not in ylabel
+
+
+def test_plot_with_bin_edges_left_over_from_slicing():
+    da = data_array(ndim=2, binedges=True)
+    f = da.fold(dim='xx', sizes={'xx': 25, 'pulse': 2})
+    f['pulse', 0].plot()
+
+
+def test_plot_with_scalar_dimension_coord_raises():
+    da = data_array(ndim=2)
+    da.coords['xx'] = sc.scalar(333.0, unit='K')
+    with pytest.raises(ValueError, match='Input data cannot be plotted'):
+        da.plot()