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

如何调整Plotly Choropleth地图初始显示区域,使加拿大省份居中?

解决Plotly Choropleth地图以加拿大省份为中心的问题

可以通过两种方式调整地图初始视角,让加拿大省份成为展示中心:

方法1:手动指定地图中心坐标与缩放级别

加拿大的大致中心坐标为纬度56°N、经度**-96°W**,直接在update_geos中设置center和zoom参数,强制地图聚焦加拿大区域:

import plotly.express as px
import json  # 补充原代码缺失的json模块导入

with open("canada_provinces.geojson", 'r') as f:
    geojson = json.load(f)

fig = px.choropleth(test_df, geojson=geojson, locations='prov', color="median_wage",  
                    color_continuous_scale="Viridis",
                    featureidkey= 'properties.name',
                    scope='north america')
# 调整地图中心与缩放级别,同时保留fitbounds确保省份完整显示
fig.update_geos(
    center=dict(lat=56, lon=-96),
    zoom=2.5,
    fitbounds='locations',
    visible=True
)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

方法2:适配GeoJSON边界自动聚焦

如果你的canada_provinces.geojson仅包含加拿大省份数据,将fitbounds参数改为'geojson',让地图自动适配整个GeoJSON的范围:

import plotly.express as px
import json

with open("canada_provinces.geojson", 'r') as f:
    geojson = json.load(f)

fig = px.choropleth(test_df, geojson=geojson, locations='prov', color="median_wage",  
                    color_continuous_scale="Viridis",
                    featureidkey= 'properties.name',
                    scope='north america')
# 改为适配GeoJSON的边界范围
fig.update_geos(fitbounds='geojson', visible=True)
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.show()

注意事项:

  • 确保canada_provinces.geojson仅包含加拿大省份的地理数据,无多余区域。
  • 若方法1的缩放效果不符合预期,可微调zoom值(数值越大,地图放大程度越高)。

内容的提问来源于stack exchange,提问作者Alejandro L

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 05:35:12