在 Jupyter notebook 中更新散景补丁

Updating bokeh patches in Jupiter notebook

我很好奇在散景中更新 Patches 字形的正确方法。 我的最小示例是:

p_blur = figure(x_range=(0, 300), y_range=(0, 300))
source = ColumnDataSource({'xs':[[100,200,300], [10,50,500,400]], 'ys':[[30,150,70], [10,500,50,50]]})
polygons = Patches(xs="xs", ys="ys",fill_color="#fb9a99")
glyph = p_blur.add_glyph(source, polygons)
nb = show(p_blur, notebook_handle=True)

如果我现在想更新字形,例如通过

source1 = ColumnDataSource({'xs':[[10,20,30], [10,50,50,40]], 'ys':[[30,15,70], [10,50,50,50]]})
glyph.data_source = source1
push_notebook( nb )

我没有看到任何变化。但是,如果我这样做:

p_blur.renderers.remove(glyph)
glyph = p_blur.add_glyph(source1, polygons)
push_notebook( nb ) 

变化反映出来了。似乎第二种方式太hacky了。有没有更正确的方法来做到这一点?

谢谢!

您可以将新数据分配给 source.data,试试这个:

source.data = {'xs':[[10,20,30], [10,50,50,40]], 'ys':[[30,15,70], [10,50,50,50]]}
push_notebook(nb)