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

如何调整contextily底图缩放?Jupyter可视化边界问题求助

解决contextily底图缩放/边界问题的方法

1. 调整zoom参数(最直接)

contextily的add_basemap里的zoom参数直接决定底图缩放级别:数值越大,底图放大越明显(细节多但显示范围小);数值越小,显示范围越大。

  • 美国图当前用zoom=5,如果觉得显示范围太小,降低zoom值(比如改成4);
  • 西班牙图出现边界溢出,说明zoom太大导致范围过窄,降低zoom值或者配合坐标轴范围调整。

2. 手动设置坐标轴范围(精准控幅)

在添加底图前,用ax.set_xlim()和ax.set_ylim()手动限定经纬度范围,确保底图和数据匹配:

# 美国全境大致经纬度范围,可根据你的数据微调
axUsa.set_xlim(-125, -66)
axUsa.set_ylim(24, 50)
# 再执行底图添加操作
ctx.add_basemap(
    axUsa,
    crs=geoTr.crs.to_string(),
    zoom=5,
    source=ctx.providers.CartoDB.Positron
)

底图会自动适配你设置的坐标轴范围,彻底解决溢出或范围过小问题。

3. 用extent参数强制锁定底图范围

在add_basemap里直接指定extent参数(格式为[xmin, xmax, ymin, ymax]),优先级高于坐标轴设置:

ctx.add_basemap(
    axUsa,
    crs=geoTr.crs.to_string(),
    zoom=5,
    source=ctx.providers.CartoDB.Positron,
    extent=[-125, -66, 24, 50]  # 美国经纬度范围
)

优化后的美国绘图代码示例

import matplotlib.pyplot as plt
import contextily as ctx

topUsa = unionTopFive[unionTopFive["country_name"] == "United States"]

mortiUsa = geoTr[(geoTr['country_name'] == 'United States') & (geoTr['nkill'] > 0)]
feritiUsa = geoTr[(geoTr['country_name'] == 'United States') & (geoTr['nwound'] > 0)]

mortiUsa['nkill'] *= 1.1 # 缩放可视化效果
feritiUsa['nwound'] *= 1.1

# 统一在subplots里设置画布尺寸,避免重复设置冲突
fig, axUsa = plt.subplots(figsize=(13,13))
mortiUsa.plot(ax=axUsa, column='nkill', alpha=0.5, marker='o', color="black", label="Morti", markersize="nkill")
feritiUsa.plot(ax=axUsa, marker='o', color='red', alpha=0.3, label="Feriti", markersize="nwound")

# 手动限定美国显示范围
axUsa.set_xlim(-125, -66)
axUsa.set_ylim(24, 50)

ctx.add_basemap(
    axUsa,
    crs=geoTr.crs.to_string(),
    zoom=5,  # 可根据显示效果调整,比如改成4显示更大范围
    source=ctx.providers.CartoDB.Positron
)

axUsa.legend(frameon=True, loc="upper left", markerscale=0.2, facecolor="white", framealpha=1, fontsize=11)
axUsa.set_axis_off()

# 注释内容保留原逻辑
axUsa.annotate(topUsa.iloc[0,:]["annotateOnly"],
                xy=(topUsa.iloc[0,:]["longitude"], topUsa.iloc[0,:]["latitude"]), 
                xytext=(topUsa.iloc[0,:]["longitude"]-11, topUsa.iloc[0,:]["latitude"]+5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[2,:]["annotateOnly"],
                xy=(topUsa.iloc[2,:]["longitude"], topUsa.iloc[2,:]["latitude"]), 
                xytext=(topUsa.iloc[2,:]["longitude"]-11, topUsa.iloc[2,:]["latitude"]+0.5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[1,:]["annotateOnly"],
                xy=(topUsa.iloc[1,:]["longitude"], topUsa.iloc[1,:]["latitude"]), 
                xytext=(topUsa.iloc[1,:]["longitude"]+5, topUsa.iloc[1,:]["latitude"]-4.4), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[3,:]["annotateOnly"],
                xy=(topUsa.iloc[3,:]["longitude"], topUsa.iloc[3,:]["latitude"]), 
                xytext=(topUsa.iloc[3,:]["longitude"]-5, topUsa.iloc[3,:]["latitude"]+5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

axUsa.annotate(topUsa.iloc[4,:]["annotateOnly"],
                xy=(topUsa.iloc[4,:]["longitude"], topUsa.iloc[4,:]["latitude"]), 
                xytext=(topUsa.iloc[4,:]["longitude"]+1, topUsa.iloc[4,:]["latitude"]-5), xycoords='data', textcoords='data',
            arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2", color='black'), size=9,
            bbox=dict(boxstyle="round,pad=0.3", edgecolor='black', facecolor='#e2e2e2'))

plt.show()

补充说明

  • 西班牙图如果溢出,可将坐标轴范围调整为西班牙区域(比如xlim: -10, 5;ylim: 35, 44),或适当降低zoom值;
  • 不要在两次plot调用中重复设置figsize,统一在plt.subplots()里设置即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 04:23:14