From e2e552b9f16cee73a82f0d509424bc9bbb7e8905 Mon Sep 17 00:00:00 2001 From: Neil Vaytet Date: Tue, 16 Jan 2024 14:49:22 +0100 Subject: [PATCH] fix plotting with errorbars when nan values are present --- src/plopp/backends/matplotlib/canvas.py | 10 ++++++++-- tests/plotting/plot_1d_test.py | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/plopp/backends/matplotlib/canvas.py b/src/plopp/backends/matplotlib/canvas.py index 0c14ed93..2cd073ec 100644 --- a/src/plopp/backends/matplotlib/canvas.py +++ b/src/plopp/backends/matplotlib/canvas.py @@ -169,11 +169,17 @@ def autoscale(self): bbox = bbox.union(mesh_bbox) elif hasattr(c, '_plopp_mask'): - line_mask = sc.array(dims=['x'], values=c._plopp_mask) + segments = c.get_segments() + # Here get_segments() can return empty segments in the case where the + # data values are NaN, which we filter out. + lengths = np.array([len(segs) for segs in segments]) + line_mask = sc.array(dims=['x'], values=c._plopp_mask[lengths > 0]) line_y = sc.DataArray( data=sc.array( dims=['x', 'y'], - values=np.array(c.get_segments())[..., 1], + values=np.array([s for (s, l) in zip(segments, lengths) if l])[ + ..., 1 + ], ), masks={'mask': line_mask}, ) diff --git a/tests/plotting/plot_1d_test.py b/tests/plotting/plot_1d_test.py index dea77144..902a0af7 100644 --- a/tests/plotting/plot_1d_test.py +++ b/tests/plotting/plot_1d_test.py @@ -434,3 +434,18 @@ def test_plot_1d_datetime_coord_log_with_mask(): masks={'m': time > sc.datetime('2017-03-16T21:10:00')}, ) pp.plot(da, scale={'time': 'log'}) + + +def test_plot_1d_data_with_errorbars(): + da = data_array(ndim=1, variances=True) + p = da.plot() + assert p.canvas.ymin < -1.0 + assert p.canvas.ymax > 1.0 + + +def test_plot_1d_data_with_variances_and_nan_values(): + da = data_array(ndim=1, variances=True) + da.values[-10:] = np.nan + p = da.plot() + assert p.canvas.ymin < -1.0 + assert p.canvas.ymax > 1.0