2016-05-06 44 views
0

我有一个数据集,我正在绘制汇总统计数据,我希望能够选择一个有趣的点并在底下绘制原始数据子图。下面的代码可以从一个jupyter笔记本运行,看看输出应该是什么样子。理想情况下,我将能够点击第一个图中靠近(1,10)的点,并在第二个图中查看聚集在10左右的原始数据。如何连接汇总统计和原始数据与情景的阴谋

底部附近注释掉的代码是我定义回调的尝试,但我认为我对于如何使用taptools和回调有个基本的误解。我如何告诉散景,它应该调用具有特定参数的特定python例程(基于点击的点),然后重新加载第二个数字?

from string import ascii_lowercase 

import numpy as np 
import pandas as pd 

from bokeh.plotting import figure, output_notebook, show, gridplot 
from bokeh.models import ColumnDataSource, widgets, HoverTool, TapTool 

output_notebook() 

class RawPlot(): 
    def __init__(self, fig, col, raw): 
     self.fig = fig 
     self.raw = raw 
     self.circ = self.fig.circle(x='index', y='raw', size=1, 
            source=ColumnDataSource(raw[col].reset_index())) 
    # ideally I would have a callback to RawPlot.update to change the underlying data 
    def update(self, col): 
     self.circ.data_source = ColumnDataSource(self.raw[col].reset_index()) 

# generate the example data 
rawdat = pd.DataFrame(np.random.random((1024, 4)) + np.arange(4)*10, 
         columns=pd.MultiIndex.from_tuples([(s, 'raw') for s in ascii_lowercase[:4]])) 
# compute summary statistics and show in a bokeh figure 
stats = rawdat.describe().T.reset_index() 
pstat = figure() 
pstat.circle(x='index', y='mean', source=ColumnDataSource(stats), size=12) 

# show the raw data of the first column 
praw = figure() 
rawplot = RawPlot(praw, 'a', rawdat) 
# this was my attempt at being able to change which column's raw data was plotted. It failed 
# taptool = pstat.select(type=TapTool) 
# taptool.callback = rawplot.update("@level_0") 
show(gridplot([[pstat, praw]])) 
+0

这是一个Bokeh服务器应用程序吗?然后,您想使用数据源的'on_change'处理程序来设置回调。 '.callback'属性只是设置一个'CustomJS'回调,即一个JavaScript回调,根据定义,它永远不会调用或执行任何Python代码。如果您想用* python *代码响应选择或小部件事件,则必须使用Bokeh服务器(启用该用例是它的主要目的)。 – bigreddot

+0

这不是一个散景服务器应用程序呢。我刚刚发现散景文档的相关部分告诉我,我不能只是在笔记本上做所有事情。 – Elliot

+0

笔记本中的另一种选择是'push_notebook' http://bokeh.pydata.org/en/0.11.1/docs/user_guide/notebook.html#jupyter-interactors该示例使用小部件,但是您可以使用'push_notebook'无需小部件即可更新数据。 – bigreddot

回答

0

下面的代码以我想要的方式处理交互。它与测试案例足够好地运行在散景服务器上。但是,从笔记本运行代码的交互性并不相同(选择pstat中的数据不会更新praw中的数据)。因此output_notebookpush_notebook被注释掉。

from string import ascii_lowercase 

import numpy as np 
import pandas as pd 

from bokeh.plotting import figure, show, gridplot 
from bokeh.models import ColumnDataSource, widgets, HoverTool, TapTool, BoxSelectTool 
from bokeh.io import output_notebook, push_notebook, output_server 
from bokeh.resources import INLINE 

# generate the example data 
rawdat = pd.DataFrame(np.random.random((1024, 4)) + np.arange(4)*10, 
         columns=pd.MultiIndex.from_tuples([(s, 'raw') for s in ascii_lowercase[:4]])) 
# compute summary statistics and show in a bokeh figure 
stats = rawdat.describe().T.reset_index() 
statsource = ColumnDataSource(stats[['mean']]) 

TOOLS = [TapTool(), BoxSelectTool()] 
pstat = figure(tools=TOOLS) 
pstat.circle(x='index', y='mean', source=statsource, size=12) 

# show the raw data of the first column 
praw = figure() 
col = rawdat.columns.levels[0][0] 
rawsource = ColumnDataSource(rawdat[col].reset_index()) 
circ = praw.circle(x='index', y='raw', size=1, 
        source=rawsource) 

# update the raw data_source when a new summary is chosen 
def update(attr, old, new): 
    ind = new['1d']['indices'][0] 
    col = rawdat.columns.levels[0][ind] 
    rawsource.data['raw'] = rawdat[col].values.ravel() 
    # push_notebook() 
statsource.on_change('selected', update) 

# serve the figures 
output_server("foo") 
# output_notebook() 
show(gridplot([[pstat, praw]]))