如何使用Datashader + Bokeh后端在HoloViews中进行链接数据选择 [英] How to do linked data selections in HoloViews with Datashader + Bokeh backend

查看:142
本文介绍了如何使用Datashader + Bokeh后端在HoloViews中进行链接数据选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我从补充HoloViews开发人员开始,这真是太了不起了.内容很多,很难弄清楚如何将它们组合在一起来完成我想要的事情:).

Let me just start by complementing the HoloViews developers, this thing is pretty amazing. There are just a lot of pieces and it is a bit hard to figure out how to put them all together to do what I want:).

我在这里尝试进行链接的多维数据绘图,即我想拥有多个绘图,以显示同一数据在不同维度上的视图.然后,我想利用Bokeh选择工具在其中一个图中选择数据,并查看其他图中的数据.但是我还需要使用Datashader来做到这一点,因为我的数据集很大.

I am trying here to do linked multidimensional data plotting, i.e. I want to have several plots showing views of the same data along various dimensions. I want then to harness the Bokeh selection tools to select data in one of the plots, and see where it is in the others. But I also need to use Datashader to do it, because my datasets are large.

这是我到目前为止(在Jupyter笔记本中运行,带有python 2)

This what I have so far (running in a Jupyter notebook, with python 2)

import numpy as np
import pandas as pd
import holoviews as hv
import holoviews.operation.datashader as hvds
hv.notebook_extension('bokeh')
%opts Scatter [tools=['box_select', 'lasso_select']] (size=10 nonselection_color='red' color='blue') Layout [shared_axes=True shared_datasource=True]

# Create some data to plot
x1 = np.arange(0,10,1e-2)
x2 = np.arange(0,10,1e-2)
X1,X2 = np.meshgrid(x1,x2)
x1 = X1.flatten()
x2 = X2.flatten()
x3 = np.sin(x1) * np.cos(x2)
x4 = x1**2 + x2**2

# Pandas dataframe object from the data 
print "Creating Pandas dataframe object"
df = pd.DataFrame.from_dict({"x1": x1, "x2": x2, "x3": x3, "x4": x4})

# Put the dataframe into a HoloViews table
dtab = hv.Table(df)

# Make some linked scatter plots using datashader
scat1 = dtab.to.scatter('x1', 'x2', [])
scat2 = dtab.to.scatter('x1', 'x3', [])
scat3 = dtab.to.scatter('x2', 'x4', [])
hvds.datashade(scat1) + hvds.datashade(scat2) + hvds.datashade(scat3)

这将产生以下内容

这非常简单.但是,它并不能完全满足我的要求.数据范围的变化和平移是链接在一起的,这非常酷,但是一个图的范围之外的数据仍然可以绘制在另一个图上.我希望该数据从所有图中消失,以便只看到属于所有已查看数据范围的数据,以便可以动态选择一些超多维数据集以在多维空间中突出显示.

which is pretty fantastically simple. However it doesn't quite do what I want. The changes of data ranges and panning are linked, which is very cool, however data outside the range of one plot still can get plotted on the others. I would like to have that data disappear from all plots, so that I only see the data that falls within all the viewed data ranges, so that one can dynamically select some hypercube of data to highlight in the multidimensional space.

此外,最好使用Bokeh选择工具以相同的方式工作,例如,我可以选择一个图上的某些点,然后将它们全部显示为红色,或者在其他图上以其他方式显示.尽管我要求"box_select"和"lasso_select",但我什至没有得到选择工具.不过,我可能错误地要求了它们,但我现在还不清楚HoloViews如何传递选项.

In addition, it would be good to have the Bokeh selection tools work the same way, so that for instance I could select some points on one plot and have them all show up in red or something on the other plots. I am not even getting the selection tools at all though, despite asking for 'box_select' and 'lasso_select'. I probably asked for them incorrectly though, it is not really clear to me how HoloViews passes options around.

推荐答案

从詹姆斯的答案开始工作( https://stackoverflow.com/a/44288019/1447953 )我将问题中的示例扩展到以下内容.它以一个图作为主"控制源,并且仅将出现在该图的数据范围内的数据绘制到一堆从"图上.拥有双向关系会很好,但这确实很酷.

Working from James' answer (https://stackoverflow.com/a/44288019/1447953) I extended the example in the question to the following. It takes one plot as the "master" control source, and plots only data that appears within the data ranges of that plot onto a bunch of "slave" plots. It would be nice to have a dual-way relationship, but this is pretty cool as it is.

import numpy as np
import pandas as pd
import holoviews as hv
import holoviews.operation.datashader as hvds
hv.notebook_extension('bokeh')
%opts Layout [shared_axes=False shared_datasource=True]

# Create some data to plot
x1 = np.arange(0,10,1e-2)
x2 = np.arange(0,10,1e-2)
X1,X2 = np.meshgrid(x1,x2)
x1 = X1.flatten()
x2 = X2.flatten()
x3 = np.sin(x1) * np.cos(x2)
x4 = x1**2 + x2**2

# Pandas dataframe object from the data 
print "Creating Pandas dataframe object"
df = pd.DataFrame.from_dict({"x1": x1, "x2": x2, "x3": x3, "x4": x4})

# Make some linked scatter plots using datashader
x1_x2 = hv.Points(df[['x1', 'x2']])
#x1_x3 = hv.Points(df[['x1', 'x3']])
#x2_x4 = hv.Points(df[['x2', 'x4']])

from holoviews import streams

maindata=x1_x2
mainx='x1'
mainy='x2'
def create_dynamic_map(xvar,yvar):
    def link_function(x_range, y_range):
        x_min = x_range[0]; x_max = x_range[1]
        y_min = y_range[0]; y_max = y_range[1]
        pts = hv.Points(df[  (getattr(df,mainx) > x_min) & (getattr(df,mainx) < x_max) 
                           & (getattr(df,mainy) > y_min) & (getattr(df,mainy) < y_max) 
                          ][[xvar, yvar]])
        return pts
    dmap = hv.DynamicMap(link_function, 
                     streams=[hv.streams.RangeXY(x_range=(-100,100), 
                                                 y_range=(-100,100), 
                                                 source=maindata)],
                     kdims=[])
    return dmap

x1_x3 = create_dynamic_map('x1','x3')
x2_x4 = create_dynamic_map('x2','x4')

hvds.datashade(x1_x2) + hvds.datashade(x1_x3) + hvds.datashade(x2_x4)

这篇关于如何使用Datashader + Bokeh后端在HoloViews中进行链接数据选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆