我显示2D数据如在背景虚化服务器变化的形状的图像,并且因此需要动态更新不仅在图像的数据源,而且其dw
,dh
,x
,和y
特性。在下面的虚拟示例中,这些更改是在连接到Button
小部件的回调函数中进行的。
我发现需要访问glyph
图像GlyphRenderer
对象的属性,并且可以通过其update()
方法(请参见代码)进行访问。但是,直到我单击工具栏的“重置”按钮,更改才会生效。我注意到,更改在第二次激活该callback()
功能时也会神秘地生效。进行这些更改的正确方法是什么?
import bokeh.plotting
import bokeh.models
import bokeh.layouts
import numpy as np
# set up the interface
fig1 = bokeh.plotting.figure(x_range=(0, 10), y_range=(0, 10))
im1 = fig1.image([], dw=5, dh=5)
button = bokeh.models.Button(label='scramble')
# add everything to the document
bokeh.plotting.curdoc().add_root(bokeh.layouts.column(button, fig1))
# define a callback and connect it
def callback():
# this always works:
im1.data_source.data = {'image': [np.random.random((100,100))]}
# these changes only take effect after pressing the "Reset"
# button, or after triggering this callback function twice:
im1.glyph.update(x=1, y=1, dw=9, dh=9)
button.on_click(callback)
我不会立即明白为什么您的代码无法正常工作。我可以建议显式地使用ColumnDataSource并将所有Image字形属性链接到该源中的列。然后,您应该能够在一行中更新source.data并应用所有更新。
以下是一些不完整的示例代码,以建议如何执行此操作:
from bokeh.models import Image, ColumnDataSource
from bokeh.plotting import figure
# the plotting code
plot = figure()
source = ColumnDataSource(data=dict(image=[], x=[], y=[], dw=[], dh=[]))
image = Image(data='image', x='x', y='y', dw='dw', dh=dh)
plot.add_glyph(source, glyph=image)
# the callback
def callback():
source.data = {'image': [np.random.random((100,100))], 'x':[1], 'y':[1], 'dw':[9], 'dh':[9]}
button.on_click(callback)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句