不显示西班牙地区的Plotly Choropleth地图

2024-09-28 05:17:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用plotly express和Chloroleth显示一张西班牙自治区地图,根据失业率上色。 为了做到这一点,我使用了一个名为“df_indi_comunidades”的dataframe with an ID for each region and the unemployement rate 'Tasa de desempleo'.

区域轮廓以多多边形形式存储在geojson文件中,如下所示:

{'features': [{'geometry': {'coordinates': [[[[-6.969706149311866,
    37.21774252292089],
   [-6.975248071819635, 37.21073261917811],
   [-6.958622304296327, 37.183501839254234],
   [-6.937193537266287, 37.183501839254234],
   [-6.944582767276646, 37.203992327117746],
   [-6.969706149311866, 37.21774252292089]]], ...
'type': 'MultiPolygon'}, 'properties': {'ENGTYPE_1': 'Autonomous Community',
'HASC_1': 'ES.AN',
'ID_1': 935,
'ISO': 'ESP',
'NAME_0': 'Spain',
'NAME_1': 'Andalucía',
'TYPE_1': 'Comunidad Autónoma',
'VARNAME_1': 'Andalousie|Andaluc¡a|Andalusien|Andaluzia'},'type': 'Feature'}, {'geometry': {'coordinates': [[[-0.7276540980614321, 42.91922077094266],
  [-0.753516403097688, 42.92461300459095],
  [-0.7830733231391207, 42.92164727608439 

我把这个文件叫做“comunidades”。这是我第一次使用json文件,我对其结构不是很熟悉,所以这可能是问题所在

每个区域都可以通过dataframe中的'ID_1'和json中的properties.ID_1来识别。

我阅读了choropleth文档,据我所知,我应该使用featureid,但它不起作用:我得到一个空白映射,json文件似乎没有被正确读取。这是我正在写的代码:

    fig = px.choropleth(data_frame=df_indic_comunidades, geojson=comunidades, locations='ID_1', color='Tasa de desempleo', color_continuous_scale='Viridis', labels={'Tasa de desempleo':'unemployment rate'}, range_color=(0, df_indic_comunidades['Tasa de desempleo'].max()),
                    featureidkey='properties.ID_1')
fig.update_geos(fitbounds="locations")
fig.show()

this is the output:所有东西都是绿色的,所以我看不到这些区域的轮廓。。。但在盘旋时,我得到了正确的失业率和地区

为什么数据显示不正确?提前感谢你的帮助


Tags: 文件theidjson区域dataframedffig

热门问题