复选框组、散点图、堆叠条形图



我用悬停工具在散焦图中制作了一个堆叠条形图,现在还想在图中添加一个CheckboxGroup。然而,我对博克是个新手,所以我有点吃力。我特别不确定如何使更新功能,我已经尝试过启动它,但根本不明白它需要做什么:

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

您使用的是创建静态HTML页面的show。但是,对于on_change,您需要传递一个Python函数,为此需要使用Bokeh服务器。请尝试以下示例:https://docs.bokeh.org/en/latest/docs/user_guide/server.html#single-模块格式如果它仍然不起作用,请确保发布完整的代码,这些代码可以在不做任何修改的情况下运行。

最新更新