首页 > 解决方案 > CheckboxGroup、散景、堆积条形图

问题描述

我用悬停工具在散景中制作了一个堆积条形图,现在还想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

标签: pythonbokehstacked-chartuser-interaction

解决方案


You're using show which creates a static HTML page. But to on_change, you pass a Python function, and for that to work you need to use a Bokeh server. Try follow this example: https://docs.bokeh.org/en/latest/docs/user_guide/server.html#single-module-format If it still doesn't work, make sure to post the complete code that can be run without any modifications.


推荐阅读