Hi,
I’m trying to connect a drop down menu using Select with a hexbin plot, however whatever I try I can not update my source.data['x']
and plot doesn’t update when I change item in the drop down menu. I tried using customJS
and on_change
options on bokeh server and can not make it work. (I made scatterplots and it’s all fine, the density plots are those that I want and have issue with). Can anyone help me out with this? At this point I’m wondering is it possible what I aim to do?
Here is minimum executable example below (version for bokeh server, bokeh version 2.1.1
):
import numpy as np
from bokeh.layouts import column, row, gridplot
from bokeh.io import output_file, show
from bokeh.plotting import figure
from bokeh.transform import log_cmap
from bokeh.util.hex import hexbin
from bokeh.io import curdoc
from bokeh.models import (ColorBar, LogTicker, HoverTool,
ColumnDataSource, CustomJS, Select)
example_data = {
'x': np.random.randint(30, size=50),
'y': np.random.randint(20, size=50),
'z': np.random.randint(10, size=50)
}
#Initiate ColumnDataSources
source = ColumnDataSource(data = dict(x = [], y = []) )
def update_x(attr, old, new):
key_property_x.value
key_property_x = Select(title="Option:", value=f"{list(example_data)[0]}",
options=list(example_data))
key_property_x.on_change("value", update_x)
#Update Data Source
def update_plot():
p.xaxis.axis_label = key_property_x.value
p.yaxis.axis_label = key_property_x.value
source.data = dict(x = example_data[key_property_x.value],
y = example_data[key_property_x.value])
hex_size = 1
bins = hexbin(source.data['x'], source.data['y'], hex_size)
return bins, hex_size
#Defining Figure Tools
defaults = 'pan,box_zoom,box_select,reset,wheel_zoom, undo,redo,save'
TOOLS = [defaults]#,hover_userdefined]
p = figure(tools=TOOLS, match_aspect=True, background_fill_color='white',
toolbar_location="above")
p.grid.visible = False
bins, hex_size = update_plot()
cmap = log_cmap('counts', 'Cividis256', 1, max(bins.counts))
h = p.hex_tile(q="q", r="r", size=hex_size, line_color=None, source=bins,
fill_color=cmap)
hover = HoverTool(
tooltips=[('Counts', '@counts')],
mode='mouse',
point_policy='follow_mouse',
renderers=[h],
)
p.add_tools(hover)
color_bar = ColorBar(
color_mapper=cmap['transform'],
location=(0, 0),
ticker=LogTicker(),
)
p.add_layout(color_bar, 'right')
col = column(key_property_x)
layout = row(col, p)
curdoc().add_root(layout)