2 回答
TA贡献1802条经验 获得超6个赞
这段代码可以满足您的要求,但是,我认为GeoDataFrame直接操作而不是转换为JSON. 此代码与 Bokeh v1.0.4 兼容。
from bokeh.models import GeoJSONDataSource, CategoricalColorMapper
from bokeh.plotting import figure, show
from bokeh.io import export_png
import geopandas as gpd
import random
import json
gdf = gpd.GeoDataFrame.from_file("Judete/Judete.shp")
gdf_json = gdf.to_json()
gjson = json.loads(gdf_json)
categories = ['A', 'B', 'C', 'D', 'E']
for item in gjson['features']:
item['properties']['category'] = random.choice(categories)
source_shapes = {}
for category in categories:
source_shapes[category] = {"type": "FeatureCollection", "features": []}
for item in gjson['features']:
source_shapes[item['properties']['category']]['features'].append(item)
p = figure(match_aspect = True, min_border = 0,
h_symmetry = False, v_symmetry = False,
x_axis_location = None, y_axis_location = None)
cmap = CategoricalColorMapper(palette = ["orange", "purple", "pink", "brown", "blue"],
factors = ['A', 'B', 'C', 'D', 'E'])
for category in categories:
source_shape = GeoJSONDataSource(geojson = json.dumps(source_shapes[category]))
p.patches('xs', 'ys', fill_color = {'field': 'category', 'transform': cmap},
line_color = 'black', line_width = 0.5,
legend = category, source = source_shape,)
p.legend.click_policy = 'hide'
show(p) # export_png(p, filename = "map.png")
结果:
TA贡献1865条经验 获得超7个赞
似乎图例当前不与 GeoJSONDataSource 一起使用,因为存在一个未解决的未解决问题Legend 不与 GeoJSONDataSource #5904 一起使用。
添加回答
举报