import numpy as np
from bqplot import Figure, LinearScale, Axis, ColorScale
from bqplot_image_gl import ImageGL
scale_x = LinearScale(min=0, max=1)
scale_y = LinearScale(min=0, max=1)
scales = {'x': scale_x,
'y': scale_y}
axis_x = Axis(scale=scale_x, label='x')
axis_y = Axis(scale=scale_y, label='y', orientation='vertical')
figure = Figure(scales=scales, axes=[axis_x, axis_y])
scales_image = {'x': scale_x,
'y': scale_y,
'image': ColorScale(min=0, max=2)}
# data should be shape [width, height] or [width, height, channels]
# where channels is 3 for rgb, and 4 for rgba
# if no channel is present, the color scale is used
# if data if is type float, [0, 1] range is assumed
# otherwise [0, 255]
# add a nice wave pattern on top of the random data
x = np.linspace(0, 1, 1024)
y = np.linspace(0, 1, 1024)
X, Y = np.meshgrid(x, y)
data = 5. * np.sin(2 * np.pi * (X + Y**2))
# have a band of NaNs in the middle
data[512:562, :] = np.nan
# uncomment to see random data
# data = np.random.random((1024, 1024, 3))
# uncomment to see 2x2 image
# data = np.random.random((2, 2))
# data[0, 0] = np.nan
# the compression argument can only be set when the widget is created
# changing the compression argument does not affect the data transfer immediatly
# change the BQPLOT_IMAGE_GL_IMAGE_DATA_COMPRESSION env variable to change the default
# from 'none' to 'png'
# e.g. export BQPLOT_IMAGE_GL_IMAGE_DATA_COMPRESSION=png
image = ImageGL(image=data, scales=scales_image, compression="png")
figure.marks = (image,)
page = figure