python-nc文件的处理和画图

(1)

(2)

import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset

# 读取 NetCDF 文件
nc_file = "surface_LHSH_monthlymeans.nc"
nc_data = Dataset(nc_file, "r")

# 获取经度和纬度数据
lons = nc_data.variables["longitude"][:]
lats = nc_data.variables["latitude"][:]

# 获取感热和潜热通量数据
slhf_data = nc_data.variables["slhf"][:]
sshf_data = nc_data.variables["sshf"][:]

# 成都的经纬度
chengdu_lon, chengdu_lat = 104.0657, 30.6595

# 计算每个格点到成都的距离
lon_grid, lat_grid = np.meshgrid(lons, lats)
distance = np.sqrt((lon_grid - chengdu_lon)**2 + (lat_grid - chengdu_lat)**2)

# 筛选出500km范围内的格点
within_range = distance <= 500

# 初始化用于存储区域平均值的数组
slhf_mean = []
sshf_mean = []

# 选择每隔一定时间步长取样数据
time_step_interval = 12  # 每隔12个时间步长取样一次
for i in range(0, slhf_data.shape[0], time_step_interval):
    slhf_mean.append(np.mean(slhf_data[i][within_range]))
    sshf_mean.append(np.mean(sshf_data[i][within_range]))

# 绘制折线图
plt.plot(slhf_mean, label="Sensible Heat Flux")
plt.plot(sshf_mean, label="Latent Heat Flux")
plt.xlabel("Time Step")
plt.ylabel("Flux (W/m^2)")
plt.title("Regional Mean Heat Flux plotted by 202183 chengdu")
plt.legend()

# 保存图片
plt.savefig("2300xxxPlot4_python-2.png")

# 显示图形
plt.show()

作者:Dreamer_Amily

物联沃分享整理
物联沃-IOTWORD物联网 » python-nc文件的处理和画图

发表回复