首页 > 解决方案 > Bokeh 中的 CheckboxGroup 绘制加法图

问题描述

我想使用一个复选框组来表示线性加法源。这个想法是让用户能够打开或关闭不同类型的多个源以显示具有附加值的图。

我实现了一个下拉版本,其中数据具有不同的组合作为数据框的列,因此代码的 CustomJS 部分如下:

 callback = CustomJS(args={'source':source},code="""
        // print the selectd value of the select widget - 
        // this is printed in the browser console.
        // cb_obj is the callback object, in this case the select 
        // widget. cb_obj.value is the selected value.
        console.log(' changed selected option', cb_obj.value);

        // create a new variable for the data of the column data source
        // this is linked to the plot
        var data = source.data;

        // allocate the selected column to the field for the y values
        data['A1'] = data[cb_obj.value];

        // register the change - this is required to process the change in 
        // the y values
        source.change.emit();
""")

我不太了解Javascript。有没有办法只给出一个数据框,如下所示:

Wave    A    B   C
340    77   70  15
341    80   73  15
342    83   76  16
343    86   78  17

并具有复选框组按钮,如果用户选择“A”和“C”,则该图将更改为 Wave 与 A+C 的图,反之亦然以删除选择。

我发现了一个类似的问题,但它只是显示/隐藏了图表并且不会更改数据:https ://stackoverflow.com/a/38706613/8565759

我在真实数据中有太多选择,无法手动分配组合。

标签: python-3.xpandasbokehbokehjs

解决方案


是的,您可以使用 CheckboxGroup 来实现 - 您可以使用activeCheckboxGroup 的属性来选择要添加到组合图中的正确列。这是您提供的数据的完整示例:

from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.layouts import layout, widgetbox
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import CheckboxGroup
import pandas as pd

output_file('additive_checkbox.html')
names = ['Wave', 'A', 'B', 'C']
rows = [(340, 77, 70, 15),
        (341, 80, 73, 15),
        (342, 83, 76, 16),
        (343, 86, 78, 17)]
data = pd.DataFrame(rows, columns=names)
data['combined'] = None
source = ColumnDataSource(data)
callback = CustomJS(args=dict(source=source), code="""
    const labels = cb_obj.labels;
    const active = cb_obj.active;
    const data = source.data;
    const sourceLen = data.combined.length;
    const combined = Array(sourceLen).fill(undefined);
    if (active.length > 0) {
        const selectedColumns = labels.filter((val, ind) => active.includes(ind));
        for(let i = 0; i < sourceLen; i++) {
            let sum = 0;
            for(col of selectedColumns){
                sum += data[col][i];
            }
            combined[i] = sum;
        }
    }
    data.combined=combined;
    source.change.emit();
""")
checkbox_group = CheckboxGroup(labels=names[1:], active=[], callback=callback)
p = figure(width=400, height=400)
p.line(x='Wave', y='combined', source=source)
show(layout([[widgetbox(checkbox_group)], [p]]))

推荐阅读