Skip to content

Commit

Permalink
Merge pull request #293 from scipp/fix_plot_with_nan_and_errorbars
Browse files Browse the repository at this point in the history
Fix plotting with errorbars when nan values are present
  • Loading branch information
nvaytet authored Jan 16, 2024
2 parents 829986d + e2e552b commit a93c62f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/plopp/backends/matplotlib/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)
Expand Down
15 changes: 15 additions & 0 deletions tests/plotting/plot_1d_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a93c62f

Please sign in to comment.