0

我是 python 新手,想用课堂上给出的数据集制作一个交互式图表。我希望能够简单地显示与我在 Bokeh 中使用 TapTool 选择的点相关联的数据。数据太密集,无法简单地使用悬停工具。似乎悬停工具不需要 javascript,但显然获取点击工具需要了解其他编码语言。我对python并不熟悉,所以添加一个javascript回调会让事情变得有点复杂。

现在,我使用来自 sci-kit 的 California Housing 数据集的数据用我的数据框绘制了这个数字。我也有来自散景的 US_states 样本数据来绘制。

from bokeh.layouts import layout
from bokeh.plotting import figure, show, output_file
from bokeh.models import Circle, Div, ColumnDataSource, CustomJS, MultiChoice, HoverTool, LinearColorMapper, ColorBar
from bokeh.io import output_notebook
from bokeh.sampledata import us_states
from bokeh.transform import transform
from bokeh.models.tools import *

#US state sample data
us_states = us_states.data.copy()
us_states = us_states["CA"]



#Creating figure and adding california basemap
color = LinearColorMapper(palette = 'Viridis256',
                      low = df.MedInc.min(),
                      high = df.MedInc.max())
color2 = LinearColorMapper(palette = 'Inferno256',
                      low = df.price.min(),
                      high = df.price.max())

cal = figure(title = "California Housing Data Geographic Distribution",
         plot_width = 1000)

cal.circle('Longitude','Latitude',source = geo_source, 
       color= transform('MedInc', color),size =2,
       alpha = 0.2,legend_label = "Median Income")

cal.circle('Longitude','Latitude',source = geo_source, 
       color= transform('price', color2),size =1,
       alpha = 0.2,legend_label = "House Price")



 color_bar = ColorBar(color_mapper = color,
                 label_standoff = 14,
                 location = (0,0),
                 title = 'Median Income')

 color_bar2 = ColorBar(color_mapper = color2,
                 label_standoff = 14,
                 location = (0,0),
                 title = 'House Price')

 cal.add_layout(color_bar,'right')
 cal.add_layout(color_bar2,'right')

 cal.legend.location = "top_right"
 cal.legend.click_policy="hide"
                      

我不知道要为 TapTool 的 JS 回调写什么,因为我不确定它是如何工作的,变量如何传输等等。

callback = CustomJS(args=dict(source=geo_source), code="""
//Insert JS code here
""")

taptool = TapTool(callback=callback)
geo_source.selected.js_on_change('indices', callback)
cal.add_tools(taptool)
4

1 回答 1

0

您应该将图形添加到 Tap 事件

plot.on_event(Tap, function)

在功能上,您可以使用索引来更新您的数据/图表

def function():
    print(source.selected.indices)
于 2021-09-28T11:48:31.767 回答