最初,我创建了一个英国邮政编码区域的交互式地图,其中单个区域根据其值(例如该邮政编码区域中的人口)进行颜色表示,如下所示。
from bokeh.plotting import figure
from bokeh.palettes import Viridis256 as palette
from bokeh.models import LinearColorMapper
from bokeh.models import ColumnDataSource
import geopandas as gpd
shp = 'file_path_to_the_downloaded_shapefile'
#read shape file into dataframe using geopandas
df = gpd.read_file(shp)
def expandMultiPolygons(row, geometry):
if row[geometry].type = 'MultiPolygon':
row[geometry] = [p for p in row[geometry]]
return row
#Some rows were in MultiPolygons instead of Polygons.
#Expand MultiPolygons to multi rows of Polygons
df = df.apply(expandMultiPolygons, geometry='geometry', axis=1)
df = df.set_index('Area')['geometry'].apply(pd.Series).stack().reset_index()
#Visualize the polygons. To visualize different colors for different post areas, I added another column called 'value' which has some random integer value.
p = figure()
color_mapper = LinearColorMapper(palette=palette)
source = ColumnDataSource(df)
p.patches('x', 'y', source=source,\
fill_color={'field': 'value', 'transform': color_mapper},\
fill_alpha=1.0, line_color="black", line_width=0.05)
其中 df 是四列的数据框:邮政编码区域、x 坐标、y 坐标、值(即人口)。
上面的代码在网络浏览器上创建了一个交互式地图,这很棒,但我注意到交互的速度不是很流畅。如果我放大或移动地图,它会缓慢渲染。数据框的大小只有 1106 行,所以我很困惑为什么它这么慢。
作为可能的解决方案之一,我遇到了 datashader ( https://datashader.readthedocs.io/en/latest/ ),但我发现示例脚本非常复杂,其中大多数都带有 Jupyter notebook 上的 holoview 包,但我想要使用散景创建仪表板。
有人建议我将数据着色器合并到上述散景脚本中吗?我是否需要在 datashader 中使用不同的函数来创建形状图而不是使用散景的补丁函数?
任何建议将不胜感激!!!