I am trying to build a plot with Bokeh to display which job is more exposed to a virus like this.
I tried to update the label when my selected value changed. However, it delayed the change. For example, when I first select "nurse" in select, it did not show the label, then when I selected "doctor", it showed "nurse" on the plot. Please help me fix it. Thank you so much.
# Start plotting
source = ColumnDataSource(full_table)
p = figure(title="各職業之新型冠狀病毒之風險圖", x_axis_label='暴露疾病頻率', y_axis_label='與人接近距離')
p.circle('Expose_frequency',
'Physical_proximity', name = 'allcircle',
size=10,fill_alpha=0.2, source=source, fill_color='gray', hover_fill_color='firebrick', hover_line_color="firebrick", line_color=None)
hover = HoverTool(tooltips=[('職業','@TW_Occupation'),('Occupation','@Occupation'),('暴露於疾病指數','@Expose_frequency'),('與人接近距離指數','@Physical_proximity')])
p.add_tools(hover)
# Define a callback function
def update_plot(attr, old, new):
old_choice=full_table[full_table['TW_Occupation']==old]
p.circle(old_choice['Expose_frequency'],old_choice['Physical_proximity'],size=10,fill_alpha=1,fill_color='Gray', line_color=None )
choice=full_table[full_table['TW_Occupation']==new]
a=choice['Expose_frequency']
b=choice['Physical_proximity']
p.circle(a,b,size=10,fill_alpha=1,fill_color='blue' )
citation=Label(x=choice.Expose_frequency.item()+0.5,y=choice.Physical_proximity.item()+0.5,
text=choice.TW_Occupation.item(),
border_line_color=None, border_line_alpha=1.0,
background_fill_color=None, background_fill_alpha=0,
text_font_size="8pt", text_align="center")
p.add_layout(citation)
# Add Select
select = Select(title='Occupation', options=full_table['TW_Occupation'].tolist(), value='')
# Attach the update_plot callback to the 'value' property of select
select.on_change('value', update_plot)
#layout
layout = row(p, select)
# Add the plot to the current document
curdoc().add_root(layout)
Question 2: I also wish to show an annotation text box aside the selected circle which looks like the hover tool, just without hovering on it.
Like this:
