python - 尝试使用具有多行及其属性的 Bokeh Widget Checkbox 组时出现值错误 "visible"

标签 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

最佳答案

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

可以填充列表,而不是创建字典,这将保留顺序,因此无需将字典的键与事件复选框进行匹配。我创建了一些 Pandas 数据来玩。下面的代码实现了后面的想法,至少在使用 python2.7 的 bokeh 版本 0.12.4 中是有效的:

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

关于python - 尝试使用具有多行及其属性的 Bokeh Widget Checkbox 组时出现值错误 "visible",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41982984/

相关文章:

python - Tensorflow 表查找 int->float

python - 'raw' 属性在 python 请求响应中意味着什么?

python - 使用制表符缩进(不是空格)转储 JSON

javascript - 仅选择一组中的一个复选框

~/.zshrc 中的 python 别名覆盖虚拟环境源

python - 如何在 Pandas 数据框中按日期汇总所有金额?

python - 我可以从 Python 2.7 中基于 raw_input 的模块导入吗?

python - 将python脚本转换为exe并作为Windows服务运行

javascript - Angular 形式复选框最初未包含在模型中

javascript - 如何使用控制台选择N个复选框?