Search code examples
pythonholoviews

Holoviews: How to wrap multiple RGB images into a Dataset and display them correctly?


I have a dataset of RGB images as a numpy array of shape (n, height, width, 3). I want to wrap the data into a hv.Dataset and visualize it (with bokeh) such that:

  • A slider can be used to select one of the n images.
  • Each image is displayed in its correct aspect ratio and orientaton.

I'm primarily interested in a solution without xarray.


Solution

  • Something like this should work, though I think there's probably a cleaner approach declaring an hv.Dataset wrapping the input array:

    import numpy as np, holoviews as hv
    hv.extension('bokeh')
    
    x,y = np.mgrid[-50:51, -100:101] * 0.1
    
    def ms(k):
        r = 0.5*np.sin(np.pi  +k*x**2+y**2)+0.5
        g = 0.5*np.sin(x**2+k*y**2)+0.5
        b = 0.5*np.sin(np.pi/k+x**2+y**2)+0.5
        return np.dstack([r,g,b])
    
    a = np.stack([ms(i) for i in [1,2,3,4]])
    
    hv.HoloMap({i:hv.RGB(a[i-1,:,:,:], bounds=(-1,-0.5,1,0.5)).opts(data_aspect=1) for i in [1,2,3,4]})
    

    enter image description here