How does one update the color mapper in Bokeh between categorical, linear and logarithmic? The code below with circle.glyph.fill_color['transform'] = mapper
does not work when executed with bokeh serve
. I reviewed this question, but I still can't figure it out. Thanks.
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models import CategoricalColorMapper
from bokeh.models import LinearColorMapper
from bokeh.models import LogColorMapper
from bokeh.palettes import Set3
from bokeh.models import Select
from bokeh.io import curdoc
from bokeh.layouts import layout
x = [1, 2, 3, 4, 5, 7, 8, 9, 10, 20, 50]
y = x
z = list(map(str, x))
select_mapper = Select(
title="ColorMapper",
value="Categorical",
options=['Categorical', 'Linear', 'Log'],
)
mapper = CategoricalColorMapper(palette=Set3[12], factors=z)
source = ColumnDataSource(dict(x=x, y=y, z=z))
p = figure(plot_width=300, plot_height=300)
circle = p.circle(
x='x', y='y',
fill_color={'field': 'z', 'transform': mapper},
source=source,
size=50,
)
def update(attrname, old, new):
# Get the current slider values
if select_mapper.value == 'Linear':
mapper = LogColorMapper(palette="Viridis256")
elif select_mapper.value == 'Log':
mapper = LinearColorMapper(palette="Viridis256")
else:
mapper = CategoricalColorMapper(palette=Set3[12], factors=z)
circle.glyph.fill_color['transform'] = mapper
source.data = dict(x=x, y=y, z=z)
select_mapper.on_change('value', update)
curdoc().add_root(layout([select_mapper, p]))
As of how to change the mapper - try changing the whole property spec instead of just transform:
circle.glyph.fill_color = dict(field='z', transform=mapper)