如何多次更新散景图?

时间:2018-09-28 13:03:31

标签: python bokeh

我想迭代地将线添加到绘图中并更改以前绘制的线的颜色。例如,仅突出显示最新的。但是,以下内容无法更新除第一行以外的所有颜色:

from bokeh.plotting import *
from bokeh.io import push_notebook

fig=figure()
h=show(fig, notebook_handle=True)

ps=[fig.line([1,2],[i,i], color="black") for i in range(3)]

for p in ps:
    p.glyph.line_color="red"
    push_notebook(h)

仅当我执行一次push_notebook()时它才有效,但是在计算运行时,我想使用多个更新。

我该如何做?

编辑: 实际用法可能更接近此:

from bokeh.plotting import *
from bokeh.io import push_notebook

output_notebook()

fig=figure()
h=show(fig, notebook_handle=True)  # start plot and "slow" computation to watch

ps=[]

for i in range(3):
    p=fig.line([1,2],[i,i], line_width=2, color="red")  # new line in red
    if ps:
        ps[-1].glyph.line_color="black"   # old lines in black -> this is not happening
    ps.append(p)
    push_notebook(h)

1 个答案:

答案 0 :(得分:0)

添加完所有内容后,您必须致电show

fig=figure()

ps=[fig.line([1,2],[i,i], line_width=2, color="black") for i in range(3)]

# call show after lines are already added to figure
h=show(fig, notebook_handle=True)

for p in ps:
    p.glyph.line_color="red"
    push_notebook(h)

我不确定这可能是一个错误。但是,无论如何,这是当前要应对的行为:

enter image description here

如果您想要不受push_notebook限制的东西,可以考虑使用embedding a real Bokeh app in the notebook