I recently found out about holoviews
and the hv.Image
method is a nice alternative to plt.image
. There is a really cool feature called hv.HoloMap
that allows one to input a function and adjust parameters within the function to interactively view the resulting 2D array. I tried following a few examples of initiating the HoloMap
object and an alternative dynamicMap
object but couldn't get it to work with my data. (http://holoviews.org/Tutorials/Showcase.html)
In my real datasets, I will have 3D array and I would like to slice along an axis (z
in this case) where I could interactively view the resulting slices. I made a basic example with numpy
and xarray
below:
How can I structure my basic function image_slice
(iterate over the z
dimension) with my hv.HoloMap
(or hv.dynamicMap
) object to view 2D slices of my 3D DataArray?
import xarray as xr
import numpy as np
import holoviews as hv; hv.notebook_extension()
#Building 2D Array (X & Y)
dist = np.linspace(-0.5,0.5,202) # Linear spatial sampling
XY,YX = np.meshgrid(dist, dist)
#Add along 3rd Dimension
Z_list = []
for i in range(10):
Z_list.append(xr.DataArray(XY*i,dims=["x","y"]))
#Concat list of 2D Arrays into a 3D Array
DA_3D = xr.concat(Z_list,dim="z")
# DA_3D.shape
# (10, 202, 202)
def image_slice(DA_var,k):
return(hv.Image(DA_var[k,:,:].values))
#http://holoviews.org/Tutorials/Showcase.html Interactive Exploration w/ Circular Wave example
keys = [(DA_3D,k) for k in range(10)] #Every combination
items = [(k, image_slice(*k)) for k in keys]
# visual_slice = hv.HoloMap(items)
# TypeError: unhashable type: 'DataArray
dmap = hv.DynamicMap(slice_image, kdims=[hv.Dimension('z_axis',range=(0, 10))])
# dmap
# TypeError: slice_image() missing 1 required positional argument: 'k'
# Which makes perfect sense because the first argument is the DataArray object but I don't know how to input that into this type of object since `hv.Dimension` requires a range
I use Python 3.5.1
and Holoviews Version((1, 4, 3),