Skip to content

Commit ae5c6ad

Browse files
committed
Fix image bbox clip.
The baseline image change was due to previously off-by-one clipping, as can be checked by additionally drawing a line with the same clipbox and checking that the image was clipped one pixel row/column too far: from matplotlib import pyplot as plt, patches fig, ax = plt.subplots() d = [[1, 2], [3, 4]] im = ax.imshow(d, extent=(0, 5, 0, 5), cmap="viridis") l, = ax.plot([0, 5], [0, 5]) rect = patches.Rectangle( xy=(1, 1), width=2, height=2, transform=im.axes.transData) im.set_clip_path(rect) l.set_clip_path(rect) plt.show()
1 parent 385dd94 commit ae5c6ad

File tree

4 files changed

+58
-39
lines changed

4 files changed

+58
-39
lines changed

lib/matplotlib/image.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,8 +863,9 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):
863863
bbox = Bbox(np.array([[x1, y1], [x2, y2]]))
864864
transformed_bbox = TransformedBbox(bbox, trans)
865865
return self._make_image(
866-
self._A, bbox, transformed_bbox, self.axes.bbox, magnification,
867-
unsampled=unsampled)
866+
self._A, bbox, transformed_bbox,
867+
self.get_clip_box() or self.axes.bbox,
868+
magnification, unsampled=unsampled)
868869

869870
def _check_unsampled_image(self, renderer):
870871
"""
-28 Bytes
Loading

lib/matplotlib/tests/baseline_images/test_image/image_cliprect.svg

Lines changed: 37 additions & 37 deletions
Loading

lib/matplotlib/tests/test_image.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,3 +1083,21 @@ def test_deprecation():
10831083
with pytest.warns(MatplotlibDeprecationWarning):
10841084
# Enough arguments to pass "shape" positionally.
10851085
obj.imshow(data, *[None] * 10)
1086+
1087+
1088+
def test_respects_bbox():
1089+
fig, axs = plt.subplots(2)
1090+
for ax in axs:
1091+
ax.set_axis_off()
1092+
im = axs[1].imshow([[0, 1], [2, 3]], aspect="auto", extent=(0, 1, 0, 1))
1093+
im.set_clip_path(None)
1094+
# Make the image invisible in axs[1], but visible in axs[0] if we pan
1095+
# axs[1] up.
1096+
im.set_clip_box(axs[0].bbox)
1097+
buf_before = io.BytesIO()
1098+
fig.savefig(buf_before, format="rgba")
1099+
assert {*buf_before.getvalue()} == {0xff} # All white.
1100+
axs[1].set(ylim=(-1, 0))
1101+
buf_after = io.BytesIO()
1102+
fig.savefig(buf_after, format="rgba")
1103+
assert buf_before.getvalue() != buf_after.getvalue() # Not all white.

0 commit comments

Comments
 (0)