Plotly choropleth:处理多边形和边界的联合

问题描述 投票:0回答:1

我正在尝试制作法国城市的分区统计图(这意味着按比例为城市着色)。但是,我想加强部门的边界(这是城市的连接联盟)

我设法创建一个包含城市和部门的 geo-json 文件(作为多边形与 Shapely 库的联合)。 geo-json 字典包含特征(每个多边形),其中“id”获取位置,例如:

geojson_code['features'][0]['id']

但我不知道如何在较差的 Chloropeth 函数中指定如何修改边框。目前,我有一个包含两列“code”(5 个字符表示城市,如“75011”,2 个字符表示部门,如“75”)和“val”的数据框。有没有办法添加边框列?

fig = px.choropleth(df_code,
                    geojson=geojson_code, 
                    locations=df_code.code, color='val',
                           color_continuous_scale="Viridis",
                           #range_color=(0, 12),
                           scope="europe",
                           labels={'val':'Nombre de demandes'})
fig.show()

我听说过 update_traces 函数,但我没能处理好

python plotly choropleth
1个回答
0
投票

作为地图的自定义,突出显示特定边界的方法需要使用图形对象。由于您的问题没有提供 geojson 数据,因此我创建了更合适的示例数据,取自here。我创建了县总数的线宽列表和线条颜色的颜色列表,并且更改了标记的一些线宽和线条颜色,但我使用了 featureidkey='properties.code ' 因为我需要将它与 geojson 关联,但是您的 geojson 数据在您的 geojson 数据中使用了 'properties.id'。

import plotly.graph_objects as go
from urllib import request
import json

np.random.seed(1)
url = 'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements-version-simplifiee.geojson'
with request.urlopen(url) as f:
    fr_departments = json.load(f)

departments = []
for i in range(len(fr_departments['features'])):
    department = fr_departments['features'][i]['properties']
    departments.append([department['code'], department['nom']])

df = pd.DataFrame(departments, columns=['code','nom'])
df['val'] = np.random.randint(0, 500, size=len(df))

border = np.ones(len(df))
border[77] = 2
border_color = ['black']*len(df)
border_color[77] = 'red'

fig = go.Figure(data=go.Choropleth(
    geojson=fr_departments,
    locations=df['code'],
    z=df['val'].astype(float), 
    featureidkey='properties.code', 
    colorscale='Viridis',
    colorbar_title='Nombre de demandes',
    marker_line_width=border,
    marker_line_color=border_color,
))
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(height=500)
fig.show()

© www.soinside.com 2019 - 2024. All rights reserved.