使用taptool在bokeh vbar上选择相应/多个条形图

时间:2018-01-26 22:43:42

标签: python bokeh

我目前正在处理体育相关测试的数据。我想通过几个测试(不同的日期)可视化一个运动员的(多维)测试数据,因此我将分组的vbar与最低分组级别的日期进行了对比。现在我想点击一个栏来选择它,并且也应该选择(并突出显示)同一日期的相应栏。 直到现在我正在使用" [python] [bokeh] taptool"来搜索stackoverflow。查询,我用标签" taptool"查找了关于git / bokeh的整个问题部分。并做了一个类似查询的谷歌,但我找不到匹配的帖子。

为了澄清我的需求,我修改了bokeh存储库中的grouped_bars_example。我的目标是通过单击一个栏选择一个制造商的所有栏。 (我知道可以按住shift键来进行多项选择,但是选择120个条中的6个相应的条很安静。但是我想找到一个有效的方法来做到这一点。只需点击一下

#### basic example code from ~/latest/docs/user_guide/categorical.html#grouped
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, HoverTool, TapTool
from bokeh.plotting import figure
from bokeh.palettes import Spectral5
from bokeh.sampledata.autompg import autompg_clean as df
from bokeh.transform import factor_cmap

# output_file('bars.html')

# preparing data for figure
df.cyl = df.cyl.astype(str)
df.yr = df.yr.astype(str)

group = df.groupby(('cyl', 'mfr'))

source = ColumnDataSource(group)
index_cmap = factor_cmap('cyl_mfr', palette=Spectral5, factors=sorted(df.cyl.unique()), end=1)

# setting up figure
p = figure(plot_width=800, plot_height=300, title="Mean MPG by # Cylinders and Manufacturer",
           x_range=group, toolbar_location=None, tools="")

# adding grouped vbar
p.vbar(x='cyl_mfr', top='mpg_mean', width=1, source=source,
       line_color="white", fill_color=index_cmap, )

# figurestyling
p.y_range.start = 0
p.x_range.range_padding = 0.05
p.xgrid.grid_line_color = None
p.xaxis.axis_label = "Manufacturer grouped by # Cylinders"
p.xaxis.major_label_orientation = 1.2
p.outline_line_color = None

# adding Tools
p.add_tools(HoverTool(tooltips=[("MPG", "@mpg_mean"), ("Cyl, Mfr", "@cyl_mfr")]))
p.add_tools(TapTool())


#### my additional code
import pandas as pd
import sys
from bokeh.plotting import curdoc, figure

# redirect output in files (just for debugging)
save_stderr = sys.stderr
f_err = open('error.log', 'w')
sys.stderr = f_err

save_stdout = sys.stdout
f_info = open('info.log', 'w')
sys.stdout = f_info

# callbackfunction to obtain selected bars
def callback_tap(attr, old, new):
    # write selected indices to file
    output = source.selected['1d']['indices']  # indices of selected bars
    print(output, type(output))

    # make calculations only, if one bar is selected
    if len(output) == 1:
        # find all corresponding indices to manufacturer based on selected bar
        #   get manufacturer corresponding to retrieved index
        man = source.data['cyl_mfr'][output][1]
        #   temporary DataFrame
        tmp = pd.DataFrame(source.data['cyl_mfr'].tolist(), columns=['cyl', 'mfr'])
        #   look up all corresponding indices for manufacturer "man"
        indices = tmp.index[tmp.mfr == man].values.tolist()

        # assing list of indices
        source.selected['1d']['indices'] = indices

# assigning callbackfunction
source.on_change('selected', callback_tap)

curdoc().add_root(p)

请注意,我更改输出以运行散景服务器以便进行自定义python回调。出于调试原因,我将输出重定向到文件。 在我的回调函数中,第一部分工作正常,我检索所选栏的indicees。另外,我找到了带有DataFrame的相应的indicees,但最后我很难以某种方式分配新的indicees,更新了vbar图。

如果有人可以帮助我,我会非常高兴。

0 个答案:

没有答案