Download notebook (.ipynb)

geom_imshow()#

geom_imshow() displays an image specified by 2D or 3D Numpy array.

Whether the image is grayscale or color depends on the shape of the image array:

  • (M, N) - grey-scale image

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

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

The array’s dtype can be int, uint or float.

By default, all values in the image array will be transformed to the range [0-255] using a linear scaler.

import numpy as np

from lets_plot import *
LetsPlot.setup_html()

Grayscale image#

A2x3 = np.array([
    [50, 150 ,200],
    [200,100,50]
    ])

ggplot() + geom_imshow(A2x3)

Grayscale image without normalization#

ggplot() + geom_imshow(A2x3, norm=False)

Grayscale image in “Viridis” colors#

ggplot() + geom_imshow(A2x3, cmap="viridis")

RGB image#

M x N x 3 array

A2x3x3 = np.array([
    [[255, 0, 0], [0, 255, 0], [0, 0, 255]],
    [[0, 255, 0], [0, 0, 255], [255, 0, 0]]
    ])
ggplot() + geom_imshow(A2x3x3)

RGB image with alpha channel#

M x N x 4 array

A2x3x4 = np.array([
    [[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1]],
    [[0, 1, 0, 0.3], [0, 0, 1, 0.3], [1, 0, 0, 0.3]]
    ])

ggplot() + geom_imshow(A2x3x4)

Just a random image#

np.random.seed(42)
image = np.random.choice([0.0, 1.0], [10, 100, 3])
ggplot() + geom_imshow(image) + coord_cartesian()