尝试将 Bokeh Widget Checkbox 组与多行及其属性一起使用时出现值错误 "visible"

Value Error when trying to use Bokeh Widget Checkbox group with several lines and their attibute "visible"

在包含 35 条线的散景图中工作,当尝试使用复选框小部件使它们可见和不可见时,在单击复选框的一个元素时会出现此错误,并且没有线消失。日志是:

PS C:\Users7334\pad-s100> bokeh serve plot4.py
2017-02-01 14:42:36,759 Starting Bokeh server version 0.12.4
2017-02-01 15:09:13,523 Starting Bokeh server on port 5006 with applications at paths ['/plot4']
2017-02-01 15:09:13,525 Starting Bokeh server with process id: 11116
2017-02-01 15:10:05,821 200 GET /plot4 (::1) 6733.00ms
2017-02-01 15:10:06,246 WebSocket connection opened
2017-02-01 15:10:06,247 ServerConnection created
2017-02-01 15:10:30,026 error handling message Message 'PATCH-DOC' (revision 1): ValueError('too many values to unpack',
)

使用的python脚本受this example影响:

from bokeh.layouts import row
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.palettes import inferno
from bokeh.models.widgets import CheckboxGroup
import pandas as pd

p = figure(title="Motor-Block Monitorization", width=900, plot_height=900, x_axis_type='datetime')

numlines = len(df.columns)
mypalette = inferno(numlines)
line_set = dict()

for (x, column_names, colores) in zip(range(0, numlines), df.columns.values, mypalette):
    if column_names != 'Date' and column_names != 'Time':
        line_set["line{0}".format(x)] = p.line(df.index, df[column_names], color=colores)

act = range(0, numlines)
checkbox = CheckboxGroup(labels=list(df.columns.values),
                     active=act)


def update(attr, old, new):
    for i, element in line_set:
        element.visible = i in checkbox.active

checkbox.on_change('active', update)
main_column = row(p, checkbox)
curdoc().add_root(main_column)

已经测试了绘制它们的不同方法,但错误仍然存​​在。

这是有效的情节:Bokeh Plot

checkbox.active 是从 0 到 n-1 的整数列表,因此当您搜索与第 0 行、第 1 行的匹配时...将其转换为整数,即:

def update(attr, old, new):
    for i, element in line_set.iteritems():
        element.visible = int(i[4:]) in checkbox.active

代替创建字典,可以填充一个列表,这将保留顺序,因此无需将字典的键与活动复选框匹配。我创建了一些熊猫数据来玩。下面的代码实现了后来的想法,至少在散景版本 0.12.4 中使用 python2.7:

import bokeh
import bokeh.plotting

# Begin Creating some panda data
# ------------------------------
import datetime
import pandas as pd
todays_date = datetime.datetime.now().date()
cols = 20;rows = 10.
index = pd.date_range(todays_date, periods=rows, freq='D')
columns = ['col%d'%x for x in range(cols)]
data = [pd.np.arange(cols)/10.+x for x in pd.np.arange(rows)/rows]
df = pd.DataFrame(data, index=index, columns=columns)
# ----------------------------
# End Creating some panda data

p = bokeh.plotting.figure(title="Motor-Block Monitorization", 
                          width=500, plot_height=500, x_axis_type='datetime')

numlines = len(df.columns)
mypalette = bokeh.palettes.inferno(numlines)
line_list = []

for (column_names, colores) in zip(df.columns.values, mypalette):
    if column_names != 'Date' and column_names != 'Time':
        line_list += [p.line(df.index, df[column_names], color=colores)]

act = range(0, numlines)
checkbox = bokeh.models.CheckboxGroup(labels=list(df.columns.values),
                     active=act)

def update(attr, old, new):
    for i, element in enumerate(line_list):
        element.visible = i in checkbox.active

checkbox.on_change('active', update)
main_column = bokeh.layouts.row(p, checkbox)
bokeh.io.curdoc().add_root(main_column)