Bokeh交互式图例隐藏多个字形

时间:2017-07-28 19:09:58

标签: python pandas bokeh

我为数据集启用了隐藏图例选项。当我点击它时,只有一个酒吧熄灭而其他酒吧停留。我不太确定导致这个问题的原因。

以下是之前和之后的条形图:

before and after

以下是我的数据:

Here's

以下是代码:

p = Bar(output,'Programs',values="Averages", group="University",plot_width=600,plot_height=400, title="Comparison")
p.legend.click_policy="hide"
output_file("bar.html")
show(p)

1 个答案:

答案 0 :(得分:1)

目前不是(Bokeh 0.12.6)可以通过legend.click_policy="hide"隐藏所有条形码,如documentation中所述:

  

交互式图例功能目前适用于“每个字形”图例。通过指定要自动分组的列创建的图例不能使用下面描述的功能

然而,可以通过添加CheckboxGroup()和CustomJS来隐藏条形图,当单击复选框时,它会隐藏条形图。您可以在下方看到MCVE,也是available online as a Jupyter Notebook

import numpy as np
from bkcharts import Bar, show
from bokeh.layouts import column
from bokeh.models import CheckboxGroup, CustomJS

data = {'University': ['ENGT'] * 3 + ['UBC'] * 3,
        'Averages': [76.5, 79.9, 72.2, 71, 72, 69],
        'Programs': ['CHML', 'CIVL', 'CPEN', 'CHML', 'CIVL', 'CPEN']}

group = "University"
bars = Bar(data=data, label='Programs', values="Averages", group=group,
           plot_width=600, plot_height=400, title="Comparison")

checkboxes = CheckboxGroup(labels=np.unique(data[group]).tolist(),
                           # Make all checkboxes checked by default
                           active=list(range(np.unique(data[group]).size)))
checkboxes.callback = CustomJS(args=dict(bars=bars), code="""

var group = '%s';

function change_bars_visibility(checkbox_name, visible) {
    for (j = 0; j < bars.renderers.length; j++) {
        // Go through rendered objects

        if (bars.renderers[j].attributes.hasOwnProperty('data_source') && 
            bars.renderers[j].data_source.data[group][0] === checkbox_name) {

            // Change the visibility of this rendered object if it belongs to
            // the group determined by the checkbox that was clicked
            bars.renderers[j].visible = visible;
        }
    }
}

for (i = 0; i < cb_obj.labels.length; i++) {
    // Go through checkbox labels
    var checkbox_name = cb_obj.labels[i];

    if (cb_obj.active.indexOf(i) >= 0) {
        // alert(checkbox_name + " is activated");
        change_bars_visibility(checkbox_name, true);
    }
    else {
        // alert(checkbox_name + " is disabled");
        change_bars_visibility(checkbox_name, false);
    }
}
""" % group)

show(column(bars, checkboxes))