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

如何在Cartopy中设置0.25°×0.25°的密集方格网格?

解决Cartopy绘制0.25°×0.25°密集网格的问题

你可以通过自定义gridlines()方法的xlocs和ylocs参数,手动生成间隔为0.25°的经纬度数组来实现密集网格:

  1. 用numpy.arange()生成指定步长的经纬度序列:
    • 经度范围设为-180°到180°,步长0.25°
    • 纬度范围设为-90°到90°,步长0.25°
  2. 将生成的序列传入gridlines()的对应参数即可。

修改后的完整代码:

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np

proj = ccrs.PlateCarree()
fig, ax = plt.subplots(1, 1, subplot_kw={'projection': proj})
ax.coastlines()

# 生成0.25°间隔的经纬度数组
lon_steps = np.arange(-180, 180.25, 0.25)
lat_steps = np.arange(-90, 90.25, 0.25)

# 绘制密集网格并添加标签
gl = ax.gridlines(xlocs=lon_steps, ylocs=lat_steps, draw_labels=True)
# 可选:调整标签字体大小避免重叠
gl.xlabel_style = {'size': 6}
gl.ylabel_style = {'size': 6}

plt.show()

说明

  • np.arange(-180, 180.25, 0.25)确保包含180°边界值,同理纬度参数包含90°
  • 密集网格会导致标签密集,通过xlabel_style和ylabel_style调整字体大小可以缓解重叠问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 22:27:04