我最近发现了有关holoviews的信息,而hv.Image方法是plt.image的不错的选择。有一个非常酷的功能,称为hv.HoloMap,它允许输入一个函数并在函数中调整参数以交互方式查看生成的2D数组。我尝试了一些启动HoloMap对象和替代的dynamicMap对象的示例,但是无法使其与我的数据配合使用。 (http://holoviews.org/Tutorials/Showcase.html

在我的真实数据集中,我将拥有3D数组,我想沿一个轴进行切片(在本例中为z),在该轴上可以交互地查看生成的切片。我用下面的numpyxarray做了一个基本示例:

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

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),

最佳答案

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

HoloMap非常类似于字典,您可以用(键,值)对填充它,然后可以使用小部件探索该数据的可视化。在构造DynamicMap时,它不包含任何项,而是定义一个回调函数,该回调函数在小部件(或您)请求特定键时得到评估。这意味着您可以在动态Dimension上定义连续范围或离散样本列表,从而使您可以探索比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 HoloMap by defining the sampling on the Dimension
dmap = hv.DynamicMap(image_slice, kdims=[hv.Dimension('z_axis', values=keys)])


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

关于python - 如何在Holoviews中使用HoloMap来查看2D数组切片(Python 3.5.1),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36989139/

10-10 19:32