You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:52:51