Rgb

Title
RGB Element
Dependencies
Matplotlib
Backends
Matplotlib
Bokeh
In [1]:
import numpy as np
import holoviews as hv
hv.extension('matplotlib')

The RGB element is a subclass of Image that supports red, green, blue channels. One of the simplest ways of creating an RGB element is to load an image file (such as PNG) off disk, using the load_image classmethod:

In [2]:
hv.RGB.load_image('../assets/penguins.png')
Out[2]:

Although convenient, this method doesn't show how an RGB is constructed from arrays. The next example shows that the input can be a simple stack of two-dimensional numpy arrays:

In [3]:
x,y = np.mgrid[-50:51, -50:51] * 0.1

r = 0.5*np.sin(np.pi  +3*x**2+y**2)+0.5
g = 0.5*np.sin(x**2+2*y**2)+0.5
b = 0.5*np.sin(np.pi/2+x**2+y**2)+0.5

hv.RGB(np.dstack([r,g,b]))
Out[3]:

You can see how the RGB object is created from the original channels:

In [4]:
%%opts Image (cmap='gray')
hv.Image(r,label="R") + hv.Image(g,label="G") + hv.Image(b,label="B")
Out[4]:

RGB also supports an optional alpha channel, which will be used as a mask revealing or hiding any Element s it is overlaid on top of:

In [5]:
%%opts Image (cmap='gray')
mask = 0.5*np.sin(0.2*(x**2+y**2))+0.5
rgba = hv.RGB(np.dstack([r,g,b,mask]))

bg = hv.Image(0.5*np.cos(x*3)+0.5, label="Background") * hv.VLine(x=0,label="Background")
overlay = (bg*rgba).relabel("RGBA Overlay")
bg + hv.Image(mask,label="Mask") + overlay
Out[5]:

Download this notebook from GitHub (right-click to download).