From 867dbd3970a929dfa62847ead4b8b708a82ab1b1 Mon Sep 17 00:00:00 2001 From: Neil Vaytet Date: Thu, 5 Sep 2024 09:02:10 +0200 Subject: [PATCH] fix image bounding box when coords are midpoints --- src/plopp/backends/matplotlib/image.py | 4 ++-- tests/backends/matplotlib/mpl_image_test.py | 22 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/plopp/backends/matplotlib/image.py b/src/plopp/backends/matplotlib/image.py index de2b58a3..5b61eef3 100644 --- a/src/plopp/backends/matplotlib/image.py +++ b/src/plopp/backends/matplotlib/image.py @@ -216,8 +216,8 @@ def bbox(self, xscale: Literal['linear', 'log'], yscale: Literal['linear', 'log' The bounding box of the image. """ ydim, xdim = self._data.dims - image_x = self._data.coords[xdim] - image_y = self._data.coords[ydim] + image_x = self._data_with_bin_edges.coords[xdim] + image_y = self._data_with_bin_edges.coords[ydim] return BoundingBox( **{**axis_bounds(('xmin', 'xmax'), image_x, xscale)}, diff --git a/tests/backends/matplotlib/mpl_image_test.py b/tests/backends/matplotlib/mpl_image_test.py index 8379d838..edffde56 100644 --- a/tests/backends/matplotlib/mpl_image_test.py +++ b/tests/backends/matplotlib/mpl_image_test.py @@ -34,3 +34,25 @@ def test_kwargs_are_forwarded_to_artist(): fig = imagefigure(Node(da), rasterized=False) [artist] = fig.artists.values() assert not artist._mesh.get_rasterized() + + +def test_bbox_midpoints(): + da = data_array(ndim=2) + fig = imagefigure(Node(da)) + [artist] = fig.artists.values() + bbox = artist.bbox(xscale='linear', yscale='linear') + assert bbox.xmin < da.coords['xx'].min().value + assert bbox.xmax > da.coords['xx'].max().value + assert bbox.ymin < da.coords['yy'].min().value + assert bbox.ymax > da.coords['yy'].max().value + + +def test_bbox_binedges(): + da = data_array(ndim=2, binedges=True) + fig = imagefigure(Node(da)) + [artist] = fig.artists.values() + bbox = artist.bbox(xscale='linear', yscale='linear') + assert bbox.xmin == da.coords['xx'].min().value + assert bbox.xmax == da.coords['xx'].max().value + assert bbox.ymin == da.coords['yy'].min().value + assert bbox.ymax == da.coords['yy'].max().value