【问题标题】:Hide several lines using CustomJS in Python Bokeh在 Python Bokeh 中使用 CustomJS 隐藏几行
【发布时间】:2021-10-06 18:52:12
【问题描述】:

假设我有 2 条不同的线路:

df[0]=fig.line(x = 'x', y = 'y',..., name = 'toto0',source=s0)
df[1]=fig.line(x = 'x', y = 'y',..., name = 'toto1',source=s1)

如果我想隐藏它们,我使用这段代码:

checkbox = CheckboxGroup(labels=['toto0','toto1'], active=2, width=100)
callback = CustomJS(args=dict(l0=df[0],l1=df[1],checkbox=checkbox),
code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
""")
checkbox.js_on_change('active', callback)
layout = row(fig,checkbox)
show(layout)

现在假设我有 20 条不同的行。 如何继续压缩下面的代码?

callback = CustomJS(args=dict(l0=df[0],...,l19=df[19],checkbox=checkbox),
code="""
l0.visible = 0 in checkbox.active;
l1.visible = 1 in checkbox.active;
...
l19.visible = 19 in checkbox.active;
""")

这是一个 Python 和一个 JavaScript 问题...谢谢!

【问题讨论】:

    标签: javascript python bokeh


    【解决方案1】:

    主要思想是将所有的linerenderes收集到一个列表中,并将这个列表传递给CustomJS。在那里,您可以再次遍历此列表并应用您的更改。

    最小示例

    import pandas as pd
    from bokeh.plotting import figure, show, output_notebook
    from bokeh.models import CheckboxGroup, CustomJS
    from bokeh.layouts import row
    output_notebook()
    
    df = pd.DataFrame(
        {'x':range(5), 
         'red':range(5), 
         'blue':list(range(5))[::-1], 
         'green':[2]*5}
    )
    
    fig = figure(width=300, height=300)
    
    line_renderer = []
    names = list(df.columns[1:])
    for name in names:
        line_renderer.append(
            fig.line(
                x = 'x', 
                y = name, 
                color=name,
                name =name, 
                source=df
            )
        )
    
    checkbox = CheckboxGroup(labels=names, active=list(range(len(names))), width=100)
    callback = CustomJS(args=dict(lines=line_renderer,checkbox=checkbox),
        code="""
        for(var i=0; i<lines.length; i++){
            lines[i].visible = checkbox.active.includes(i);
        }
        """
    )
    checkbox.js_on_change('active', callback)
    layout = row(fig,checkbox)
    show(layout)
    

    输出

    【讨论】:

    • 完美运行,谢谢! JS中的数组是我错过的
    猜你喜欢
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多