Using parameters norm, vmain, vmax and cmap in geom_imshow()

You can use parameters norm, vmain, vmax and cmap when rendering grayscale images.

A grayscale image is specified by Numpy's 2D array where each element's value represents the luminance of corresponding pixel in the image.

In [1]:
import numpy as np
from lets_plot import *
LetsPlot.setup_html()
In [2]:
# Generate 2D Numpy arrays. 
# First two arrays both contain float numbers but have different range of values.
# The 3rd array contains int-s.
arr_f0 = np.linspace(.3, .7, 30).reshape(1, -1)
arr_f1 = np.linspace(30., 170., 30).reshape(1, -1)
arr_i = np.linspace(30, 170, 30, dtype=int).reshape(1, -1)
In [3]:
print("arr_f0: shape {}, data range [{}-{}] {}".format(arr_f0.shape, arr_f0.min(), arr_f0.max(), arr_f0.dtype))
print("arr_f1: shape {}, data range [{}-{}] {}".format(arr_f1.shape, arr_f1.min(), arr_f1.max(), arr_f1.dtype))
print("arr_i : shape {}, data range [{}-{}] {}".format(arr_i.shape, arr_i.min(), arr_i.max(), arr_i.dtype))
arr_f0: shape (1, 30), data range [0.3-0.7] float64
arr_f1: shape (1, 30), data range [30.0-170.0] float64
arr_i : shape (1, 30), data range [30-170] int32
In [4]:
# Setup suitable plot options for the demo.
p = (ggplot() + ggsize(450, 60) + geom_rect(xmin=-0.5, ymin=-0.5, xmax=29.5, ymax=0.5, color="black", alpha=0) + 
coord_cartesian() + theme_classic() + theme(axis="blank"))

Image normalization

By default, geom_imshow() applies a linear scaling to transform data values to range [0-255].

In [5]:
(p + geom_imshow(arr_f0)).show()
(p + geom_imshow(arr_f1)).show()
(p + geom_imshow(arr_i)).show()

You can disable image normalization using the norm parameter.

In [6]:
(p + geom_imshow(arr_f0, norm=False)).show()
(p + geom_imshow(arr_f1, norm=False)).show()
(p + geom_imshow(arr_i, norm=False)).show()

Define normalization range using vmin, vmax parameters

In [7]:
# Set upper limit less than the data max value.
(p + geom_imshow(arr_f0, vmax=.5)).show()
(p + geom_imshow(arr_f1, vmax=100)).show()
(p + geom_imshow(arr_i, vmax=100)).show()
In [8]:
# Set limits wider than the data entire range.
(p + geom_imshow(arr_f0, vmin=.4, vmax=.6)).show()
(p + geom_imshow(arr_f1, vmin=70, vmax=120)).show()
(p + geom_imshow(arr_i, vmin=50, vmax=150)).show()

Colormaps

In [9]:
(p + geom_imshow(arr_f0, cmap="magma")).show()
(p + geom_imshow(arr_f1, cmap="viridis")).show()
(p + geom_imshow(arr_i, cmap="plasma")).show()