我们可以轻松地使用Bokeh制作交互式图形,用户可以在其中切换行的开和关,但我还没有找到默认情况下(即在用户选中复选框之前)隐藏行的方法。例如,下面的代码可以工作,但是当用户首次加载页面时,默认情况下所有行均处于打开状态,即使它们都不处于活动状态(active = [])。然后,用户必须选择任何复选框以使它们消失,然后选择给定的复选框以显示给定的行笨拙。那么,在用户选择其中之一之前,默认情况下如何隐藏行?
from bokeh.io import output_file, show
from bokeh.layouts import row
from bokeh.plotting import figure
from bokeh.models import CheckboxGroup, CustomJS
output_file("toggle_lines.html")
### Main plot
plot = figure()
# Dummy data for testing
x = list(range(90))
y0 = [ a**1.5 for a in x]
y1 = [ a**1.55 for a in x]
y2 = [ a**1.60 for a in x]
y3 = [ a**1.65 for a in x]
l0 = plot.line(x, y0, color='red')
l1 = plot.line(x, y1, color='red')
l2 = plot.line(x, y2, color='red')
l3 = plot.line(x, y3, color='red')
checkbox = CheckboxGroup(labels=["l0", "l1", "l2", "l3"], active=[])
checkbox.callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2, l3=l3, checkbox=checkbox),
code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
l2.visible = 2 in checkbox.active;
l3.visible = 2 in checkbox.active;
""")
layout = row(checkbox, plot)
show(layout)
答案 0 :(得分:1)
您可以在python代码中为要开始隐藏的任何内容设置.visible = False
。
答案 1 :(得分:0)
我只是想说明这里的JS代码是错误的,正确的代码是
l0.visible = checkbox.active.includes(0);
等等等等