Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix image bounding box when coords are midpoints #361

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/plopp/backends/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
22 changes: 22 additions & 0 deletions tests/backends/matplotlib/mpl_image_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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