如何更改散景中现有图像的范围和位置?

How to change the extent and position of an existing image in bokeh?

我在散景服务器中将二维数据显示为不同形状的图像,因此不仅需要动态更新图像的数据源,还需要动态更新其 dwdhxy 属性。在下面的虚拟示例中,这些更改是在连接到 Button 小部件的回调函数中进行的。

我发现我需要访问图像 GlyphRenderer 对象的 glyph 属性,我可以通过它的 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 并将所有图像字形属性链接到该源中的列。然后您应该能够在一行中更新 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)