复选框组,散景,堆积条形图

时间:2020-04-16 07:38:45

标签: python bokeh stacked-chart user-interaction

我已经使用悬停工具在bokeh中制作了一个堆积的条形图,现在还想向该图添加一个CheckboxGroup。但是,我对Bokeh还是陌生的,所以我有点挣扎。我尤其不确定如何制作更新功能,我曾尝试启动它,但根本不了解它需要做什么:

    trees_to_plot = [tree_selection.labels[i] for i in 
                        tree_selection.active]
    temp = make_dataset(trees_to_plot,
                           bin_width = 40)
    # \TODO: add more code here!

图表本身的代码如下:

p = figure(x_range = districtName,plot_width = 900, plot_height=400, 
           title='Tree pr. district',toolbar_location= None)
# Stacked bar chart
renderers = p.vbar_stack(stackers=treeName,x='bydelsnavn',source=temp,
            width=0.5, color = colornames)

# Add the hover tool
for r in renderers:
    tree = r.name
    hover = HoverTool(tooltips=[
        ("%s" % tree, "@{%s}" % tree)
    ], renderers = [r])
    p.add_tools(hover)

p.xaxis.axis_label = 'Copenhagen city cistricts'
p.yaxis.axis_label = 'Tree counts'

# Creat the checkbox selection element
tree_selection = CheckboxGroup(labels=treeName, active = [0,1])
# Link the checkboxes to the information on the graph
tree_selection.on_change('active', update)

# Add to the CheckboxGroup to the figure
controls = WidgetBox(tree_selection)
layout = row(controls,p)
show(layout)

此代码将显示图表中的所有复选框,但显然复选框与图表本身之间没有发生任何交互。有人可以指出我正确的方向吗?

提前谢谢! :D

1 个答案:

答案 0 :(得分:0)

您正在使用show来创建静态HTML页面。但是要向on_change传递Python函数,要使其正常工作,您需要使用Bokeh服务器。请尝试遵循以下示例:https://docs.bokeh.org/en/latest/docs/user_guide/server.html#single-module-format 如果仍然无法运行,请确保发布无需修改即可运行的完整代码。