imshow_extent
origin and extent in imshow
imshow() allows you to render an image (either a 2D array
which will be color-mapped (based on norm and cmap) or and 3D RGB(A)
array which will be used as-is) to a rectangular region in dataspace.
The orientation of the image in the final rendering is controlled by
the origin and extent kwargs (and attributes on the resultingAxesImage instance) and the data limits of the axes.
The extent kwarg controls the bounding box in data coordinates that
the image will fill specified as (left, right, bottom, top) in
data coordinates, the origin kwarg controls how the image fills
that bounding box, and the orientation in the final rendered image is
also affected by the axes limits.
Most of the code below is used for adding labels and informative
text to the plots. The described effects of origin and extent can be
seen in the plots without the need to follow all code details.
For a quick understanding, you may want to skip the code details below and
directly continue with the discussion of the results.
1 | import numpy as np |
Default extent
First, let’s have a look at the default extent=None
1 | generate_imshow_demo_grid(extents=[None]) |

Generally, for an array of shape (M, N), the first index runs along the
vertical, the second index runs along the horizontal.
The pixel centers are at integer positions ranging from 0 to N' = N - 1
horizontally and from 0 to M' = M - 1 vertically.
origin determines how to the data is filled in the bounding box.
For origin='lower':
- [0, 0] is at (left, bottom)
- [M’, 0] is at (left, top)
- [0, N’] is at (right, bottom)
- [M’, N’] is at (right, top)
origin='upper' reverses the vertical axes direction and filling:
- [0, 0] is at (left, top)
- [M’, 0] is at (left, bottom)
- [0, N’] is at (right, top)
- [M’, N’] is at (right, bottom)
In summary, the position of the [0, 0] index as well as the extent are
influenced by origin:
origin
[0, 0] position
extent
upper
top left
(-0.5, numcols-0.5, numrows-0.5, -0.5)
lower
bottom left
(-0.5, numcols-0.5, -0.5, numrows-0.5)
The default value of origin is set by rcParams["image.origin"] = 'upper' which defaults
to 'upper' to match the matrix indexing conventions in math and
computer graphics image indexing conventions.
Explicit extent
By setting extent we define the coordinates of the image area. The
underlying image data is interpolated/resampled to fill that area.
If the axes is set to autoscale, then the view limits of the axes are set
to match the extent which ensures that the coordinate set by(left, bottom) is at the bottom left of the axes! However, this
may invert the axis so they do not increase in the ‘natural’ direction.
1 | extents = [(-0.5, 6.5, -0.5, 5.5), |

Explicit extent and axes limits
If we fix the axes limits by explicitly setting set_xlim / set_ylim, we
force a certain size and orientation of the axes.
This can decouple the ‘left-right’ and ‘top-bottom’ sense of the image from
the orientation on the screen.
In the example below we have chosen the limits slightly larger than the
extent (note the white areas within the Axes).
While we keep the extents as in the examples before, the coordinate (0, 0)
is now explicitly put at the bottom left and values increase to up and to
the right (from the viewer point of view).
We can see that:
- The coordinate
(left, bottom)anchors the image which then fills the
box going towards the(right, top)point in data space. - The first column is always closest to the ‘left’.
- origin controls if the first row is closest to ‘top’ or ‘bottom’.
- The image may be inverted along either direction.
- The ‘left-right’ and ‘top-bottom’ sense of the image may be uncoupled from
the orientation on the screen.
1 | generate_imshow_demo_grid(extents=[None] + extents, |

Total running time of the script: ( 0 minutes 2.056 seconds)




