1

我最近发现holoviews并且该hv.Image方法是plt.image. 有一个非常酷的功能hv.HoloMap,它允许人们输入一个函数并调整函数内的参数,以交互方式查看生成的二维数组。我尝试遵循一些启动HoloMap对象和替代dynamicMap对象的示例,但无法让它与我的数据一起使用。(http://holoviews.org/Tutorials/Showcase.html

在我的真实数据集中,我将拥有 3D 数组,并且我想沿一个轴(z在这种情况下)进行切片,在该轴上我可以交互地查看生成的切片。我在下面做了一个基本的numpy例子xarray

如何使用我的(或)对象构建我的基本函数image_slice(迭代z维度)以查看我的 3D DataArray 的 2D 切片?hv.HoloMaphv.dynamicMap

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

我使用Python 3.5.1Holoviews Version((1, 4, 3),

4

1 回答 1

3

首先感谢您的关注,我是 HoloViews 的作者之一。HoloMap了解 a和之间的区别很重要DynamicMap

HoloMap 很像字典,您使用(键,值)对填充它,然后您可以使用小部件探索该数据的可视化。DynamicMap 在您构造它时不包含任何项目,而是定义一个回调函数,当小部件(或您)请求特定键时,该函数会被评估。这意味着您可以在动态维度上定义连续范围或离散样本列表,让您探索比 HoloMap 更大的空间。

以您的示例为例,您可以通过以下方式构造 HoloMap 和 DynamicMap:

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(k):
    return(hv.Image(DA_3D[k,:,:].values))

keys = list(range(10))

# Construct a HoloMap by evaluating the function over all the keys
hmap = hv.HoloMap([(k, image_slice(k)) for k in keys], kdims=['z_axis'])

# Construct a DynamicMap by defining the sampling on the Dimension
dmap = hv.DynamicMap(image_slice, kdims=[hv.Dimension('z_axis', values=keys)])

如果您还有其他问题,可以加入我们的Gitter。请注意,我们计划将 xarray 与 HoloViews 正确集成,因此您不必手动定义 HoloMap/DynamicMap 来探索它的多维数组。

于 2016-05-08T15:22:05.200 回答