繁体   English   中英

如何将摘要统计信息和原始数据的图与bokeh连接起来

[英]How do I connect plots of summary statistics and raw data with bokeh

我有一个要绘制汇总统计数据的数据集,我希望能够选择一个有趣的点,并在一个单独的子图中绘制基础原始数据。 可以从jupyter笔记本运行以下代码,以查看输出结果。 理想情况下,我可以在第一个绘图中单击(1,10)附近的点,并在第二个绘图中看到原始数据聚集在10附近。

在底部附近被注释掉的代码是我定义回调的尝试,但是我认为我对taptools和回调应该如何工作有根本的误解。 我如何告诉bokeh它应该调用带有特定参数的特定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服务器一起很好地用于测试用例。 但是,从笔记本计算机运行代码不会具有相同的交互性(在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]]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM