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

如何移除Cartopy拼接子图投影间的分隔线?

解决Cartopy拼接子图的分隔线问题

以下是移除两个子图中间分隔线的具体修改方案:

修改后的完整代码

import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature

fig = plt.figure(figsize=(25,13), facecolor='white')
# 设置wspace为0,彻底消除子图间的间距
gs = fig.add_gridspec(1,2, width_ratios=[4,2], height_ratios=[1], hspace=0.2, wspace=0)

# 调整两个子图的经度范围,让它们在0°经线精准衔接
ax1 = fig.add_subplot(gs[0,0], projection=ccrs.PlateCarree())
ax2 = fig.add_subplot(gs[0,1], projection=ccrs.PlateCarree()) 

ax1.set_extent([0, 180, -90, 90])
ax2.set_extent([-180, 0, -90, 90])

# 添加地图要素
ax1.add_feature(cfeature.LAND, color='lightgray')
ax2.add_feature(cfeature.LAND, color='lightgray')
ax1.add_feature(cfeature.COASTLINE)
ax2.add_feature(cfeature.COASTLINE)

# 隐藏中间的分隔边框
ax1.spines['right'].set_visible(False)
ax2.spines['left'].set_visible(False)

# 可选:隐藏内侧刻度,让拼接效果更自然
ax1.set_xticks([])
ax2.set_xticks([])
ax1.set_yticks([])
ax2.set_yticks([])

plt.show()

关键修改说明

  • 调整子图范围:原代码中ax1覆盖了全经度范围,和ax2的[-180,0]区域重叠,导致中间出现重复的海岸线和边框。修改后ax1负责[0,180]经度,ax2负责[-180,0],两者在0°经线精准衔接,避免重叠。
  • 消除子图间距:确保wspace=0,让两个子图完全贴合,没有空隙。
  • 隐藏内侧边框:通过spines属性分别隐藏ax1的右侧边框和ax2的左侧边框,直接去掉中间的分隔线。
  • 隐藏内侧刻度(可选):如果不需要显示刻度,移除内侧的刻度和标签,让拼接后的地图看起来更像一个完整的整体。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 05:03:28