Repository: scipp/plopp

Issue:
vmin and vmax not working on 1d plots

Discussion:

Patch:
diff --git a/src/plopp/graphics/canvas.py b/src/plopp/graphics/canvas.py
index 5f618b6..c106cb6 100644
--- a/src/plopp/graphics/canvas.py
+++ b/src/plopp/graphics/canvas.py
@@ -62,6 +62,8 @@ class Canvas:
         self._user_vmin = vmin
         self._user_vmax = vmax
         self._scale = {} if scale is None else scale
+        self.xunit = None
+        self.yunit = None
 
         if self.ax is None:
             if figsize is None:
@@ -136,6 +138,12 @@ class Canvas:
                 self._xmax = max(self._xmax, right.value)
                 self._ymin = min(self._ymin, bottom.value)
                 self._ymax = max(self._ymax, top.value)
+        if self._user_vmin is not None:
+            assert self._user_vmin.unit == self.yunit
+            self._ymin = self._user_vmin.value
+        if self._user_vmax is not None:
+            assert self._user_vmax.unit == self.yunit
+            self._ymax = self._user_vmax.value
         self.ax.set_xlim(_none_if_not_finite(self._xmin),
                          _none_if_not_finite(self._xmax))
         self.ax.set_ylim(_none_if_not_finite(self._ymin),
diff --git a/src/plopp/graphics/fig1d.py b/src/plopp/graphics/fig1d.py
index c15ecae..01247cc 100644
--- a/src/plopp/graphics/fig1d.py
+++ b/src/plopp/graphics/fig1d.py
@@ -75,7 +75,9 @@ class Figure1d(BaseFig):
                              aspect=aspect,
                              grid=grid,
                              figsize=figsize,
-                             title=title)
+                             title=title,
+                             vmin=vmin,
+                             vmax=vmax)
         self.canvas.yscale = norm
 
         self.render()
@@ -118,6 +120,8 @@ class Figure1d(BaseFig):
                 'unit': new_values.meta[new_values.dim].unit
             }
 
+            self.canvas.xunit = self.dims['x']['unit']
+            self.canvas.yunit = new_values.unit
             self.canvas.xlabel = name_with_unit(
                 var=new_values.meta[self.dims['x']['dim']])
             self.canvas.ylabel = name_with_unit(var=new_values.data, name="")
diff --git a/src/plopp/graphics/fig2d.py b/src/plopp/graphics/fig2d.py
index 219d182..4aa4f22 100644
--- a/src/plopp/graphics/fig2d.py
+++ b/src/plopp/graphics/fig2d.py
@@ -126,6 +126,8 @@ class Figure2d(BaseFig):
                 }
             })
 
+            self.canvas.xunit = self.dims['x']['unit']
+            self.canvas.yunit = self.dims['y']['unit']
             self.canvas.xlabel = name_with_unit(
                 var=new_values.meta[self.dims['x']['dim']])
             self.canvas.ylabel = name_with_unit(


Test patch:
diff --git a/tests/graphics/fig1d_test.py b/tests/graphics/fig1d_test.py
index e756b16..d584c51 100644
--- a/tests/graphics/fig1d_test.py
+++ b/tests/graphics/fig1d_test.py
@@ -135,3 +135,29 @@ def test_grid():
     da = data_array(ndim=1)
     fig = Figure1d(input_node(da), grid=True)
     assert fig.canvas.ax.xaxis.get_gridlines()[0].get_visible()
+
+
+def test_vmin():
+    da = data_array(ndim=1)
+    fig = Figure1d(input_node(da), vmin=sc.scalar(-0.5, unit='m/s'))
+    assert fig.canvas.ax.get_ylim()[0] == -0.5
+
+
+def test_vmin_unit_mismatch_raises():
+    da = data_array(ndim=1)
+    with pytest.raises(AssertionError):
+        _ = Figure1d(input_node(da), vmin=sc.scalar(-0.5, unit='m'))
+
+
+def test_vmax():
+    da = data_array(ndim=1)
+    fig = Figure1d(input_node(da), vmax=sc.scalar(0.68, unit='m/s'))
+    assert fig.canvas.ax.get_ylim()[1] == 0.68
+
+
+def test_vmin_vmax():
+    da = data_array(ndim=1)
+    fig = Figure1d(input_node(da),
+                   vmin=sc.scalar(-0.5, unit='m/s'),
+                   vmax=sc.scalar(0.68, unit='m/s'))
+    assert np.allclose(fig.canvas.ax.get_ylim(), [-0.5, 0.68])