我最近发现了holoviews
,而hv.Image
方法是plt.image
的一个很好的替代方法。有一个非常酷的功能叫hv.HoloMap
,允许用户输入一个函数并调整函数内的参数,以交互方式查看生成的2D数组。我尝试了一些启动HoloMap
对象和另一个dynamicMap
对象的示例,但无法使用它来处理我的数据。 (http://holoviews.org/Tutorials/Showcase.html)
在我的真实数据集中,我将拥有3D数组,我想沿着轴(在这种情况下为z
)切片,在那里我可以交互式地查看生成的切片。我在numpy
和xarray
下面做了一个基本的例子:
如何使用image_slice
(或z
)对象构建基本函数hv.HoloMap
(迭代hv.dynamicMap
维度)以查看2D切片我的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
我使用Python 3.5.1
和Holoviews Version((1, 4, 3),
答案 0 :(得分:2)
首先感谢您的兴趣,我是HoloViews的作者之一。了解HoloMap
和DynamicMap
之间的区别非常重要。
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来探索它是一个多维数组。