Skip to content

Commit 177c3e7

Browse files
committed
Only auto-change axes aspect in imshow if image transform is/contains transData.
If the image transform does not contain transData then setting the axes aspect to 1 will typically not help to make image pixel squares anyways.
1 parent 4b5fc94 commit 177c3e7

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
@@ -5530,12 +5530,12 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
55305530
55315531
The input may either be actual RGB(A) data, or 2D scalar data, which
55325532
will be rendered as a pseudocolor image. For displaying a grayscale
5533-
image set up the colormapping using the parameters
5533+
image, set up the colormapping using the parameters
55345534
``cmap='gray', vmin=0, vmax=255``.
55355535
55365536
The number of pixels used to render an image is set by the Axes size
5537-
and the *dpi* of the figure. This can lead to aliasing artifacts when
5538-
the image is resampled because the displayed image size will usually
5537+
and the figure *dpi*. This can lead to aliasing artifacts when
5538+
the image is resampled, because the displayed image size will usually
55395539
not match the size of *X* (see
55405540
:doc:`/gallery/images_contours_and_fields/image_antialiasing`).
55415541
The resampling can be controlled via the *interpolation* parameter
@@ -5570,7 +5570,7 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
55705570
55715571
This parameter is ignored if *X* is RGB(A).
55725572
5573-
aspect : {'equal', 'auto'} or float, default: :rc:`image.aspect`
5573+
aspect : {'equal', 'auto'} or float or None, default: None
55745574
The aspect ratio of the Axes. This parameter is particularly
55755575
relevant for images since it determines whether data pixels are
55765576
square.
@@ -5585,6 +5585,11 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
55855585
that the data fit in the Axes. In general, this will result in
55865586
non-square pixels.
55875587
5588+
Normally, None (the default) means to use :rc:`image.aspect`. However, if
5589+
the image uses a transform that does not contain the axes data transform,
5590+
then None means to not modify the axes aspect at all (in that case, directly
5591+
call `.Axes.set_aspect` if desired).
5592+
55885593
interpolation : str, default: :rc:`image.interpolation`
55895594
The interpolation method used.
55905595
@@ -5718,16 +5723,20 @@ def imshow(self, X, cmap=None, norm=None, *, aspect=None,
57185723
`~matplotlib.pyplot.imshow` expects RGB images adopting the straight
57195724
(unassociated) alpha representation.
57205725
"""
5721-
if aspect is None:
5722-
aspect = mpl.rcParams['image.aspect']
5723-
self.set_aspect(aspect)
57245726
im = mimage.AxesImage(self, cmap=cmap, norm=norm,
57255727
interpolation=interpolation, origin=origin,
57265728
extent=extent, filternorm=filternorm,
57275729
filterrad=filterrad, resample=resample,
57285730
interpolation_stage=interpolation_stage,
57295731
**kwargs)
57305732

5733+
if aspect is None and not (
5734+
im.is_transform_set()
5735+
and not im.get_transform().contains_branch(self.transData)):
5736+
aspect = mpl.rcParams['image.aspect']
5737+
if aspect is not None:
5738+
self.set_aspect(aspect)
5739+
57315740
im.set_data(X)
57325741
im.set_alpha(alpha)
57335742
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
@@ -1491,3 +1491,14 @@ def test_axesimage_get_shape():
14911491
im.set_data(z)
14921492
assert im.get_shape() == (4, 3)
14931493
assert im.get_size() == im.get_shape()
1494+
1495+
1496+
def test_non_transdata_image_does_not_touch_aspect():
1497+
ax = plt.figure().add_subplot()
1498+
im = np.arange(4).reshape((2, 2))
1499+
ax.imshow(im, transform=ax.transAxes)
1500+
assert ax.get_aspect() == "auto"
1501+
ax.imshow(im, transform=Affine2D().scale(2) + ax.transData)
1502+
assert ax.get_aspect() == 1
1503+
ax.imshow(im, transform=ax.transAxes, aspect=2)
1504+
assert ax.get_aspect() == 2

0 commit comments

Comments
 (0)