2 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊
這段代碼可以滿足您的要求,但是,我認(rèn)為GeoDataFrame直接操作而不是轉(zhuǎn)換為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")
結(jié)果:

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
似乎圖例當(dāng)前不與 GeoJSONDataSource 一起使用,因?yàn)榇嬖谝粋€(gè)未解決的未解決問題Legend 不與 GeoJSONDataSource #5904 一起使用。
添加回答
舉報(bào)