Colorbar: change color_mapper type with widget

Hi,

I’m trying to update a colorbar object, with a Select widget and I can update certain attributes with the below code.

from bokeh.plotting import figure
from bokeh.io import output_notebook, output_file, curdoc
from bokeh.palettes import RdYlGn, PuRd
from bokeh.models import Select
from bokeh.layouts import layout, widgetbox, column
from bokeh.io import curdoc
from bokeh.models import LinearColorMapper, ColorBar, FixedTicker, FuncTickFormatter, LogColorMapper

p = figure()
p.circle(x=1, y=1)

ticker = FixedTicker(ticks=[0, 50, 100, 150, 200])
mapper = LinearColorMapper(palette=RdYlGn[4], low=0, high=200)
colorbar = ColorBar(color_mapper=mapper, ticker=ticker,
height=110, width=15, location=(20,410))
p.add_layout(colorbar)

def change(attr, old, new):
if select_data.value == ‘two’:
mapper.palette=PuRd[4]
ticker.ticks=[0, 100, 200]
colorbar.height=200
else:
mapper.palette=RdYlGn[4]
ticker.ticks=[0, 100, 200]
colorbar.height=110

select_data = Select(options=[‘one’, ‘two’])
select_data.on_change(‘value’,change)

w = widgetbox(select_data)
c = column(p, w)
curdoc().add_root©

``

but after I change the callback function to update the mapper type as well (from Linear to Log)

def change(attr, old, new):
if select_data.value == ‘two’:
mapper.palette=PuRd[6]
ticker.ticks=[0, 50, 100, 150, 200]
colorbar.height=200
colorbar.color_mapper = LogColorMapper(palette=PuRd[6], low=0, high=200)
else:
mapper.palette=RdYlGn[6]
ticker.ticks=[0, 100, 200]
colorbar.height=200
colorbar.color_mapper = LinearColorMapper(palette=RdYlGn[6], low=0, high=200)

``

it stops working, and the colorbar does not update at all.

I’ve also experimented with using the .trigger method but did not work.

Could you please advice why it is not working as expected?

thank you in advance!

Laszlo

1 Like