数据:era5 nc格式 核心函数:ax.plot_surface
实际上是上学期听学术报告看到类似的天气形势3D图,搜了挺多教程,效果差强人意吧 希望对你们有微小的帮助
In [1]:
import xarray as xr
import cmaps
import itertools
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection, PolyCollection
import numpy as np
import cartopy.feature as cf
from cartopy.mpl.patch import geos_to_path
import cartopy.crs as ccrs
from cartopy.io.shapereader import Reader
Warning: ecCodes 2.21.0 or higher is recommended. You are running version 2.14.1
In [2]:
# 加载ERA5数据集
ds = xr.open_dataset('/home/mw/input/ERA5_Lekima4742/ERA5_Lekima.nc')
time = ds.time
# 选择温度数据
temp = ds.t.sel(time=time[0], method='nearest')
levels = [500, 600, 700, 850, 1000]
data = temp.sel(level=levels)-273.15
lon = np.array(temp.longitude)
lat = np.array(temp.latitude)
In [29]:
# 绘制3D图像
#fig = plt.figure()
#ax = fig.add_subplot(111, projection='3d')
proj= ccrs.PlateCarree()
fig = plt.figure(figsize=(8,5),dpi=200)
ax = Axes3D(fig)
target_projection = ccrs.PlateCarree()
for i, level in enumerate(levels):
x, y = np.meshgrid(lon, lat)
z = np.full((x.shape[0], y.shape[1]), level)
t = data[i,:, :]
ax.plot_surface(x, y, z, rstride=1, cstride=1, alpha=0.5, cmap='coolwarm', linewidth=0.5)
cset = ax.contourf(x, y, t, levels=np.arange(-30, 30, 3), cmap=cmaps.ncl_default, offset=level, linewidth=2)
ax.clabel(cset, inline=1, fontsize=10, fmt='%1.0f')
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:6: MatplotlibDeprecationWarning: Axes3D(fig) adding itself to the figure is deprecated since 3.4. Pass the keyword argument auto_add_to_figure=False and use fig.add_axes(ax) to suppress this warning. The default value of auto_add_to_figure will change to False in mpl3.5 and True values will no longer work in 3.6. This is consistent with other Axes classes.
/opt/conda/lib/python3.7/site-packages/cmaps/cmaps.py:3181: UserWarning: Trying to register the cmap 'ncl_default' which already exists.
matplotlib.cm.register_cmap(name=cname, cmap=cmap)
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:15: UserWarning: The following kwargs were not used by contour: 'linewidth'
from ipykernel import kernelapp as app