rasterio.plot module

Implementations of various common operations.

Including show() for displaying an array or with matplotlib. Most can handle a numpy array or rasterio.Band(). Primarily supports $ rio insp.

rasterio.plot.adjust_band(band, kind=None)

Adjust a band to be between 0 and 1.

Parameters:
  • band (array, shape (height, width)) – A band of a raster object.

  • kind (str) – An unused option. For now, there is only one option (‘linear’).

Returns:

band_normed – An adjusted version of the input band.

Return type:

array, shape (height, width)

rasterio.plot.get_plt()

import matplotlib.pyplot raise import error if matplotlib is not installed

rasterio.plot.plotting_extent(source, transform=None)
Returns an extent in the format needed

for matplotlib.pyplot.imshow() (left, right, bottom, top) instead of rasterio’s bounds (left, bottom, right, top)

Parameters:
  • source (numpy.ndarray or dataset object opened in 'r' mode) – If array, data in the order rows, columns and optionally bands. If array is band order (bands in the first dimension), use arr[0]

  • transform (Affine, required if source is array) – Defines the affine transform if source is an array

Returns:

left, right, bottom, top

Return type:

tuple of float

rasterio.plot.reshape_as_image(arr)

Returns the source array reshaped into the order expected by image processing and visualization software (matplotlib, scikit-image, etc) by swapping the axes order from (bands, rows, columns) to (rows, columns, bands)

Parameters:

arr (array-like of shape (bands, rows, columns)) – image to reshape

rasterio.plot.reshape_as_raster(arr)

Returns the array in a raster order by swapping the axes order from (rows, columns, bands) to (bands, rows, columns)

Parameters:

arr (array-like in the image form of (rows, columns, bands)) – image to reshape

rasterio.plot.show(source, with_bounds=True, contour=False, contour_label_kws=None, ax=None, title=None, transform=None, adjust=False, **kwargs)

Display a raster or raster band using matplotlib.

Parameters:
  • source (array or dataset object opened in 'r' mode or Band or tuple(dataset, bidx)) – If Band or tuple (dataset, bidx), display the selected band. If raster dataset display the rgb image as defined in the colorinterp metadata, or default to first band.

  • with_bounds (bool (opt)) – Whether to change the image extent to the spatial bounds of the image, rather than pixel coordinates. Only works when source is (raster dataset, bidx) or raster dataset.

  • contour (bool (opt)) – Whether to plot the raster data as contours

  • contour_label_kws (dictionary (opt)) – Keyword arguments for labeling the contours, empty dictionary for no labels.

  • ax (matplotlib.axes.Axes, optional) – Axes to plot on, otherwise uses current axes.

  • title (str, optional) – Title for the figure.

  • transform (Affine, optional) – Defines the affine transform if source is an array

  • adjust (bool) – If the plotted data is an RGB image, adjust the values of each band so that they fall between 0 and 1 before plotting. If True, values will be adjusted by the min / max of each band. If False, no adjustment will be applied.

  • **kwargs (key, value pairings optional) – These will be passed to the matplotlib.pyplot.imshow() or matplotlib.pyplot.contour() contour method depending on contour argument.

Returns:

ax – Axes with plot.

Return type:

matplotlib.axes.Axes

rasterio.plot.show_hist(source, bins=10, masked=True, title='Histogram', ax=None, label=None, range=None, **kwargs)

Easily display a histogram with matplotlib.

Parameters:
  • source (array or dataset object opened in 'r' mode or Band or tuple(dataset, bidx)) – Input data to display. The first three arrays in multi-dimensional arrays are plotted as red, green, and blue.

  • bins (int, optional) – Compute histogram across N bins.

  • masked (bool, optional) – When working with a rasterio.Band() object, specifies if the data should be masked on read.

  • title (str, optional) – Title for the figure.

  • ax (matplotlib.axes.Axes, optional) – The raster will be added to this axes if passed.

  • label (str, optional) – String, or list of strings. If passed, matplotlib will use this label list. Otherwise, a default label list will be automatically created

  • range (list, optional) – List of [min, max] values. If passed, matplotlib will use this range. Otherwise, a default range will be automatically created

  • **kwargs (optional keyword arguments) – These will be passed to the matplotlib.axes.Axes.hist() method.