import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.path as mpath
def plot_polar_map(dmeridian: float = 30.0, # 经度网格线间隔
dparallel: float = 15.0): # 纬度网格线间隔
"""
绘制北极区域的极区投影地图,并添加经纬度网格和标签。
参数:
dmeridian (float): 经度网格线的间隔(单位:度),默认为30.0
dparallel (float): 纬度网格线的间隔(单位:度),默认为15.0
"""
# 创建图形对象,设置尺寸为8x8英寸
fig = plt.figure(figsize=[8, 8])
# 设置北极区域的极区投影
projection = ccrs.NorthPolarStereo()
ax = plt.axes(projection=projection)
# 绘制海岸线
ax.coastlines(linewidths=0.5)
# 添加陆地特征,设置颜色为浅灰色
ax.add_feature(cfeature.LAND, facecolor='lightgray')
# 设置显示范围,限制显示区域为北极
ax.set_extent([0, 360, 0, 90], ccrs.PlateCarree())
# 计算经纬度网格线的数量
num_merid = int(360.0 / dmeridian + 1.0) # 经度网格线的数量
num_parra = int(90.0 / dparallel + 1.0) # 纬度网格线的数量
# 绘制网格线
gl = ax.gridlines(crs=ccrs.PlateCarree(),
xlocs=np.linspace(0.0, 360.0, num_merid),
ylocs=np.linspace(0.0, 90.0, num_parra),
linestyle="--", linewidth=1, color='k', alpha=0.5)
# 创建圆形路径用于设置图像边界
theta = np.linspace(0, 2 * np.pi, 120)
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
center, radius = [0.5, 0.5], 0.5
circle = mpath.Path(verts * radius + center)
# 设置图形边界为圆形
ax.set_boundary(circle, transform=ax.transAxes)
# 设置标签对齐方式
va = 'center' # 垂直对齐方式,选项有:'center', 'bottom', 'top'
ha = 'center' # 水平对齐方式,选项有:'right', 'left'
# 设置度符号
degree_symbol = u'\u00B0'
# 绘制经度标签
lond = np.linspace(0, 360, num_merid)
latd = np.zeros(len(lond))
for alon, alat in zip(lond, latd):
projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
if alon > 0 and alon < 180:
ha = 'left'
va = 'center'
if alon > 180 and alon < 360:
ha = 'right'
va = 'center'
if np.abs(alon - 180) < 0.01:
ha = 'center'
va = 'bottom'
if alon == 0.:
ha = 'center'
va = 'top'
if alon < 360.:
txt = f' {int(alon)} ' + degree_symbol
ax.text(projx1, projy1, txt, va=va, ha=ha, color='g')
# 绘制纬度标签(这里选择了经度315作为示例)
lond2 = 315 * np.ones(len(lond))
latd2 = np.linspace(0, 90, num_parra)
va, ha = 'center', 'center'
for alon, alat in zip(lond2, latd2):
projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
txt = f' {int(alat)} ' + degree_symbol
ax.text(projx1, projy1, txt, va=va, ha=ha, color='r')
# 设置窗口标题
fig.canvas.manager.set_window_title('北极区域极区投影地图')
# 显示绘制的图形
plt.show()
# 调用函数绘制极区地图
plot_polar_map(dmeridian=30.0, dparallel=15.0)
查看绘制南极代码
import matplotlib.pyplot as plt
import numpy as np
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.path as mpath
def plot_south_polar_map(dmeridian: float = 30.0, # 经度网格线间隔
dparallel: float = 15.0): # 纬度网格线间隔
"""
绘制南极区域的极区投影地图,并添加经纬度网格和标签。
参数:
dmeridian (float): 经度网格线的间隔(单位:度),默认为30.0
dparallel (float): 纬度网格线的间隔(单位:度),默认为15.0
"""
# 创建图形对象,设置尺寸为8x8英寸
fig = plt.figure(figsize=[8, 8])
# 设置南极区域的极区投影
projection = ccrs.SouthPolarStereo()
ax = plt.axes(projection=projection)
# 绘制海岸线
ax.coastlines(linewidths=0.5)
# 添加陆地特征,设置颜色为浅灰色
ax.add_feature(cfeature.LAND, facecolor='lightgray')
# 设置显示范围,限制显示区域为南极
ax.set_extent([0, 360, -90, 0], ccrs.PlateCarree())
# 计算经纬度网格线的数量
num_merid = int(360.0 / dmeridian + 1.0) # 经度网格线的数量
num_parra = int(90.0 / dparallel + 1.0) # 纬度网格线的数量
# 绘制网格线
gl = ax.gridlines(crs=ccrs.PlateCarree(),
xlocs=np.linspace(0.0, 360.0, num_merid),
ylocs=np.linspace(-90.0, 0.0, num_parra),
linestyle="--", linewidth=1, color='k', alpha=0.5)
# 创建圆形路径用于设置图像边界
theta = np.linspace(0, 2 * np.pi, 120)
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
center, radius = [0.5, 0.5], 0.5
circle = mpath.Path(verts * radius + center)
# 设置图形边界为圆形
ax.set_boundary(circle, transform=ax.transAxes)
# 设置标签对齐方式
va = 'center' # 垂直对齐方式,选项有:'center', 'bottom', 'top'
ha = 'center' # 水平对齐方式,选项有:'right', 'left'
# 设置度符号
degree_symbol = u'\u00B0'
# 绘制经度标签
lond = np.linspace(0, 360, num_merid)
latd = np.zeros(len(lond))
for alon, alat in zip(lond, latd):
projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
if alon > 0 and alon < 180:
ha = 'left'
va = 'center'
if alon > 180 and alon < 360:
ha = 'right'
va = 'center'
if np.abs(alon - 180) < 0.01:
ha = 'center'
va = 'bottom'
if alon == 0.:
ha = 'center'
va = 'top'
if alon < 360.:
txt = f' {int(alon)} ' + degree_symbol
ax.text(projx1, projy1, txt, va=va, ha=ha, color='g')
# 绘制纬度标签(这里选择了经度315作为示例)
lond2 = 315 * np.ones(len(lond))
latd2 = np.linspace(-90, 0, num_parra)
va, ha = 'center', 'center'
for alon, alat in zip(lond2, latd2):
projx1, projy1 = ax.projection.transform_point(alon, alat, ccrs.Geodetic())
txt = f' {int(alat)} ' + degree_symbol
ax.text(projx1, projy1, txt, va=va, ha=ha, color='r')
# 设置窗口标题
fig.canvas.manager.set_window_title('南极区域极区投影地图')
# 显示绘制的图形
plt.show()
# 调用函数绘制南极地图
plot_south_polar_map(dmeridian=30.0, dparallel=15.0)