散景不更新CheckboxGroup的情节线更新 [英] Bokeh not updating plot line update from CheckboxGroup

查看:39
本文介绍了散景不更新CheckboxGroup的情节线更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这篇出色的教程和Bokeh一起玩.

I'm following this great tutorial to play a little bit with Bokeh.

基本上,我有一个 figure ,其中添加了两个独立的 line .一切都正确渲染,但是当我要更新时,即使我检查新的 ColumnDataSource 是否已使用新值很好地更新,也没有任何反应.

Basically, I have a figure with two independent line added to it. Everything is rendered properly but when I want to update nothing happens even if I checked that the new ColumnDataSource is well updated with the new values.

我使用以下命令进行渲染: bokeh serve --show my_app

I render it using the command : bokeh serve --show my_app

这是我创建的方式:

src_p6 = make_dataset(["select_a", "select_b"])
p6 = make_plot(src_p6)
select_selection = CheckboxGroup(labels=["select_a", "select_b"], active = [0, 1])
select_selection.on_change('active', update)
controls = WidgetBox(select_selection)
curdoc().add_root(column(controls, p6, width=1200))


def make_dataset(select_list):
  if 'select_a' in select_list and 'select_b' in select_list:
    tmp = pd.DataFrame({'time': df["time"], 
                       'a': df["a"], 
                       'b': df["b"]
                       })
  elif 'select_a' in select_list and 'select_b' not in select_list:
    tmp = pd.DataFrame({'time': df["time"], 
                       'a': df["a"]
                       })
  elif 'select_a' not in select_list and 'select_b' in select_list:
    tmp = pd.DataFrame({'time': df["time"], 
                       'b': df["b"]
                       })
  else:
    tmp = pd.DataFrame({'time': df["time"]
                       })

  src = ColumnDataSource(tmp)

  return src


def make_plot(plot_src):
  p = figure(plot_width=1000, plot_height=600, 
           title="Line x2 with hover and update",
           x_axis_label='Time', 
           y_axis_label='Values'
          )

  hover_content = [("Time", "@time")]

  if 'a' in plot_src.data:
    p.line(x='time', y='a', source=plot_src, legend="A", line_color="blue")
    hover_content.append(("A", "@a"))
  if 'b' in plot_src.data:
    p.line(x='time', y='b', source=plot_src, legend="B", line_color="red")
    hover_content.append(("B", "@b"))

  p.add_tools(HoverTool(tooltips=hover_content))

  return p


def update(attr, old, new):
  print(src_p6.data)

  select_to_plot = [select_selection.labels[i] for i in select_selection.active]

  new_src = make_dataset(select_to_plot)

  src_p6.data = new_src.data

  print("**********************")
  print(src_p6.data) # I see here that the data are well updated compared to the first print


我的传入数据是JSON,看起来像这样:


My incoming data is JSON and looks like this :

# {"data":[{"time":0,"a":123,"b":123},{"time":1,"a":456,"b":456},{"time":2,"a":789,"b":789}]}
# data = json.load(data_file, encoding='utf-8')
# df = pd.io.json.json_normalize(data['data'])

感谢您的见识

推荐答案

首先感谢 @bigreddot 时间和指导.

我最大的问题之一是,我实际上并不想更新值,而只是显示/隐藏它,因此仅从源中删除它是行不通的.

One of my biggest problem was that I didn't actually wanted to update the values but rather just show/hide it, so just removing it from the source was not working.

在我的 make_plot 函数中使用 if 语句添加 line 也不起作用,因为这只是在第一次绘制图时才调用创建.对于更新,它会更新图中的值,但不会从头开始重建所有内容... 因此,如果仅从一行开始,即使有可能,我也不知道它将如何创建新行...

Adding line in my make_plot function with a if statement doesn't work also because it is only called the first time the plot is created. For updates, it update the value on the figure but do not reconstruct everything from scratch... So if you start with only one line, I don't know how it will create a new line, if it's even possible...

我开始简化我的 make_dataset 函数,使其仅返回一个简单的Python字典:

I started to simplify my make_dataset function to only return a simple Python dict :

tmp = dict(time=df["time"], a=df["a"], b=df["b"])

但是当我想删除一行时,即使有更好的解决方案,我也使用了一个空数组(我只是在这里与Bokeh一起玩):

But when I wanted to remove a line, I used an empty array even if there is better solutions (I was just playing with Bokeh here) : Line ON/OFF, Interactive legend

empty = np.empty(len(df["time"])); empty.fill(None)
tmp = dict(time=df["time"], a=df["a"], b=empty)


当我第一次创建我的情节时,我会做:


When I first create my plot I then do :

src_p6 = ColumnDataSource(data=make_dataset(["select_a", "select_b"]))
p6 = make_plot(src_p6)


并且更新功能使用基本的python字典更新 ColumnDataSource .data :

new_src = make_dataset(select_to_plot)
src_p6.data = new_src

这篇关于散景不更新CheckboxGroup的情节线更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆