Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 800d1fc

Browse filesBrowse files
committed
Only 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 800d1fc
Copy full SHA for 800d1fc

File tree

Expand file treeCollapse file tree

3 files changed

+24
-7
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+24
-7
lines changed

‎galleries/examples/misc/demo_ribbon_box.py

Copy file name to clipboardExpand all lines: galleries/examples/misc/demo_ribbon_box.py
+1-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/axes/_axes.py
+14-6Lines changed: 14 additions & 6 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
@@ -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+
This parameter is ignored if the image uses a transform that does
5589+
not contain the axes data transform
5590+
(``not im.get_transform().contains(ax.transData)``). In that case,
5591+
directly call `.Axes.set_aspect` if desired.
5592+
55885593
interpolation : str, default: :rc:`image.interpolation`
55895594
The interpolation method used.
55905595
@@ -5718,16 +5723,19 @@ 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 (not im.is_transform_set()
5734+
or im.get_transform().contains_branch(self.transData)):
5735+
if aspect is None:
5736+
aspect = mpl.rcParams['image.aspect']
5737+
self.set_aspect(aspect)
5738+
57315739
im.set_data(X)
57325740
im.set_alpha(alpha)
57335741
if im.get_clip_path() is None:

‎lib/matplotlib/tests/test_image.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_image.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,3 +1491,12 @@ 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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.