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)