Skip to content

Commit 3044bde

Browse files
authored
Merge pull request #26122 from anntzer/ai
Only change axes aspect in imshow if image transform is/contains transData
2 parents 6391678 + 177c3e7 commit 3044bde

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

galleries/examples/misc/demo_ribbon_box.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def main():
8686
background_gradient[:, :, :3] = [1, 1, 0]
8787
background_gradient[:, :, 3] = [[0.1, 0.3], [0.3, 0.5]] # alpha channel
8888
ax.imshow(background_gradient, interpolation="bicubic", zorder=0.1,
89-
extent=(0, 1, 0, 1), transform=ax.transAxes, aspect="auto")
89+
extent=(0, 1, 0, 1), transform=ax.transAxes)
9090

9191
plt.show()
9292

lib/matplotlib/axes/_axes.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5527,12 +5527,12 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
55275527
55285528
The input may either be actual RGB(A) data, or 2D scalar data, which
55295529
will be rendered as a pseudocolor image. For displaying a grayscale
5530-
image set up the colormapping using the parameters
5530+
image, set up the colormapping using the parameters
55315531
``cmap='gray', vmin=0, vmax=255``.
55325532
55335533
The number of pixels used to render an image is set by the Axes size
5534-
and the *dpi* of the figure. This can lead to aliasing artifacts when
5535-
the image is resampled because the displayed image size will usually
5534+
and the figure *dpi*. This can lead to aliasing artifacts when
5535+
the image is resampled, because the displayed image size will usually
55365536
not match the size of *X* (see
55375537
:doc:`/gallery/images_contours_and_fields/image_antialiasing`).
55385538
The resampling can be controlled via the *interpolation* parameter
@@ -5567,7 +5567,7 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
55675567
55685568
This parameter is ignored if *X* is RGB(A).
55695569
5570-
aspect : {'equal', 'auto'} or float, default: :rc:`image.aspect`
5570+
aspect : {'equal', 'auto'} or float or None, default: None
55715571
The aspect ratio of the Axes. This parameter is particularly
55725572
relevant for images since it determines whether data pixels are
55735573
square.
@@ -5582,6 +5582,11 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
55825582
that the data fit in the Axes. In general, this will result in
55835583
non-square pixels.
55845584
5585+
Normally, None (the default) means to use :rc:`image.aspect`. However, if
5586+
the image uses a transform that does not contain the axes data transform,
5587+
then None means to not modify the axes aspect at all (in that case, directly
5588+
call `.Axes.set_aspect` if desired).
5589+
55855590
interpolation : str, default: :rc:`image.interpolation`
55865591
The interpolation method used.
55875592
@@ -5715,16 +5720,20 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
57155720
`~matplotlib.pyplot.imshow` expects RGB images adopting the straight
57165721
(unassociated) alpha representation.
57175722
"""
5718-
if aspect is None:
5719-
aspect = mpl.rcParams['image.aspect']
5720-
self.set_aspect(aspect)
57215723
im = mimage.AxesImage(self, cmap=cmap, norm=norm,
57225724
interpolation=interpolation, origin=origin,
57235725
extent=extent, filternorm=filternorm,
57245726
filterrad=filterrad, resample=resample,
57255727
interpolation_stage=interpolation_stage,
57265728
**kwargs)
57275729

5730+
if aspect is None and not (
5731+
im.is_transform_set()
5732+
and not im.get_transform().contains_branch(self.transData)):
5733+
aspect = mpl.rcParams['image.aspect']
5734+
if aspect is not None:
5735+
self.set_aspect(aspect)
5736+
57285737
im.set_data(X)
57295738
im.set_alpha(alpha)
57305739
if im.get_clip_path() is None:

lib/matplotlib/tests/test_image.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1492,3 +1492,14 @@ def test_axesimage_get_shape():
14921492
im.set_data(z)
14931493
assert im.get_shape() == (4, 3)
14941494
assert im.get_size() == im.get_shape()
1495+
1496+
1497+
def test_non_transdata_image_does_not_touch_aspect():
1498+
ax = plt.figure().add_subplot()
1499+
im = np.arange(4).reshape((2, 2))
1500+
ax.imshow(im, transform=ax.transAxes)
1501+
assert ax.get_aspect() == "auto"
1502+
ax.imshow(im, transform=Affine2D().scale(2) + ax.transData)
1503+
assert ax.get_aspect() == 1
1504+
ax.imshow(im, transform=ax.transAxes, aspect=2)
1505+
assert ax.get_aspect() == 2

0 commit comments

Comments
 (0)