首页 > 解决方案 > 散景图像的动态更新

问题描述

我正在使用 jupyter/notebooks 或 jupyter/lab。

我想以动态方式更改散景显示的图像内容 - 在按钮单击、组合框更改或背景线程更改时。尽管更改 1D 图形的内容很容易,如示例笔记本中所示,但我仍坚持更改散景生成的 2D 图像的内容。我将不胜感激任何帮助使其工作。

这是最简单的代码。

from bokeh.io import output_notebook, show
from bokeh.plotting import figure
output_notebook()

from bokeh.layouts import column, row
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource, Select, TextInput

def simple_app(doc):
    
    p = figure(tools="", toolbar_location=None)
    
    p = figure(tooltips=[("x", "$x"), ("y", "$y"), ("value", "@image")])
    p.x_range.range_padding = p.y_range.range_padding = 0
    
    r = p.image(image=[np.zeros((200,200))], x=0, y=0, dw=10, dh=10, palette="Spectral11", level="image")
    
    select = Select(title="Select data", value="file2", options=['file1','file2'])
    
    def update_data(attrname, old, new):
        tdata = {"file1": np.random.rand(200,200)*3,
                 "file2": np.random.rand(200,200)*5.,
                }
        # reading files        
        cd = ColumnDataSource(data={'image': tdata[select.value]})
        print(f"Selected {select.value}")

        r.data_source = cd # here it potentially could be changed, but does not seem to work
    
    select.on_change('value', update_data)

    layout = column(row(select, width=200), row(p))

    doc.add_root(layout)

show(simple_app)

标签: pythonjupyter-notebookbokehjupyter-lab

解决方案


推荐阅读