如何通过回调函数更新图像图的color_mapper / colorbar?

时间:2019-06-18 10:53:21

标签: bokeh python-3.7

我正在图像图上显示不同的2D阵列。我另外添加了color_mapper和colorbar。用户可以通过从选择小部件中选择来更改图像。效果很好,但是color_mapper和color_bar并未调整为新的图像/阵列。

让我为您展示一些代码来说明我的问题:


   def callback(attr, old, new):

       dict_arrays = dict("array_a" = array_a,
                          "array_b" = array_b)

       array = dict_arrays[select_widget.value]

       # I think at this point i have to adjust the colorbar and
       # color_mapper otherwise the colormap of the old plot will
       # be applied:
       .
       .
       .

       # define new dataset:
       source.data = dict(image=[array])


   # some data:
   dim = 100
   array_a = np.random.randtint(0,75,10000).reshape(dim,dim)
   array_b = np.random.randtint(25,100,10000).reshape(dim,dim)


   # find out the max and min of that array:
   vmax = np.nanmax(array_a)
   vmin = np.nanmin(array_a)

   #This is my source:
   source = ColumnDataSource(data={'image' : [array_a]})

   # create the color_mapper:
   color_mapper = LinearColorMapper(palette=cc.coolwarm,
                                        low=vmin,
                                       high=vmax)

   # create the figure
   plot = figure()

   # create the plot:
   plot.image(image='image', x=0, y=0, dw=100, dh=100,
                             color_mapper=color_mapper,
                             source=source)

   color_bar = ColorBar(color_mapper=color_mapper,
                             ticker=BasicTicker(),
                             location=(0,0),
                             line_color=None,
                             label_standoff=12)

   plot.add_layout(color_bar, 'right')



   # create a select_widget:
   select_widget = Select(title="title", value = "array_a",
                                       options = ["array_a", "array_b"])
   # on_change callback
   select_widget.on_change('value', callback)

   # layout:
   layout = layout([select_widget, plot])
   curdoc().add_root(layout)

0 个答案:

没有答案