geom_imshow#

geom_imshow(image_data, cmap=None, *, norm=None, alpha=None, vmin=None, vmax=None, extent=None, compression=None, show_legend=True, color_by='paint_c', cguide=None, breaks=None, labels=None, lablim=None, format=None)#

Display an image specified by a ndarray with shape:

  • (M, N) - greyscale image

  • (M, N, 3) - color RGB image

  • (M, N, 4) - color RGBA image with an alpha channel

This geom is not as flexible as geom_raster() or geom_tile() but vastly superior in terms of rendering efficiency.

Parameters:
image_datandarray

Specify image type, size, and pixel values. Supported array shapes are:

  • (M, N): an image with scalar data. The values are mapped to colors (greys by default) using normalization. See parameters norm, cmap, vmin, vmax.

  • (M, N, 3): an image with RGB values (0-1 float or 0-255 int).

  • (M, N, 4): an image with RGBA values (0-1 float or 0-255 int).

The first two dimensions (M, N) define the rows and columns of the image. Out-of-range values are clipped.

cmapstr or list, optional

Name of colormap or a list of colors. If a string, it should be the name of a colormap supported by the Palettable package (jiffyclub/palettable), for example, “viridis”, “magma”, “plasma”, “inferno”. If a list, it should contain color strings in hex (‘#RRGGBB’), ‘rgb(r, g, b)’, or ‘rgba(r, g, b, a)’ format with int components (0-255). The greyscale values will be quantized to map to the provided colors. This parameter is ignored for RGB(A) images.

normbool

True (default) - luminance values in greyscale images will be scaled to [0-255] range using a linear scaler. False - disables scaling of luminance values in greyscale images. This parameter is ignored for RGB(A) images.

alphafloat, optional

The alpha blending value, between 0 (transparent) and 1 (opaque).

vmin, vmaxnumber, optional

Define the data range used for luminance normalization in greyscale images. This parameter is ignored for RGB(A) images or if parameter norm=False.

extentlist of 4 numbers: [left, right, bottom, top], optional

Define the image’s bounding box in terms of the “data coordinates”.

  • left, right: coordinates of the pixels’ outer edges along the x-axis for pixels in the 1st and the last column.

  • bottom, top: coordinates of the pixels’ outer edges along the y-axis for pixels in the 1st and the last row.

The default is: [-0.5, ncol-0.5, -0.5, nrow-0.5]

compressionint, optional

The compression level to be used by the zlib module. Values from 0 (no compression) to 9 (highest). Value None means that the zlib module uses the default level of compression (which is generally acceptable).

show_legendbool, default=True

Greyscale images only. False - do not show the legend for this layer.

color_by{‘fill’, ‘color’, ‘paint_a’, ‘paint_b’, ‘paint_c’}, default=’paint_c’

Define the color aesthetic used by the legend shown for a greyscale image.

cguideoptional

A result of guide_colorbar() call. Use to customize the colorbar for greyscale images.

breakslist or dict, optional

Greyscale images only. A list of data values specifying the positions of ticks on the colorbar, or a dictionary which maps the tick labels to the breaks values.

labelslist of str or dict, optional

Greyscale images only. A list of labels on ticks of the colorbar, or a dictionary which maps the breaks values to the tick labels.

lablimint, optional

Greyscale images only. The maximum label length (in characters) before trimming is applied.

formatstr, optional

Greyscale images only. Define the format for labels on the colorbar. The syntax resembles Python’s:

  • ‘.2f’ -> ‘12.45’

  • ‘Num {}’ -> ‘Num 12.456789’

  • ‘TTL: {.2f}$’ -> ‘TTL: 12.45$’

For more info, see Formatting.

Returns:
LayerSpec

Geom object specification.

Notes

This geom doesn’t understand any aesthetics. It doesn’t support color scales either.

Examples

1import numpy as np
2from lets_plot import *
3LetsPlot.setup_html()
4np.random.seed(42)
5image = np.random.randint(256, size=(64, 64, 4))
6ggplot() + geom_imshow(image)

1import numpy as np
2from lets_plot import *
3LetsPlot.setup_html()
4n = 64
5image = 256 * np.linspace(np.linspace(0, .5, n), \
6                          np.linspace(.5, .5, n), n)
7ggplot() + geom_imshow(image, norm=False)

1import numpy as np
2from lets_plot import *
3LetsPlot.setup_html()
4np.random.seed(42)
5image = np.random.normal(size=(64, 64))
6ggplot() + geom_imshow(image, vmin=-1, vmax=1)