【问题标题】:using MultiSelect widget to hide and show lines in bokeh使用 MultiSelect 小部件在散景中隐藏和显示线条
【发布时间】:2016-08-01 17:50:17
【问题描述】:

我正在处理四组数据,每组数据都有多个时间序列。我正在使用散景将它们全部绘制在一起,结果如下所示:

multiline graph bokeh with widget

from bokeh.plotting import figure, output_file, show
from bokeh.palettes import RdYlGn4
from bokeh.models import CustomJS, ColumnDataSource, MultiSelect
from bokeh.layouts import row, widgetbox

output_file("graph.html")
p = figure(plot_width=1000, plot_height=400, x_axis_type="datetime", title="title")
cdn = range(4)
for i,comp in enumerate(cdn):
    ts=[t for t in data_plu_price.columns if int(t) in df.T[df.C==comp].values]
    n_lines=len(data[ts].columns) 
    p.multi_line(xs=[data[ts].index.values]*n_lines, ys=[data[t].values for t in ts],line_color=RdYlGn4[i], legend=str(i))
p.title.align = "center"
p.title.text_font_size = "20px"
p.xaxis.axis_label = 'date'
p.yaxis.axis_label = 'val'

callback = CustomJS("""Some Code""")
multi_select = MultiSelect(title="Select:", value=cdn,
                       options=[(str(i), str(i)) for i in range(4)])
layout = row(p,widgetbox(multi_select))
show(layout)

问题是它看起来真的很乱,所以我想使用多选小部件来显示/隐藏所有多行组(4)。如何使用带有multi_line 绘图的多选小部件?

提前致谢。

【问题讨论】:

    标签: python python-2.7 widget bokeh


    【解决方案1】:

    在此 PR 的 0.12.1 版中刚刚添加了对执行此操作的支持(使用 MultiSelect 小部件隐藏/显示行):https://github.com/bokeh/bokeh/pull/4868

    这里有一个例子(复制如下):https://github.com/bokeh/bokeh/blob/master/examples/plotting/file/line_on_off.py

    """ Example demonstrating turning lines on and off - with JS only
    
    """
    
    import numpy as np
    
    from bokeh.io import output_file, show
    from bokeh.layouts import row
    from bokeh.palettes import Viridis3
    from bokeh.plotting import figure
    from bokeh.models import CheckboxGroup, CustomJS
    
    output_file("line_on_off.html", title="line_on_off.py example")
    
    code = """
        if (0 in checkbox.active) {
            l0.visible = true
        } else {
            l0.visible = false
        }
        if (1 in checkbox.active) {
            l1.visible = true
        } else {
            l1.visible = false
        }
        if (2 in checkbox.active) {
            l2.visible = true
        } else {
            l2.visible = false
        }
    """
    
    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="Line 0", **props)
    l1 = p.line(x, 4 * np.cos(x), color=Viridis3[1], legend="Line 1", **props)
    l2 = p.line(x, np.tan(x), color=Viridis3[2], legend="Line 2", **props)
    
    callback = CustomJS(code=code, args={})
    checkbox = CheckboxGroup(labels=["Line 0", "Line 1", "Line 2"], active=[0, 1, 2], callback=callback, width=100)
    callback.args = dict(l0=l0, l1=l1, l2=l2, checkbox=checkbox)
    
    layout = row(checkbox, p)
    show(layout)
    

    【讨论】:

    • 监听器的实现有问题,您可以自己尝试原始代码并检查框没有隐藏必须的行,可能是js和chrome的交叉浏览问题。如果你用 'checkbox.active.includes(1)' 改变所有的句子 '(checkbox.active)' 就可以了。即便如此,答案真的很有帮助
    • checkbox.active.includes 在 IE11 上导致 SCRIPT438: Object doesn't support property or method 'includes' File: Function code (1), Line: 3, Column: 5
    • 我同意@pablo-a 他的评论。这不会隐藏正确的行,只是选择的前 n 行,因此如果您选择第 0 行和第 2 行,它会显示 0 和 1(前 2 行)
    • 我对 javascript 了解的不够多,但这可能是因为in 签入了数组中的键而不是值?
    • 这确实是问题所在。而不是测试0 in checkbox.active 你应该测试checkbox.active.indexOf(0) > -1
    猜你喜欢
    • 2015-11-22
    • 2011-09-16
    • 2017-06-18
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2017-08-09
    • 2021-06-15
    • 2017-10-11
    相关资源
    最近更新 更多