如何使用cartopy或底图在python中使用netcdf数据绘制流线?

问题描述 投票:0回答:1

我想用 NOAA 从复合材料(2.5*2.5 经度/纬度)绘制流线。 我尝试用底图绘制它(附有代码),但我面临这个错误:

'y' must be strictly increasing.
之后我决定用cartopy(附代码)用
(crs=ccrs.PlateCarree(central_longitude=180)
来画。东经160°到西经160°范围内的流线未画出。 当然,有了这个风数据,我使用以下代码在底图中绘制了矢量风:
plt.quiver(lon[::5],lat[::5],U4[0,::5,::5],V4[0,::5,::5],scale_units='xy',scale=0.25)
但是我在绘制流线时收到错误。 如果有人可以帮助我解决这个问题(使用底图或cartopy),我将非常感激。 我的代码试图解决这个问题:

from netCDF4 import Dataset as NetCDFFile 
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.basemap import Basemap

fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111)
nc2 = NetCDFFile('E:/cycle-of-Mjo/NOAA/nc/u850b1.nc')
nc4 = NetCDFFile('E:/cycle-of-Mjo/NOAA/nc/v850b1.nc')
lat = nc2.variables['lat'][:]
lon = nc2.variables['lon'][:]
U4 = nc2.variables['uwnd'][:]
V4 = nc4.variables['vwnd'][:]
map = Basemap(projection='cyl', lon_0 =0 , lat_0 =-20 , 
llcrnrlon=0.,llcrnrlat=-20.,urcrnrlon=360.,urcrnrlat=61.,resolution='i' 
,suppress_ticks=False)
plt.tick_params(labelleft=False, labelbottom=False , axis='both',which='major')
map.drawcoastlines()
parallels = np.arange(-20.,61.,20.)
meridians = np.arange(0,360.,50.)
map.drawparallels(lat_ticks,labels=[0,0,0,0],fontsize=10 , dashes=(0,1), fontweight='bold')
map.drawmeridians(lon_ticks,labels=[0,0,0,0],fontsize=10 , dashes=(0,1), fontweight='bold')
lons,lats= np.meshgrid(lon,lat)
x,y = map(lons,lats)
map.streamplot(x,y,U1[0, :, :],V1[0, :, :],density = 2)
**Error:'y' must be strictly increasing**

与卡托比:

from netCDF4 import Dataset as NetCDFFile 
import numpy as np  
import cartopy.crs as ccrs 
import matplotlib.pyplot as plt
import cartopy.mpl.ticker as cticker
from cartopy.mpl.ticker import LatitudeFormatter, LongitudeFormatter
from cartopy.mpl.gridliner import LongitudeFormatter

nc2 = NetCDFFile('E:/cycle-of-Mjo/NOAA/nc/u850b1.nc')
nc4 = NetCDFFile('E:/cycle-of-Mjo/NOAA/nc/v850b1.nc')
lat = nc2.variables['lat'][:]
lon = nc2.variables['lon'][:]
U4 = nc2.variables['uwnd'][:]
V4 = nc4.variables['vwnd'][:]
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=180.0))
ax.coastlines()
ax.set_extent([0, 360, -20, 60], crs=ccrs.PlateCarree())  
ax.set_xticks(np.arange(0,360,50), crs=ccrs.PlateCarree())
strm = plt.streamplot(lon[::5], lat[::5], U1[0,::5,::5], V1[0,::5,::5], color='blue', 
density=1,arrowstyle='->', arrowsize=1, transform=ccrs.PlateCarree() )  
plt.show() 

enter image description here

python matplotlib matplotlib-basemap cartopy
1个回答
0
投票

输入的 NetCDF 网格数据可能具有从北到南的纬度值,因此按值递减排序。

但是

streamplot()
期望
x
y
数组按升序排序,如文档 here

中所述

均匀间隔严格递增阵列形成网格

所以你应该像这样对纬度数组进行排序:

lat = nc2.variables['lat'][:][::-1]

这将颠倒列表的顺序并将纬度值范围从南到北。

当然,您还必须相应地重新排列

U
V
数组,以便风值适合您的新网格。

© www.soinside.com 2019 - 2024. All rights reserved.