Plotly Express Choropleth Mapbox添加区域文本失败,求解决方法
Plotly Express Choropleth Mapbox 区域文本标注不显示问题
我尝试在Plotly Express的Choropleth Mapbox地图的每个国家区域添加文本,以下是我的核心代码:
fig = px.choropleth_mapbox(df, geojson=geojson, color="VS 2022 MTD", locations=total_categoria["depto"], featureidkey="properties.name", center={"lat": -33.38160714046037, "lon": -56.5245340}, mapbox_style="carto-positron", zoom=5,color_continuous_scale="RdYlGn", ) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show()
我尝试了两种方法都没成功:
- 添加
hover_name=total_categoria["depto"],但这仅能在hover时显示名称,无法直接在地图区域上显示文本 - 使用以下代码循环添加文本标注:
# Add text annotations for i, row in total_categoria.iterrows(): fig.add_trace(go.Scattermapbox( lat=[row["latitude"]], # 假设DataFrame中有"latitude"列 lon=[row["longitude"]], # 假设DataFrame中有"longitude"列 mode='text', text=row["depto"], textposition="bottom", ))
但始终无法在各区域位置显示文本,当前地图仅显示区域着色效果,没有任何文本标注。请问有解决思路吗?
内容的提问来源于stack exchange,提问作者Mateo Severi
相关产品推荐
相关产品推荐

