Dear Bokeh community,
From a drop down selection I would like to have a plot updated for what has been selected.
This means a dataFrame has to be filtered first.
I am quite new to bokeh and callbacks.
For now I have:
“”"
import bokeh
from bokeh.plotting import figure, output_notebook, show, ColumnDataSource, output_file
from bokeh.models import HoverTool, BoxSelectTool, Range1d, CustomJS, TapTool
from bokeh.models.widgets import Select
from bokeh.io import hplot
print(“Bokeh version: %s” %bokeh.version)
print(“Panda version: %s” %pd.version)
output_notebook()
output_file(“NozzleOut.html”)
_tools_to_show = ‘box_zoom,pan,save,hover,resize,reset,tap,wheel_zoom,tap’
def callbackMedium(uniqueMediaLst, df_m):
#data
medium = cb_obj.get(‘value’)
df_noNan, beginDot, endDot, nfdCount, colsNozzleLogCroppedLst = NozzleOutMedium1(medium, uniqueMediaLst, df_m)
df_tot = NozzleOutMedium2(df_noNan, beginDot, endDot)
#plot
p = figure(plot_width=600,
plot_height=600,
tools=_tools_to_show,
title=“NozzleOuts. NFD-Count: %s. Medium: %s” %(nfdCount, medium) ,
y_range=Range1d(start=0.0, end=nfdCount+100))
p.xaxis.axis_label = ‘zPos[dotNr]’
p.xaxis.major_label_orientation = 90
p.yaxis.axis_label = ‘Drops[#]’
i = 0
for (name, series) in df_tot.iteritems():
need to repmat the name to be same dimension as index
name_for_display = np.tile(name, [len(df_tot.index),1])
source = ColumnDataSource({‘x’: df_tot.index, ‘y’: series.values, ‘series_name’: name_for_display})
trouble formating x as datestring, so pre-formating and using an extra column. It’s not pretty but it works.
p.scatter(‘x’, ‘y’, fill_alpha=0.6, size=5, source = source, color=colorRange[i])
hover = p.select(dict(type=HoverTool))
hover.tooltips = [(“ColHead”, “@series_name”), (“Nozzle”, “@x”), (“Drops”, “@y”)]
hover.mode = ‘mouse’
i +=1
return p
select = Select(title=“Select Medium:”, value=“All”, options=uniqueMediaLst, callback=CustomJS.from_py_func(callbackMedium))
show(hplot(p, select))
“”"
Explanation:
A medium is selected from uniqueMediaLst. Functions NozzleOutMedium1 and NozzleOutMedium2, filters df_m from the selected medium and creates df_tot (index of 388 to 2170, with 12 columns. values are ints. In the for loop each column is plotted and proper hovering is added.
Question:
I am aware that the callback function is not defined in the way it should be. What is the proper way to write the callback function so that the dataFrame is filtered and the plot p is updated with each selection?