.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/images_contours_and_fields/affine_image.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. meta:: :keywords: codex .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_images_contours_and_fields_affine_image.py: ============================ Affine transform of an image ============================ Prepending an affine transformation (`~.transforms.Affine2D`) to the :ref:`data transform ` of an image allows to manipulate the image's shape and orientation. This is an example of the concept of :ref:`transform chaining `. The image of the output should have its boundary match the dashed yellow rectangle. .. GENERATED FROM PYTHON SOURCE LINES 15-68 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import matplotlib.transforms as mtransforms def get_image(): delta = 0.25 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z1 = np.exp(-X**2 - Y**2) Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2) Z = (Z1 - Z2) return Z def do_plot(ax, Z, transform): im = ax.imshow(Z, interpolation='none', origin='lower', extent=[-2, 4, -3, 2], clip_on=True) trans_data = transform + ax.transData im.set_transform(trans_data) # display intended extent of the image x1, x2, y1, y2 = im.get_extent() ax.plot([x1, x2, x2, x1, x1], [y1, y1, y2, y2, y1], "y--", transform=trans_data) ax.set_xlim(-5, 5) ax.set_ylim(-4, 4) # prepare image and figure fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2) Z = get_image() # image rotation do_plot(ax1, Z, mtransforms.Affine2D().rotate_deg(30)) # image skew do_plot(ax2, Z, mtransforms.Affine2D().skew_deg(30, 15)) # scale and reflection do_plot(ax3, Z, mtransforms.Affine2D().scale(-1, .5)) # everything and a translation do_plot(ax4, Z, mtransforms.Affine2D(). rotate_deg(30).skew_deg(30, 15).scale(-1, .5).translate(.5, -1)) plt.show() .. image-sg:: /gallery/images_contours_and_fields/images/sphx_glr_affine_image_001.png :alt: affine image :srcset: /gallery/images_contours_and_fields/images/sphx_glr_affine_image_001.png, /gallery/images_contours_and_fields/images/sphx_glr_affine_image_001_2_00x.png 2.00x :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 69-76 .. admonition:: References The use of the following functions, methods, classes and modules is shown in this example: - `matplotlib.axes.Axes.imshow` / `matplotlib.pyplot.imshow` - `matplotlib.transforms.Affine2D` .. _sphx_glr_download_gallery_images_contours_and_fields_affine_image.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: affine_image.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: affine_image.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_