如何在中心经度为 180 的 Cartopy PlateCarree 投影上转换风矢量
How to transform wind vector on Cartopy PlateCarree projection having central longitude 180
我是 python 的新手。我使用 windspharm python 包从 netCDF4 文件格式 'u' wind 和 'v' wind 计算了发散风部分,然后我想使用 'quiver' 命令绘制发散风矢量,并且它在 cartopy PlateCarree 投影上显示矢量。
ax2 = plt.axes(projection=ccrs.PlateCarree())
q2=ax2.quiver(lons, lats, uchi1, vchi1, width=0.0005,scale_units='xy', scale=0.07, transform=ccrs.PlateCarree())
qk2=plt.quiverkey (q2,0.96, 1.02, 0.5, '0.5 m/s')
plt.title('Divergent wind', fontsize=16)
plt.show()
但是当我试图在具有 central_longitude=180
的 Cartopy 投影 PlateCarree 上转换这个发散的风向量时
ax2 = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
q2=ax2.quiver(lons, lats, uchi1, vchi1, width=0.0005,scale_units='xy', scale=0.07, transform=ccrs.PlateCarree())
qk2=plt.quiverkey (q2,0.96, 1.02, 0.5, '0.5 m/s')
ax2.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree())
ax2.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f')
lat_formatter = LatitudeFormatter()
ax2.xaxis.set_major_formatter(lon_formatter)
ax2.yaxis.set_major_formatter(lat_formatter)
plt.title('Divergent wind', fontsize=16)
plt.show()
现在显示错误为-
6 q2=ax2.quiver(lons, lats, uchi1, vchi1,width=0.0005, scale_units='xy',scale=0.07,transform=ccrs.PlateCarree())
7 qk2=plt.quiverkey (q2,0.96, 1.02, 0.5, '0.5 m/s')
8 plt.colorbar(vp_fill, orientation='horizontal')
File ~/anaconda3/lib/python3.9/site-packages/cartopy/mpl/geoaxes.py:310, in _add_transform.<locals>.wrapper(self, *args, **kwargs)
305 raise ValueError('Invalid transform: Spherical {} '
306 'is not supported - consider using '
307 'PlateCarree/RotatedPole.'.format(func.__name__))
309 kwargs['transform'] = transform
--> 310 return func(self, *args, **kwargs)
File ~/anaconda3/lib/python3.9/site-packages/xarray/core/variable.py:674, in
Variable._validate_indexers(self, key)
669 raise IndexError(
670 "Boolean array size {:d} is used to index array "
671 "with shape {:s}.".format(len(k), str(self.shape))
672 )
673 if k.ndim > 1:
--> 674 raise IndexError(
675 "{}-dimensional boolean indexing is "
676 "not supported. ".format(k.ndim)
677 )
678 if getattr(k, "dims", (dim,)) != (dim,):
679 raise IndexError(
680 "Boolean indexer should be unlabeled or on the "
681 "same dimension to the indexed array. Indexer is "
(...)
684 )
685 )
IndexError: 2-dimensional boolean indexing is not supported.
请帮助我在中心经度 180 度的 PlateCarree 投影上绘制风矢量。
EDIT_1-
uchi1 和 vchi1 是 xarray 数据数组
uchi1
xarray.DataArray'u_chi'lat: 73lon: 144
array([[0.12443417, 0.12168238, 0.11869895, ..., 0.13124993,
0.12922251,
0.12694916],
[0.13728575, 0.13166314, 0.12577812, ..., 0.15224756,
0.14761028,
0.14261246],
[0.14412266, 0.1364844 , 0.12858798, ..., 0.16488427,
0.15838091,
0.15144138],
...,
[0.4486847 , 0.4489504 , 0.44671202, ..., 0.43283802,
0.44058776,
0.44589037],
[0.46339756, 0.4668066 , 0.46879947, ..., 0.44473046,
0.45234278,
0.45857257],
[0.42911786, 0.4292356 , 0.42853624, ..., 0.4238725 , 0.4264338 ,
0.42818335]], dtype=float32)
Coordinates:
lat
(lat)
float32
90.0 87.5 85.0 ... -87.5 -90.0
lon
(lon)
float32
0.0 2.5 5.0 ... 352.5 355.0 357.5
Attributes:
units :
m s**-1
long_name :
irrotational_eastward_wind
EDIT_2-
按照下面的答案,我绘制了发散风矢量
Divergent wind vector plot
我得到非常密集的向量,很难分析。
可以使用 quiver
调整矢量密度吗?
你确定是投影造成的?例如第 8 行的 vp_fill
是什么?
没有您的数据就无法复制您的示例。但是,当我使用如下所示的一些合成数据时,无论投影的 central_longitude
是什么,它似乎都按预期工作。
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import numpy as np
shp = (16, 32)
lons = np.random.randint(-179, 179, shp)
lats = np.random.randint(-89, 89, shp)
u = np.random.randn(*shp)
v = np.random.randn(*shp)
proj = ccrs.PlateCarree(central_longitude=180)
fig, ax = plt.subplots(figsize=(10,5), subplot_kw=dict(projection=proj), constrained_layout=True)
ax.set_title('Divergent wind', fontsize=16)
q = ax.quiver(lons, lats, u, v, transform=ccrs.PlateCarree(), color="g", width=0.002, scale_units='xy', scale=0.3)
ax.coastlines('110m', alpha=0.5)
qk = ax.quiverkey (q, 0.96, 1.02, 3.0, '3.0 m/s')
ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f')
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)
我是 python 的新手。我使用 windspharm python 包从 netCDF4 文件格式 'u' wind 和 'v' wind 计算了发散风部分,然后我想使用 'quiver' 命令绘制发散风矢量,并且它在 cartopy PlateCarree 投影上显示矢量。
ax2 = plt.axes(projection=ccrs.PlateCarree())
q2=ax2.quiver(lons, lats, uchi1, vchi1, width=0.0005,scale_units='xy', scale=0.07, transform=ccrs.PlateCarree())
qk2=plt.quiverkey (q2,0.96, 1.02, 0.5, '0.5 m/s')
plt.title('Divergent wind', fontsize=16)
plt.show()
但是当我试图在具有 central_longitude=180
的 Cartopy 投影 PlateCarree 上转换这个发散的风向量时ax2 = plt.axes(projection=ccrs.PlateCarree(central_longitude=180))
q2=ax2.quiver(lons, lats, uchi1, vchi1, width=0.0005,scale_units='xy', scale=0.07, transform=ccrs.PlateCarree())
qk2=plt.quiverkey (q2,0.96, 1.02, 0.5, '0.5 m/s')
ax2.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree())
ax2.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f')
lat_formatter = LatitudeFormatter()
ax2.xaxis.set_major_formatter(lon_formatter)
ax2.yaxis.set_major_formatter(lat_formatter)
plt.title('Divergent wind', fontsize=16)
plt.show()
现在显示错误为-
6 q2=ax2.quiver(lons, lats, uchi1, vchi1,width=0.0005, scale_units='xy',scale=0.07,transform=ccrs.PlateCarree())
7 qk2=plt.quiverkey (q2,0.96, 1.02, 0.5, '0.5 m/s')
8 plt.colorbar(vp_fill, orientation='horizontal')
File ~/anaconda3/lib/python3.9/site-packages/cartopy/mpl/geoaxes.py:310, in _add_transform.<locals>.wrapper(self, *args, **kwargs)
305 raise ValueError('Invalid transform: Spherical {} '
306 'is not supported - consider using '
307 'PlateCarree/RotatedPole.'.format(func.__name__))
309 kwargs['transform'] = transform
--> 310 return func(self, *args, **kwargs)
File ~/anaconda3/lib/python3.9/site-packages/xarray/core/variable.py:674, in
Variable._validate_indexers(self, key)
669 raise IndexError(
670 "Boolean array size {:d} is used to index array "
671 "with shape {:s}.".format(len(k), str(self.shape))
672 )
673 if k.ndim > 1:
--> 674 raise IndexError(
675 "{}-dimensional boolean indexing is "
676 "not supported. ".format(k.ndim)
677 )
678 if getattr(k, "dims", (dim,)) != (dim,):
679 raise IndexError(
680 "Boolean indexer should be unlabeled or on the "
681 "same dimension to the indexed array. Indexer is "
(...)
684 )
685 )
IndexError: 2-dimensional boolean indexing is not supported.
请帮助我在中心经度 180 度的 PlateCarree 投影上绘制风矢量。
EDIT_1-
uchi1 和 vchi1 是 xarray 数据数组
uchi1
xarray.DataArray'u_chi'lat: 73lon: 144
array([[0.12443417, 0.12168238, 0.11869895, ..., 0.13124993,
0.12922251,
0.12694916],
[0.13728575, 0.13166314, 0.12577812, ..., 0.15224756,
0.14761028,
0.14261246],
[0.14412266, 0.1364844 , 0.12858798, ..., 0.16488427,
0.15838091,
0.15144138],
...,
[0.4486847 , 0.4489504 , 0.44671202, ..., 0.43283802,
0.44058776,
0.44589037],
[0.46339756, 0.4668066 , 0.46879947, ..., 0.44473046,
0.45234278,
0.45857257],
[0.42911786, 0.4292356 , 0.42853624, ..., 0.4238725 , 0.4264338 ,
0.42818335]], dtype=float32)
Coordinates:
lat
(lat)
float32
90.0 87.5 85.0 ... -87.5 -90.0
lon
(lon)
float32
0.0 2.5 5.0 ... 352.5 355.0 357.5
Attributes:
units :
m s**-1
long_name :
irrotational_eastward_wind
EDIT_2- 按照下面的答案,我绘制了发散风矢量 Divergent wind vector plot
我得到非常密集的向量,很难分析。
可以使用 quiver
调整矢量密度吗?
你确定是投影造成的?例如第 8 行的 vp_fill
是什么?
没有您的数据就无法复制您的示例。但是,当我使用如下所示的一些合成数据时,无论投影的 central_longitude
是什么,它似乎都按预期工作。
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import numpy as np
shp = (16, 32)
lons = np.random.randint(-179, 179, shp)
lats = np.random.randint(-89, 89, shp)
u = np.random.randn(*shp)
v = np.random.randn(*shp)
proj = ccrs.PlateCarree(central_longitude=180)
fig, ax = plt.subplots(figsize=(10,5), subplot_kw=dict(projection=proj), constrained_layout=True)
ax.set_title('Divergent wind', fontsize=16)
q = ax.quiver(lons, lats, u, v, transform=ccrs.PlateCarree(), color="g", width=0.002, scale_units='xy', scale=0.3)
ax.coastlines('110m', alpha=0.5)
qk = ax.quiverkey (q, 0.96, 1.02, 3.0, '3.0 m/s')
ax.set_xticks([0, 60, 120, 180, 240, 300, 359.99], crs=ccrs.PlateCarree())
ax.set_yticks([-90, -60, -30, 0, 30, 60, 90], crs=ccrs.PlateCarree())
lon_formatter = LongitudeFormatter(zero_direction_label=True, number_format='.0f')
lat_formatter = LatitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)
ax.yaxis.set_major_formatter(lat_formatter)