我正在尝试在Jupyter笔记本上绘制一个带有bokeh的交互式地图,其中一些城市位于巴西地图的顶部。但是,我无法添加交互性,因此,如果用户从下拉菜单中选择城市,则地图只会显示该城市。这就是我所拥有的。我正在运行bokeh 0.12.16和jupyter 5.7.0
用于获取巴西国家/地区的纬度和经度的功能。所需的形状文件可以在这里shp file brazil zip
下载import geopandas as gpd
def getXYcoordinates(path_to_shp):
# File path
shapefile=path_to_shp
points = gpd.read_file(shapefile)
# Geometry of polygons
geom = points["geometry"]
# Obtain x,y points
x=[]
y=[]
for polygon in geom:
if polygon.type=='Polygon':
x.append(polygon.exterior.coords.xy[0])
y.append(polygon.exterior.coords.xy[1])
else:
for small_polygon in polygon:
x.append(small_polygon.exterior.coords.xy[0])
y.append(small_polygon.exterior.coords.xy[1])
#Return x,y
estados_xs=[list(code) for code in x]
estados_ys=[list(code) for code in y]
return (estados_xs,estados_ys)
estado_xs,estado_ys=getXYcoordinates('your/path')
从那里,我有我的bokeh应用程序:
import numpy as np
from bokeh.io import show, output_notebook
from bokeh.plotting import figure
from bokeh.application.handlers import FunctionHandler
from bokeh.application import Application
from bokeh.models import ColumnDataSource, WidgetBox,Select
from bokeh.layouts import row
# Display plots in the notebook
output_notebook()
def modify_doc(doc):
def make_dataset(estado_xs,estado_ys,choice,scale=1):
#make cities data
x=[-68.10552979, -35.88986206, -35.90269852, -35.91287613, -36.30236053, -50.82569504, -51.2868042, -49.99347305, -49.98902893, -49.91069412]
y=[-10.72191715, -9.84430599, -9.86458302, -9.84375, -10.34274101, 2.522084, 4.25541687, 0.89958298, 0.95208299, 1.03291702]
cities=ColumnDataSource(data = {'x': x, 'y': y})
#make states data
estados= ColumnDataSource(data = {'x': estado_xs, 'y': estado_ys})
return estados,cities
def make_plot(estados,cities):
#create plot
p = figure(title="Brasil", toolbar_location="left",
plot_width=650, plot_height=500,y_range=(-36,7),x_range=(-75,-33))
p.xgrid.visible = False
p.ygrid.visible = False
#Draw circles claims
p.circle('x', 'y', source=cities,size=10, fill_alpha=.3, line_alpha=0)
# Draw state lines
p.patches('x', 'y' ,source=estados ,fill_alpha=0,line_alpha=.5,line_color="grey", line_width=1,fill_color='white')
return p
#update function
def update(attr, old, new):
x=[-68.10552979, -35.88986206, -35.90269852, -35.91287613, -36.30236053, -50.82569504, -51.2868042, -49.99347305, -49.98902893, -49.91069412]
y=[-10.72191715, -9.84430599, -9.86458302, -9.84375, -10.34274101, 2.522084, 4.25541687, 0.89958298, 0.95208299, 1.03291702]
if new=='All':
new_src=ColumnDataSource(data = {'x': x, 'y': y})
else:
new_src=ColumnDataSource(data = {'x': x[new], 'y': y[new]})
cities.data.update(new_src.data)
#create widgets
select_dono = Select(title='Dono:',options=['All','1','2','3','4','5'],value='All')
select_dono.on_change('value',update)
#create layouts
estados,claims = make_dataset(estado_xs,estado_ys,select_dono.value,scale=15)
p = make_plot(estados,claims)
layout = row(p,WidgetBox(select_dono, width = 100))
doc.add_root(layout)
handler = FunctionHandler(modify_doc)
app = Application(handler)
show(app)
这是我能够绘制的图像,没有交互: