如何更改bokeh中现有图像的范围和位置? [英] How to change the extent and position of an existing image in bokeh?

查看:55
本文介绍了如何更改bokeh中现有图像的范围和位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在bokeh服务器中将2d数据显示为各种形状的图像,因此不仅需要动态更新图像的数据源,还需要动态更新其dwdhxy特性.在下面的虚拟示例中,这些更改是在与Button小部件连接的回调函数中进行的.

I am displaying 2d data as images of varying shapes in a bokeh server, and therefore need to dynamically update not only the image's data source, but also its dw, dh, x, and y properties. In the dummy example below, these changes are made in a callback function which is connected to a Button widget.

我发现我需要访问图像的GlyphRenderer对象的glyph属性,并且可以通过其update()方法(请参见代码)进行访问.但是,直到我单击工具栏的重置"按钮,更改才会生效.我注意到,第二次激活callback()函数时,这些更改也会神秘地生效.进行这些更改的正确方法是什么?

I've figured out that I need to access the glyph attribute of the image's GlyphRenderer object, and I can do so through its update() method (see code). But the changes don't take effect until I click the toolbar's Reset button. I've noticed that the changes also mysteriously take effect the second time I activate the callback() function. What is the proper way to make these changes?

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,并应用所有更新.

I don't immediately see why you code isn't work. I can suggest explicitly using a ColumnDataSource and linking all of the Image glyph properties to columns in that source. Then you should be able to update the source.data in a single line and have all of the updates apply.

这里有一些不完整的示例代码来建议如何做到这一点:

Here's some incomplete sample code to suggest how to do that:

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)

这篇关于如何更改bokeh中现有图像的范围和位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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