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.

In [1]:
import numpy as np
from lets_plot import *

LetsPlot.setup_html()

Grayscale image

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

ggplot() + geom_imshow(A2x3)
Out[2]:

Grayscale image without normalization

In [3]:
ggplot() + geom_imshow(A2x3, norm=False)
Out[3]:

Grayscale image in "Viridis" colors

In [4]:
ggplot() + geom_imshow(A2x3, cmap="viridis")
Out[4]:

RGB image

M x N x 3 array

In [5]:
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)
Out[5]:

RGB image with alpha channel

M x N x 4 array

In [6]:
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)
Out[6]:

Just a random image

In [7]:
image = np.random.choice([0.0, 1.0], [10, 100, 3])
ggplot() + geom_imshow(image) + coord_cartesian()
Out[7]: