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]]))
这是一个Bokeh服务器应用程序吗?然后,您想使用数据源的'on_change'处理程序来设置回调。 '.callback'属性只是设置一个'CustomJS'回调,即一个JavaScript回调,根据定义,它永远不会调用或执行任何Python代码。如果您想用* python *代码响应选择或小部件事件,则必须使用Bokeh服务器(启用该用例是它的主要目的)。 – bigreddot
这不是一个散景服务器应用程序呢。我刚刚发现散景文档的相关部分告诉我,我不能只是在笔记本上做所有事情。 – Elliot
笔记本中的另一种选择是'push_notebook' http://bokeh.pydata.org/en/0.11.1/docs/user_guide/notebook.html#jupyter-interactors该示例使用小部件,但是您可以使用'push_notebook'无需小部件即可更新数据。 – bigreddot