W3cubDocs

/Matplotlib 3.1

matplotlib.image

The image module supports basic image loading, rescaling and display operations.

class matplotlib.image.AxesImage(ax, cmap=None, norm=None, interpolation=None, origin=None, extent=None, filternorm=1, filterrad=4.0, resample=False, **kwargs) [source]

Bases: matplotlib.image._ImageBase

interpolation and cmap default to their rc settings

cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1

extent is data axes (left, right, bottom, top) for making image plots registered with data plots. Default is to label the pixel centers with the zero-based row and column indices.

Additional kwargs are matplotlib.artist properties

format_cursor_data(self, data) [source]

Return a string representation of data.

Note

This method is intended to be overridden by artist subclasses. As an end-user of Matplotlib you will most likely not call this method yourself.

The default implementation converts ints and floats and arrays of ints and floats into a comma-separated string enclosed in square brackets.

See also

get_cursor_data
get_cursor_data(self, event) [source]

Return the image value at the event position or None if the event is outside the image.

get_extent(self) [source]

Get the image extent: left, right, bottom, top

get_window_extent(self, renderer=None) [source]

Get the axes bounding box in display space.

The bounding box' width and height are nonnegative.

Subclasses should override for inclusion in the bounding box "tight" calculation. Default is to return an empty bounding box at 0, 0.

Be careful when using this function, the results will not update if the artist window extent of the artist changes. The extent can change due to any changes in the transform stack, such as changing the axes limits, the figure size, or the canvas used (as is done when saving a figure). This can lead to unexpected behavior where interactive figures will look fine on the screen, but will save incorrectly.

make_image(self, renderer, magnification=1.0, unsampled=False) [source]

Normalize, rescale, and colormap this image's data for rendering using renderer, with the given magnification.

If unsampled is True, the image will not be scaled, but an appropriate affine transformation will be returned instead.

Returns:
image : (M, N, 4) uint8 array

The RGBA image, resampled unless unsampled is True.

x, y : float

The upper left corner where the image should be drawn, in pixel space.

trans : Affine2D

The affine transformation from image to pixel space.

set_extent(self, extent) [source]

extent is data axes (left, right, bottom, top) for making image plots

This updates ax.dataLim, and, if autoscaling, sets viewLim to tightly fit the image, regardless of dataLim. Autoscaling state is not changed, so following this with ax.autoscale_view will redo the autoscaling in accord with dataLim.

class matplotlib.image.BboxImage(bbox, cmap=None, norm=None, interpolation=None, origin=None, filternorm=1, filterrad=4.0, resample=False, interp_at_native=<deprecated parameter>, **kwargs) [source]

Bases: matplotlib.image._ImageBase

The Image class whose size is determined by the given bbox.

cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1

kwargs are an optional list of Artist keyword args

contains(self, mouseevent) [source]

Test whether the mouse event occurred within the image.

get_transform(self) [source]

Return the Transform instance used by this artist.

get_window_extent(self, renderer=None) [source]

Get the axes bounding box in display space.

The bounding box' width and height are nonnegative.

Subclasses should override for inclusion in the bounding box "tight" calculation. Default is to return an empty bounding box at 0, 0.

Be careful when using this function, the results will not update if the artist window extent of the artist changes. The extent can change due to any changes in the transform stack, such as changing the axes limits, the figure size, or the canvas used (as is done when saving a figure). This can lead to unexpected behavior where interactive figures will look fine on the screen, but will save incorrectly.

interp_at_native
make_image(self, renderer, magnification=1.0, unsampled=False) [source]

Normalize, rescale, and colormap this image's data for rendering using renderer, with the given magnification.

If unsampled is True, the image will not be scaled, but an appropriate affine transformation will be returned instead.

Returns:
image : (M, N, 4) uint8 array

The RGBA image, resampled unless unsampled is True.

x, y : float

The upper left corner where the image should be drawn, in pixel space.

trans : Affine2D

The affine transformation from image to pixel space.

class matplotlib.image.FigureImage(fig, cmap=None, norm=None, offsetx=0, offsety=0, origin=None, **kwargs) [source]

Bases: matplotlib.image._ImageBase

cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1

kwargs are an optional list of Artist keyword args

get_extent(self) [source]

Get the image extent: left, right, bottom, top

make_image(self, renderer, magnification=1.0, unsampled=False) [source]

Normalize, rescale, and colormap this image's data for rendering using renderer, with the given magnification.

If unsampled is True, the image will not be scaled, but an appropriate affine transformation will be returned instead.

Returns:
image : (M, N, 4) uint8 array

The RGBA image, resampled unless unsampled is True.

x, y : float

The upper left corner where the image should be drawn, in pixel space.

trans : Affine2D

The affine transformation from image to pixel space.

set_data(self, A) [source]

Set the image array.

zorder = 0
class matplotlib.image.NonUniformImage(ax, *, interpolation='nearest', **kwargs) [source]

Bases: matplotlib.image.AxesImage

kwargs are identical to those for AxesImage, except that 'nearest' and 'bilinear' are the only supported 'interpolation' options.

get_extent(self) [source]

Get the image extent: left, right, bottom, top

make_image(self, renderer, magnification=1.0, unsampled=False) [source]

Normalize, rescale, and colormap this image's data for rendering using renderer, with the given magnification.

If unsampled is True, the image will not be scaled, but an appropriate affine transformation will be returned instead.

Returns:
image : (M, N, 4) uint8 array

The RGBA image, resampled unless unsampled is True.

x, y : float

The upper left corner where the image should be drawn, in pixel space.

trans : Affine2D

The affine transformation from image to pixel space.

set_array(self, *args) [source]

Retained for backwards compatibility - use set_data instead.

Parameters:
A : array-like
set_cmap(self, cmap) [source]

set the colormap for luminance data

Parameters:
cmap : colormap or registered colormap name
set_data(self, x, y, A) [source]

Set the grid for the pixel centers, and the pixel values.

x and y are monotonic 1-D ndarrays of lengths N and M,
respectively, specifying pixel centers
A is an (M,N) ndarray or masked array of values to be
colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA array.
set_filternorm(self, s) [source]

Set whether the resize filter normalizes the weights.

See help for imshow.

Parameters:
filternorm : bool
set_filterrad(self, s) [source]

Set the resize filter radius only applicable to some interpolation schemes -- see help for imshow

Parameters:
filterrad : positive float
set_interpolation(self, s) [source]
Parameters:
s : str, None

Either 'nearest', 'bilinear', or None.

set_norm(self, norm) [source]

Set the normalization instance.

Parameters:
norm : Normalize

Notes

If there are any colorbars using the mappable for this norm, setting the norm of the mappable will reset the norm, locator, and formatters on the colorbar to default.

class matplotlib.image.PcolorImage(ax, x=None, y=None, A=None, cmap=None, norm=None, **kwargs) [source]

Bases: matplotlib.image.AxesImage

Make a pcolor-style plot with an irregular rectangular grid.

This uses a variation of the original irregular image code, and it is used by pcolorfast for the corresponding grid type.

cmap defaults to its rc setting

cmap is a colors.Colormap instance norm is a colors.Normalize instance to map luminance to 0-1

Additional kwargs are matplotlib.artist properties

get_cursor_data(self, event) [source]

Return the image value at the event position or None if the event is outside the image.

make_image(self, renderer, magnification=1.0, unsampled=False) [source]

Normalize, rescale, and colormap this image's data for rendering using renderer, with the given magnification.

If unsampled is True, the image will not be scaled, but an appropriate affine transformation will be returned instead.

Returns:
image : (M, N, 4) uint8 array

The RGBA image, resampled unless unsampled is True.

x, y : float

The upper left corner where the image should be drawn, in pixel space.

trans : Affine2D

The affine transformation from image to pixel space.

set_array(self, *args) [source]

Retained for backwards compatibility - use set_data instead.

Parameters:
A : array-like
set_data(self, x, y, A) [source]

Set the grid for the rectangle boundaries, and the data values.

x and y are monotonic 1-D ndarrays of lengths N+1 and M+1,
respectively, specifying rectangle boundaries. If None, they will be created as uniform arrays from 0 through N and 0 through M, respectively.
A is an (M,N) ndarray or masked array of values to be
colormapped, or a (M,N,3) RGB array, or a (M,N,4) RGBA array.
matplotlib.image.composite_images(images, renderer, magnification=1.0) [source]

Composite a number of RGBA images into one. The images are composited in the order in which they appear in the images list.

Parameters:
images : list of Images

Each must have a make_image method. For each image, can_composite should return True, though this is not enforced by this function. Each image must have a purely affine transformation with no shear.

renderer : RendererBase instance
magnification : float

The additional magnification to apply for the renderer in use.

Returns:
tuple : image, offset_x, offset_y

Returns the tuple:

  • image: A numpy array of the same type as the input images.
  • offset_x, offset_y: The offset of the image (left, bottom) in the output figure.
matplotlib.image.imread(fname, format=None) [source]

Read an image from a file into an array.

Parameters:
fname : str or file-like

The image file to read. This can be a filename, a URL or a Python file-like object opened in read-binary mode.

format : str, optional

The image file format assumed for reading the data. If not given, the format is deduced from the filename. If nothing can be deduced, PNG is tried.

Returns:
imagedata : numpy.array

The image data. The returned array has shape

  • (M, N) for grayscale images.
  • (M, N, 3) for RGB images.
  • (M, N, 4) for RGBA images.

Notes

Matplotlib can only read PNGs natively. Further image formats are supported via the optional dependency on Pillow. Note, URL strings are not compatible with Pillow. Check the Pillow documentation for more information.

matplotlib.image.imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None, origin=None, dpi=100) [source]

Save an array as an image file.

Parameters:
fname : str or PathLike file-like

A path or a Python file-like object to store the image in. If format is not set, then the output format is inferred from the extension of fname, if any, and from rcParams["savefig.format"] otherwise. If format is set, it determines the output format.

arr : array-like

The image data. The shape can be one of MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).

vmin, vmax : scalar, optional

vmin and vmax set the color scaling for the image by fixing the values that map to the colormap color limits. If either vmin or vmax is None, that limit is determined from the arr min/max value.

cmap : str or Colormap, optional

A Colormap instance or registered colormap name. The colormap maps scalar data to colors. It is ignored for RGB(A) data. Defaults to rcParams["image.cmap"] ('viridis').

format : str, optional

The file format, e.g. 'png', 'pdf', 'svg', ... The behavior when this is unset is documented under fname.

origin : {'upper', 'lower'}, optional

Indicates whether the (0, 0) index of the array is in the upper left or lower left corner of the axes. Defaults to rcParams["image.origin"] ('upper').

dpi : int

The DPI to store in the metadata of the file. This does not affect the resolution of the output image.

matplotlib.image.pil_to_array(pilImage) [source]

Load a PIL image and return it as a numpy array.

Returns:
numpy.array

The array shape depends on the image type:

  • (M, N) for grayscale images.
  • (M, N, 3) for RGB images.
  • (M, N, 4) for RGBA images.
matplotlib.image.thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear', preview=False) [source]

Make a thumbnail of image in infile with output filename thumbfile.

See Image Thumbnail.

Parameters:
infile : str or file-like

The image file -- must be PNG, or Pillow-readable if you have Pillow installed.

thumbfile : str or file-like

The thumbnail filename.

scale : float, optional

The scale factor for the thumbnail.

interpolation : str, optional

The interpolation scheme used in the resampling. See the interpolation parameter of imshow for possible values.

preview : bool, optional

If True, the default backend (presumably a user interface backend) will be used which will cause a figure to be raised if show is called. If it is False, the figure is created using FigureCanvasBase and the drawing backend is selected as savefig would normally do.

Returns:
figure : Figure

The figure instance containing the thumbnail.

© 2012–2018 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.1.1/api/image_api.html