该程序的主要特点:
- 使用Cartopy创建地图投影
- 添加海岸线、国界线等地理要素
- 绘制等值线图显示气象要素分布
- 自动设置颜色标尺和标题
- 支持不同层次的数据展示
import matplotlib.pyplot as plt
def plot_vertical_cross_section(data, start_lat, start_lon,
end_lat, end_lon):
"""绘制垂直剖面图"""
# 计算剖面线上的点
num_points = 100
lats = np.linspace(start_lat, end_lat, num_points)
lons = np.linspace(start_lon, end_lon, num_points)
# 插值到剖面线上
from scipy.interpolate import griddata
# 创建网格点
grid_lats, grid_z = np.meshgrid(lats, data['elevation'])
# 对温度进行插值
temp_cross = np.zeros((data['temperature'].shape[0], len(lats)))
for z in range(data['temperature'].shape[0]):
points = np.array([data['latitude'].flatten(),
data['longitude'].flatten()]).T
temp_cross[z, :] = griddata(
points,
data['temperature'][z].flatten(),
(lats, lons),
method='linear'
)
# 绘图
fig, ax = plt.subplots(figsize=(12, 6))
levels = np.linspace(np.min(temp_cross), np.max(temp_cross), 15)
cs = ax.contourf(lats, range(data['temperature'].shape[0]),
temp_cross, levels=levels, cmap='RdBu_r')
plt.colorbar(cs, ax=ax, label='Temperature (K)')
ax.set_xlabel('Latitude')
ax.set_ylabel('Model Level')
ax.set_title('Temperature Vertical Cross Section')
plt.show()
# 使用示例
plot_vertical_cross_section(data, 30.0, 120.0, 35.0, 125.0)