python - Bokeh:显示/隐藏图形的小部件

标签 python bokeh

希望按照 UI 的方式做一些事情,如下所示:Bokeh: Using Checkbox widget to hide and show plots其中我可以有选择地显示/隐藏一列数字中的整个数字。最好有一个下拉菜单(带有多个选择的选项菜单),我可以在其中选择显示哪些图(假设我可以命名这些图)。

我对JS不熟悉,有什么指导吗? (提前致谢)

我希望图像不再可见,下一个数字会像这样跳起来:

例如:enter image description here

我在生成的列中有多个数字:

from bokeh.io import output_file, show
from bokeh.layouts import column
from bokeh.plotting import figure

output_file("layout.html")

x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]

# create a new plot
s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)

# create another one
s2 = figure(plot_width=250, plot_height=250, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)

# create and another
s3 = figure(plot_width=250, plot_height=250, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)

# put the results in a column and show
show(column(s1, s2, s3))

最佳答案

绘图没有可见切换,至少从版本 0.13 开始是这样。因此,您必须重置布局小部件的 children 值。我不太确定您想要与下拉菜单进行什么交互。这是带有复选框的完整示例:

from bokeh.io import output_file, show
from bokeh.layouts import column, row
from bokeh.plotting import figure
from bokeh.models import CheckboxGroup, CustomJS

output_file("layout.html")

x = list(range(11))
y0 = x
y1 = [10 - i for i in x]
y2 = [abs(i - 5) for i in x]

s1 = figure(plot_width=250, plot_height=250, title=None)
s1.circle(x, y0, size=10, color="navy", alpha=0.5)

s2 = figure(plot_width=250, plot_height=250, title=None)
s2.triangle(x, y1, size=10, color="firebrick", alpha=0.5)

s3 = figure(plot_width=250, plot_height=250, title=None)
s3.square(x, y2, size=10, color="olive", alpha=0.5)

col = column(s1, s2, s3)

checkbox = CheckboxGroup(labels=["Plot 1", "Plot 2", "Plot 3"],
                         active=[0, 1, 2], width=100)

callback = CustomJS(args=dict(plots=[s1,s2, s3], col=col, checkbox=checkbox), code="""
const children = []
for (const i of checkbox.active) {
     children.push(plots[i])
} 
col.children = children
""")
checkbox.js_on_change('active', callback)

show(row(checkbox, col))

您可以使用MultiSelect执行类似的操作:

select = MultiSelect(options=[("0", "Plot 1"), ("1", "Plot 2"), ("2", "Plot 3")],
                       value=["0", "1", "2"], width=100)

callback = CustomJS(args=dict(plots=[s1,s2, s3], col=col, select=select), code="""
const children = []
for (const i of select.value) {
    children.push(plots[i])
}
col.children = children
""")
select.js_on_change('value', callback)

仅供引用,该代码有点草率 - 它依赖于 JS 隐式将“0”等字符串转换为数字。

关于python - Bokeh:显示/隐藏图形的小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52415577/

相关文章:

python - 与随机森林相比,SVM 性能较差

python - 在 Python 中处理配置文件

python - Bokeh 图未使用 show(p) 或 p.show() 显示

python - 如何最好地使用 python 创建一个简单的、交互式的、可共享的图?

python - 在 Heroku 上托管的 Flask 应用程序中渲染 Bokeh 图

python - 如何获取循环小数的长度?

python - 如何为多个 HTML 表单提供单一 View - Django

python - 在 pygame 中显示的缓冲区在 tkinter 中不显示

javascript - 在reveal.js中嵌入本地html文件

python - Bokeh 条形图无法正确显示