在散景图上更新标签文本

时间:2020-03-24 14:14:29

标签: python visualization bokeh

我正在尝试构建一个图,以显示哪个工作更容易受到Bokeh的[this] [1]等病毒的影响。 当我选择的值更改时,我尝试更新标签。但是,它会延迟更改。例如,当我首先在select中选择“ nurse”时,它没有显示标签,然后当我选择“ doctor”时,它在图上显示了“ nurse”。请帮助我修复它。非常感谢。

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)```

Qeustion 2: 
I also want to wish to show an annotation text box aside the selected circle which looks like the hover tool just without hovering on it. 
[enter image description here][2]


  [1]: https://www.nytimes.com/interactive/2020/03/15/business/economy/coronavirus-worker-risk.html?smid=fb-nytimes&smtyp=cur&fbclid=IwAR0UG6dj1eqMilukx9wit5FX4P4TxAodtvW8b0toGyYDCKygM087uZr8P38
  [2]: https://i.stack.imgur.com/El7Dv.png

0 个答案:

没有答案