如何通过 select 小部件更改图像绘图?
How can I change an image-plot by select widget?
我想使用 select-widget/slider-widget 来更改图像绘图。有没有办法做到这一点?如果 "no" 我有什么选择来实现这个问题?
这是我所做的一个简短示例:
#Let's make some data:
image_a = np.random.randint(0,10,10000).reshape(100,100)
image_b = np.random.randint(0,10,10000).reshape(100,100)
image_c = np.random.randint(0,10,10000).reshape(100,100)
# define a callback:
def callback(attr, old, new):
plot.image = [select_widget.value]
#create a figure:
plot = figure()
# And plot the image "image_a"
plot.image(image=[image_a])
# create a select-widget with options:
select_widget = Select(title="Title", \
value="image_a", \
options=["image_a","image_b","image_c"])
# If the widget will be changed call a callback-function to modify
# the image by selection:
select_widget.on_change('value', callback)
layout = row(select_widget, plot)
curdoc().add_root(layout)
不幸的是,图像不会按预期更新。 Lateron 我想使用滑块加载不同的图像。但我不确定,如果我能做到。
也许还有另一种绘制 numpy 数组的方法?
问候
你 运行 这是一个 Bokeh 服务器应用程序吗,即 bokeh serve
?真正的 python 回调仅在 Bokeh 服务器应用程序的上下文中起作用(Bokeh 服务器正是运行 Python 回调代码的 Python 进程)。否则,如果这是独立内容(即 output_file
和 show
),则只有 JavaScript CustomJS
回调是可能的。
用户指南章节中有许多使用 CustomJS
回调更新绘图的示例:JavaScript Callbacks
在用户指南的 examples/app
folder of the GitHub repo. You should also consider studying the Running a Bokeh Server 章节中,有许多在 Bokeh 服务器应用程序中使用的真实 Python 回调示例。它的编写正是为了提供这种背景。
无论如何,您上面的代码在任何一种情况下都不起作用。 select 小部件的 value 是一个 string,即 "image_a",因此您的回调相当于:
plot.image = ["image_a"]
这显然是荒谬的。您需要列表的内容是一个实际的数组。假设这是一个 Bokeh 服务器应用程序(以便真正的 Python 回调可以工作),您可以使用字典将字符串映射到实际数组:
data = {
"image_a": image_a,
"image_b": image_b,
"image_c": image_c,
}
然后:
plot.image = [data[select_widget.value]]
我想使用 select-widget/slider-widget 来更改图像绘图。有没有办法做到这一点?如果 "no" 我有什么选择来实现这个问题?
这是我所做的一个简短示例:
#Let's make some data:
image_a = np.random.randint(0,10,10000).reshape(100,100)
image_b = np.random.randint(0,10,10000).reshape(100,100)
image_c = np.random.randint(0,10,10000).reshape(100,100)
# define a callback:
def callback(attr, old, new):
plot.image = [select_widget.value]
#create a figure:
plot = figure()
# And plot the image "image_a"
plot.image(image=[image_a])
# create a select-widget with options:
select_widget = Select(title="Title", \
value="image_a", \
options=["image_a","image_b","image_c"])
# If the widget will be changed call a callback-function to modify
# the image by selection:
select_widget.on_change('value', callback)
layout = row(select_widget, plot)
curdoc().add_root(layout)
不幸的是,图像不会按预期更新。 Lateron 我想使用滑块加载不同的图像。但我不确定,如果我能做到。
也许还有另一种绘制 numpy 数组的方法?
问候
你 运行 这是一个 Bokeh 服务器应用程序吗,即 bokeh serve
?真正的 python 回调仅在 Bokeh 服务器应用程序的上下文中起作用(Bokeh 服务器正是运行 Python 回调代码的 Python 进程)。否则,如果这是独立内容(即 output_file
和 show
),则只有 JavaScript CustomJS
回调是可能的。
用户指南章节中有许多使用 CustomJS
回调更新绘图的示例:JavaScript Callbacks
在用户指南的 examples/app
folder of the GitHub repo. You should also consider studying the Running a Bokeh Server 章节中,有许多在 Bokeh 服务器应用程序中使用的真实 Python 回调示例。它的编写正是为了提供这种背景。
无论如何,您上面的代码在任何一种情况下都不起作用。 select 小部件的 value 是一个 string,即 "image_a",因此您的回调相当于:
plot.image = ["image_a"]
这显然是荒谬的。您需要列表的内容是一个实际的数组。假设这是一个 Bokeh 服务器应用程序(以便真正的 Python 回调可以工作),您可以使用字典将字符串映射到实际数组:
data = {
"image_a": image_a,
"image_b": image_b,
"image_c": image_c,
}
然后:
plot.image = [data[select_widget.value]]