import netCDF4 as nc import numpy as np import matplotlib.pyplot as plt filename = r'C:\Users\24759\.spyder-py3\wrfout_d01_2020-08-20_02_00_00 (1)' data = nc.Dataset(filename) temperature = data.variables['T'][:] qrain = data.variables['QRAIN'][:] qcloud= data.variables['QCLOUD'][:] subzero_temperature = temperature < 273 overcooled_water = np.logical_and(subzero_temperature, np.logical_or(qcloud > 0, qrain > 0)) t_2d = temperature[0, 0, :, :] overcooled_water_grid = np.zeros_like(t_2d, dtype=bool) overcooled_water_grid[overcooled_water[0, 0, :, :]] = True x = np.linspace(0, 1, t_2d.shape[1]) y = np.linspace(0, 1, t_2d.shape[0]) fig, ax = plt.subplots(figsize=(12, 8)) cf = ax.contourf(x, y, t_2d, cmap='coolwarm') cbar = plt.colorbar(cf) ax.contour(x, y, overcooled_water_grid, colors='green', linewidths=1) ax.set_title("Temperature and Overcooled Water Distribution") ax.set_xlabel("Longitude") ax.set_ylabel("Latitude") cbar.ax.set_ylabel('Temperature (K)', rotation=270, labelpad=15) plt.show()在这段代码的基础上画出(100,25)到(120,36)的航线图
时间: 2024-01-30 13:04:18 浏览: 173
要画出(100,25)到(120,36)的航线图,需要先找到这两个点在数据中对应的位置。可以通过以下方式计算:
```python
# 获取经纬度数据
lon = data.variables['XLONG'][0]
lat = data.variables['XLAT'][0]
# 找到最接近给定经纬度的索引值
start_lon_idx = np.abs(lon[0] - 100).argmin()
start_lat_idx = np.abs(lat[:, 0] - 25).argmin()
end_lon_idx = np.abs(lon[0] - 120).argmin()
end_lat_idx = np.abs(lat[:, 0] - 36).argmin()
```
然后,可以使用Matplotlib中的`plot`函数绘制两个点之间的直线:
```python
fig, ax = plt.subplots(figsize=(12, 8))
# 绘制温度和过冷水分布
cf = ax.contourf(x, y, t_2d, cmap='coolwarm')
cbar = plt.colorbar(cf)
ax.contour(x, y, overcooled_water_grid, colors='green', linewidths=1)
# 绘制航线
ax.plot([start_lon_idx, end_lon_idx], [start_lat_idx, end_lat_idx], color='red', linewidth=2)
ax.set_title("Temperature and Overcooled Water Distribution")
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
cbar.ax.set_ylabel('Temperature (K)', rotation=270, labelpad=15)
plt.show()
```
完整代码如下:
```python
import netCDF4 as nc
import numpy as np
import matplotlib.pyplot as plt
filename = r'C:\Users\24759\.spyder-py3\wrfout_d01_2020-08-20_02_00_00 (1)'
data = nc.Dataset(filename)
temperature = data.variables['T'][:]
qrain = data.variables['QRAIN'][:]
qcloud= data.variables['QCLOUD'][:]
subzero_temperature = temperature < 273
overcooled_water = np.logical_and(subzero_temperature, np.logical_or(qcloud > 0, qrain > 0))
t_2d = temperature[0, 0, :, :]
overcooled_water_grid = np.zeros_like(t_2d, dtype=bool)
overcooled_water_grid[overcooled_water[0, 0, :, :]] = True
# 获取经纬度数据
lon = data.variables['XLONG'][0]
lat = data.variables['XLAT'][0]
# 找到最接近给定经纬度的索引值
start_lon_idx = np.abs(lon[0] - 100).argmin()
start_lat_idx = np.abs(lat[:, 0] - 25).argmin()
end_lon_idx = np.abs(lon[0] - 120).argmin()
end_lat_idx = np.abs(lat[:, 0] - 36).argmin()
fig, ax = plt.subplots(figsize=(12, 8))
# 绘制温度和过冷水分布
cf = ax.contourf(x, y, t_2d, cmap='coolwarm')
cbar = plt.colorbar(cf)
ax.contour(x, y, overcooled_water_grid, colors='green', linewidths=1)
# 绘制航线
ax.plot([start_lon_idx, end_lon_idx], [start_lat_idx, end_lat_idx], color='red', linewidth=2)
ax.set_title("Temperature and Overcooled Water Distribution")
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
cbar.ax.set_ylabel('Temperature (K)', rotation=270, labelpad=15)
plt.show()
```
阅读全文