尝试使用Bokeh Widget复选框组时有几行错误及其属性"可见"

时间:2017-02-01 15:04:01

标签: python python-2.7 checkbox bokeh

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

PS C:\Users\407334\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

1 个答案:

答案 0 :(得分:1)

checkbox.active是从0到n-1的整数列表,所以当你搜索到line0的匹配时,line1 ...将它转换为整数,即:

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)

enter image description here