散景线 on/off 示例无法正常工作
Bokeh line on/off example is not working as it should
我正在使用 Bokeh CustomJS 过滤器,并且对打开和关闭绘图元素很感兴趣,并且我正在使用 the line_on_off.py example 而不对代码进行任何更改。当我在 Jupyter notebook 中绘制它时,我 运行 遇到了一个问题:无论我先关闭哪个复选框,最后一行(黄色切线图)总是先关闭。例如,如果我先关闭“Line 0”按钮,最后一行就会消失,与先关闭“Line 1”和“Line 2”按钮一样。此外,关闭任何第二个复选框总是会关闭“第 1 行”图(第二个图)。
看起来代码总是关闭最后一个可见的渲染图,无论实际单击哪个复选框按钮。我已经阅读了大量有关 Bokeh 的 CustomJS 的资料,并且在这里遇到了使用过相同示例的用户提出的多个问题,似乎没有我遇到的问题。示例中有错误还是我没有正确使用 js 代码?
我正在使用:
- python 3.7.6
- 背景虚化 2.3.3
- jupyter 1.0.0.
感谢任何帮助,谢谢!
在我的 jupyter notebook 上进行的实验中,我能够重现您描述的行为。在 JavaScript 部分进行一些小的改动,这个例子就完美了。
我将 0 in checkbox.active
更改为 checkbox.active.includes(0)
并且 1 和 2 也一样。我的猜测是第一个正在寻找索引而不是值,但我不是 JavaScript.
这是完整的工作示例。
import numpy as np
from bokeh.io import output_notebook, show
from bokeh.layouts import row
from bokeh.models import CheckboxGroup, CustomJS
from bokeh.palettes import Viridis3
from bokeh.plotting import figure
output_notebook()
p = figure()
props = dict(line_width=4, line_alpha=0.7)
x = np.linspace(0, 4 * np.pi, 100)
l0 = p.line(x, np.sin(x), color=Viridis3[0], legend_label="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend_label="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend_label="Line 2", **props)
checkbox = CheckboxGroup(labels=["Line 0", "Line 1", "Line 2"],
active=[0, 1, 2], width=100)
callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2, checkbox=checkbox), code="""
console.log(checkbox.active)
l0.visible = checkbox.active.includes(0);
l1.visible = checkbox.active.includes(1);
l2.visible = checkbox.active.includes(2);
""")
checkbox.js_on_change('active', callback)
layout = row(checkbox, p)
show(layout)
我正在使用 Bokeh CustomJS 过滤器,并且对打开和关闭绘图元素很感兴趣,并且我正在使用 the line_on_off.py example 而不对代码进行任何更改。当我在 Jupyter notebook 中绘制它时,我 运行 遇到了一个问题:无论我先关闭哪个复选框,最后一行(黄色切线图)总是先关闭。例如,如果我先关闭“Line 0”按钮,最后一行就会消失,与先关闭“Line 1”和“Line 2”按钮一样。此外,关闭任何第二个复选框总是会关闭“第 1 行”图(第二个图)。
看起来代码总是关闭最后一个可见的渲染图,无论实际单击哪个复选框按钮。我已经阅读了大量有关 Bokeh 的 CustomJS 的资料,并且在这里遇到了使用过相同示例的用户提出的多个问题,似乎没有我遇到的问题。示例中有错误还是我没有正确使用 js 代码?
我正在使用:
- python 3.7.6
- 背景虚化 2.3.3
- jupyter 1.0.0.
感谢任何帮助,谢谢!
在我的 jupyter notebook 上进行的实验中,我能够重现您描述的行为。在 JavaScript 部分进行一些小的改动,这个例子就完美了。
我将 0 in checkbox.active
更改为 checkbox.active.includes(0)
并且 1 和 2 也一样。我的猜测是第一个正在寻找索引而不是值,但我不是 JavaScript.
这是完整的工作示例。
import numpy as np
from bokeh.io import output_notebook, show
from bokeh.layouts import row
from bokeh.models import CheckboxGroup, CustomJS
from bokeh.palettes import Viridis3
from bokeh.plotting import figure
output_notebook()
p = figure()
props = dict(line_width=4, line_alpha=0.7)
x = np.linspace(0, 4 * np.pi, 100)
l0 = p.line(x, np.sin(x), color=Viridis3[0], legend_label="Line 0", **props)
l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend_label="Line 1", **props)
l2 = p.line(x, np.tan(x), color=Viridis3[2], legend_label="Line 2", **props)
checkbox = CheckboxGroup(labels=["Line 0", "Line 1", "Line 2"],
active=[0, 1, 2], width=100)
callback = CustomJS(args=dict(l0=l0, l1=l1, l2=l2, checkbox=checkbox), code="""
console.log(checkbox.active)
l0.visible = checkbox.active.includes(0);
l1.visible = checkbox.active.includes(1);
l2.visible = checkbox.active.includes(2);
""")
checkbox.js_on_change('active', callback)
layout = row(checkbox, p)
show(layout)