title: Python绘制气象风场
date: 2021-08-05 21:01:52
tag:

气象风场数据来源为欧洲中心再分析数据ERA5 hourly data on pressure levels from 1979 to present (copernicus.eu)

#在地图上绘制风场和轨迹
import matplotlib.pyplot as plt###引入库包
import numpy as np
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.ticker as mticker
import netCDF4 as nc
import matplotlib.pyplot as plt

mpl.rcParams["font.family"] = 'Arial'  #默认字体类型
mpl.rcParams["mathtext.fontset"] = 'cm' #数学文字字体
mpl.rcParams["font.size"] = 15   #字体大小
mpl.rcParams["axes.linewidth"] = 1   #轴线边框粗细(默认的太粗了)

f1=nc.Dataset('wind.nc') #打开.nc文件
#读取文件中的数据
lat=f1.variables['latitude'][:]
lon=f1.variables['longitude'][:]
time=f1.variables['time'][:]
u1000=f1.variables['u'][:]#下载的1000hPa的风场数据
v1000=f1.variables['v'][:]

lon2d, lat2d = np.meshgrid(lon, lat)
u1000_aim=np.mean(u1000[4:6,:,:],axis=0)
v1000_aim=np.mean(v1000[4:6,:,:],axis=0)

proj = ccrs.PlateCarree(central_longitude=180)  
fig = plt.figure(figsize=(10,8),dpi=550)  # 创建画布
ax = fig.subplots(1, 1, subplot_kw={'projection': proj})  # 创建子图


u_all=u1000_aim
v_all=v1000_aim

#-----------绘制地图-------------------------------------------

# ax.add_feature(cfeature.LAND.with_scale('50m'))####添加陆地######
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))#####添加海岸线#########
# ax.add_feature(cfeature.OCEAN.with_scale('50m'))######添加海洋########

#-----------添加经纬度---------------------------------------
extent=[100,269,-80,80]##经纬度范围
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=0., color='k', alpha=0.5, linestyle='--')
dlon, dlat = 45, 30   #设置步长
xticks = np.arange(0, 360.1, dlon)  #设置绘图范围
yticks = np.arange(-90, 90.1, dlat)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())  #图幅设置坐标轴刻度
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))  #设置坐标轴刻度标签格式
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.set_extent(extent)     #显示所选择的区域

#----------修改国界,并添加省界-----------------------------
#在这个网站上可以找到dat文件,https://gmt-china.org/data/
# with open('C:/Users/hj/.local/share/cartopy/shapefiles/natural_earth/physical/CN-border-La.dat') as src:
#     context = src.read()
#     blocks = [cnt for cnt in context.split('>') if len(cnt) > 0]
#     borders = [np.fromstring(block, dtype=float, sep=' ') for block in blocks]
# for line in borders:
#     ax.plot(line[0::2], line[1::2], '-', color='k',lw=0.3, transform=ccrs.Geodetic())

#-------------------plot---------------------------
#levels = np.arange(1004, 1032 + 1, 1)
#cb = ax.contourf(lon2d,lat2d,msl_all, levels=levels, cmap='Spectral_r',transform=ccrs.PlateCarree())

cq =ax.quiver(lon2d[::20,::20],lat2d[::20,::20],u_all[::20,::20],v_all[::20,::20],color='k',scale=250,zorder=10,width=0.002,headwidth=3,headlength=4.5,transform=ccrs.PlateCarree())   

plt.savefig('pic.jpg',dpi=300)

画出的图如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gU4EB1Zt-1629730874406)(https://z3.ax1x.com/2021/08/05/feDIN8.jpg)]

下面画风速的颜色图:

#在地图上绘制风场和轨迹
import matplotlib.pyplot as plt###引入库包
import numpy as np
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.ticker as mticker
import netCDF4 as nc
import matplotlib.pyplot as plt

mpl.rcParams["font.family"] = 'Arial'  #默认字体类型
mpl.rcParams["mathtext.fontset"] = 'cm' #数学文字字体
mpl.rcParams["font.size"] = 15   #字体大小
mpl.rcParams["axes.linewidth"] = 1   #轴线边框粗细(默认的太粗了)

f1=nc.Dataset('wind.nc') #打开.nc文件
#读取文件中的数据
lat=f1.variables['latitude'][:]
lon=f1.variables['longitude'][:]
time=f1.variables['time'][:]
u1000=f1.variables['u'][:]#下载的1000hPa的风场数据
v1000=f1.variables['v'][:]

lon2d, lat2d = np.meshgrid(lon, lat)
u1000_aim=np.mean(u1000[4:6,:,:],axis=0)
v1000_aim=np.mean(v1000[4:6,:,:],axis=0)

proj = ccrs.PlateCarree(central_longitude=180)  
fig = plt.figure(figsize=(10,8),dpi=550)  # 创建画布
ax = fig.subplots(1, 1, subplot_kw={'projection': proj})  # 创建子图

u_all=u1000_aim
v_all=v1000_aim
ws=np.sqrt(u_all*u_all+v_all*v_all)

#-----------绘制地图-------------------------------------------

# ax.add_feature(cfeature.LAND.with_scale('50m'))####添加陆地######
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))#####添加海岸线#########
# ax.add_feature(cfeature.OCEAN.with_scale('50m'))######添加海洋########

#-----------添加经纬度---------------------------------------
extent=[100,269,-80,80]##经纬度范围
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=0., color='k', alpha=0.5, linestyle='--')
dlon, dlat = 45, 30   #设置步长
xticks = np.arange(0, 360.1, dlon)  #设置绘图范围
yticks = np.arange(-90, 90.1, dlat)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())  #图幅设置坐标轴刻度
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))  #设置坐标轴刻度标签格式
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.set_extent(extent)     #显示所选择的区域


#cb = ax.contourf(lon2d,lat2d,ws, levels=levels, cmap='Spectral_r',transform=ccrs.PlateCarree())
cb = ax.pcolormesh(lon2d,lat2d,ws, cmap='Spectral_r',transform=ccrs.PlateCarree())
cbar=fig.colorbar(cb,ax=(ax),orientation='vertical',ticks=np.arange(0, 24 + 4,4),
                  aspect=20,shrink=0.9,pad=0.02)
#pad调和图的距离
#aspect调颜色棒的宽度
#shrink调颜色棒的长度
cbar.ax.tick_params(pad=0.02,length=2,width=0.5)

plt.savefig('pic3.jpg',dpi=300)

画出的图如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TlKE7PMK-1629730874411)(https://z3.ax1x.com/2021/08/05/ferTVx.jpg)]

画航线轨迹和异常点

#在地图上绘制风场和轨迹
import matplotlib.pyplot as plt###引入库包
import numpy as np
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.ticker as mticker
import netCDF4 as nc
import matplotlib.pyplot as plt

mpl.rcParams["font.family"] = 'Arial'  #默认字体类型
mpl.rcParams["mathtext.fontset"] = 'cm' #数学文字字体
mpl.rcParams["font.size"] = 15   #字体大小
mpl.rcParams["axes.linewidth"] = 1   #轴线边框粗细(默认的太粗了)

import pandas as pd
traj=pd.read_csv('Antarctic28.csv')
traj_lon=traj.iloc[:,3]
traj_lat=traj.iloc[:,2]

aim_lat=[0.752, -12.006, -62.226, -62.233, -64.400, -69.332]
aim_lon=[119.1357833,115.1921333,-58.92556667,-58.91463333,-16.3727,76.43843333]


proj = ccrs.PlateCarree(central_longitude=45)  #中国为左
fig = plt.figure(figsize=(10,8),dpi=550)  # 创建画布
ax = fig.subplots(1, 1, subplot_kw={'projection': proj})  # 创建子图

#-----------绘制地图-------------------------------------------

# ax.add_feature(cfeature.LAND.with_scale('50m'))####添加陆地######
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))#####添加海岸线#########
# ax.add_feature(cfeature.OCEAN.with_scale('50m'))######添加海洋########

#-----------添加经纬度---------------------------------------
extent=[-90,135,-80,80]##经纬度范围
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=0., color='k', alpha=0.5, linestyle='--')
dlon, dlat = 45, 30   #设置步长
xticks = np.arange(0, 360.1, dlon)  #设置绘图范围
yticks = np.arange(-90, 90.1, dlat)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())  #图幅设置坐标轴刻度
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))  #设置坐标轴刻度标签格式
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.set_extent(extent)     #显示所选择的区域


#cb = ax.contourf(lon2d,lat2d,ws, levels=levels, cmap='Spectral_r',transform=ccrs.PlateCarree())
# cb = ax.pcolormesh(lon2d,lat2d,ws, cmap='Spectral_r',transform=ccrs.PlateCarree(),alpha=0.8)
# cbar=fig.colorbar(cb,ax=(ax),orientation='vertical',ticks=np.arange(0, 24 + 4,4),
#                   aspect=10,shrink=0.5,pad=0.02)
# #pad调和图的距离
# #aspect调颜色棒的宽度
# #shrink调颜色棒的长度
# cbar.ax.tick_params(pad=0.02,length=2,width=0.5)
ax.plot(traj_lon[0:3525],traj_lat[0:3525],transform=ccrs.PlateCarree(),color='red')
ax.plot(traj_lon[3525:],traj_lat[3525:],transform=ccrs.PlateCarree(),color='b')

ax.scatter(aim_lon[0:4],aim_lat[0:4],transform=ccrs.PlateCarree(),s=300,color='gray',alpha=0.6)
ax.scatter(aim_lon[4:],aim_lat[4:],transform=ccrs.PlateCarree(),s=300,color='gray',alpha=0.6)

ax.scatter(aim_lon[0:4],aim_lat[0:4],transform=ccrs.PlateCarree(),s=50,color='red')
ax.scatter(aim_lon[4:],aim_lat[4:],transform=ccrs.PlateCarree(),s=50,color='b')
plt.savefig('pic4.jpg',dpi=300)

画出的图如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d4jLH1Dz-1629730874414)(https://z3.ax1x.com/2021/08/05/fes6OA.jpg)]

画出异常点附近的风场

#在地图上绘制风场和轨迹
import matplotlib.pyplot as plt###引入库包
import numpy as np
import matplotlib as mpl
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.ticker as mticker
import netCDF4 as nc
import matplotlib.pyplot as plt

mpl.rcParams["font.family"] = 'Arial'  #默认字体类型
mpl.rcParams["mathtext.fontset"] = 'cm' #数学文字字体
mpl.rcParams["font.size"] = 15   #字体大小
mpl.rcParams["axes.linewidth"] = 1   #轴线边框粗细(默认的太粗了)

f1=nc.Dataset('wind.nc') #打开.nc文件
#读取文件中的数据
lat=f1.variables['latitude'][:]
lon=f1.variables['longitude'][:]
time=f1.variables['time'][:]
u1000=f1.variables['u'][:]#下载的1000hPa的风场数据
v1000=f1.variables['v'][:]

lon2d, lat2d = np.meshgrid(lon, lat)
u1000_aim=np.mean(u1000[4:6,:,:],axis=0)
v1000_aim=np.mean(v1000[4:6,:,:],axis=0)

import pandas as pd
traj=pd.read_csv('Antarctic28.csv')
traj_lon=traj.iloc[:,3]
traj_lat=traj.iloc[:,2]

aim_lat=[0.752, -12.006, -62.226, -62.233, -64.400, -69.332]
aim_lon=[119.1357833,115.1921333,-58.92556667,-58.91463333,-16.3727,76.43843333]


mpl.rcParams["font.size"] = 18   #字体大小
proj = ccrs.PlateCarree(central_longitude=115)  #中国为左
fig = plt.figure(figsize=(10,8),dpi=550)  # 创建画布
ax = fig.subplots(1, 1, subplot_kw={'projection': proj})  # 创建子图

u_all=u1000_aim
v_all=v1000_aim
ws=np.sqrt(u_all*u_all+v_all*v_all)

#-----------绘制地图-------------------------------------------

# ax.add_feature(cfeature.LAND.with_scale('50m'))####添加陆地######
ax.add_feature(cfeature.COASTLINE.with_scale('50m'))#####添加海岸线#########
# ax.add_feature(cfeature.OCEAN.with_scale('50m'))######添加海洋########

#-----------添加经纬度---------------------------------------
extent=[118,120.5,-0.5,2]##经纬度范围
gl = ax.gridlines(crs=ccrs.PlateCarree(), draw_labels=False, linewidth=0., color='k', alpha=0.5, linestyle='--')
dlon, dlat = 1, 0.5   #设置步长
xticks = np.arange(117, 121.1, dlon)  #设置绘图范围
yticks = np.arange(-1, 2.1, dlat)
ax.set_xticks(xticks, crs=ccrs.PlateCarree())  #图幅设置坐标轴刻度
ax.set_yticks(yticks, crs=ccrs.PlateCarree())
ax.xaxis.set_major_formatter(LongitudeFormatter(zero_direction_label=True))  #设置坐标轴刻度标签格式
ax.yaxis.set_major_formatter(LatitudeFormatter())
ax.set_extent(extent)     #显示所选择的区域

# levels = np.arange(0, 4 + 1, 1)
#cb = ax.contourf(lon2d,lat2d,ws, levels=levels, cmap='Spectral_r',transform=ccrs.PlateCarree())
cb = ax.pcolormesh(lon2d,lat2d,ws, cmap='Spectral_r',transform=ccrs.PlateCarree(),alpha=0.8,vmin=0.0,vmax=4)
cbar=fig.colorbar(cb,ax=(ax),orientation='vertical',ticks=np.arange(0, 4 + 1,1),
                  aspect=10,shrink=0.5,pad=0.02)
#pad调和图的距离
#aspect调颜色棒的宽度
#shrink调颜色棒的长度
cbar.ax.tick_params(pad=0.02,length=2,width=0.5)


ax.plot(traj_lon[0:3525],traj_lat[0:3525],transform=ccrs.PlateCarree(),color='red')
ax.plot(traj_lon[3525:],traj_lat[3525:],transform=ccrs.PlateCarree(),color='b')

ax.scatter(aim_lon[0:4],aim_lat[0:4],transform=ccrs.PlateCarree(),s=300,color='black',alpha=0.6)
ax.scatter(aim_lon[4:],aim_lat[4:],transform=ccrs.PlateCarree(),s=300,color='black',alpha=0.6)

ax.scatter(aim_lon[0:4],aim_lat[0:4],transform=ccrs.PlateCarree(),s=50,color='red')
ax.scatter(aim_lon[4:],aim_lat[4:],transform=ccrs.PlateCarree(),s=50,color='b')

cq = ax.quiver(lon2d[::1,::1],lat2d[::1,::1],u_all[::1,::1],v_all[::1,::1],color='k',
               scale=20,zorder=10,width=0.002,headwidth=3,headlength=4.5,transform=ccrs.PlateCarree())

plt.savefig('pic5.jpg',dpi=300)

画出的图如下:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pQQVy9nE-1629730874416)(https://z3.ax1x.com/2021/08/05/feyp6J.jpg)]

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐