如何在具有中央经度180的CATTOPY PLATECARREE投影上转变风量矢量

发布于 2025-02-01 16:18:16 字数 3758 浏览 5 评论 0原文

我是Python的新手。我使用Windpharm Python软件包计算了NetCDF4文件格式“ U”风和“ V”风的发散风部分,然后我想使用“ Quiver'命令绘制发散的风向矢量,并在Cartopy PlateCarreey投影上显示了矢量。

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()

但是,当我试图在Cartopy投影platecroveion上转换具有Central_Longitudity = 180的Cartopy Protofution Platefotion时,

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- 在以下答案之后,我绘制了发散的风矢量 不同的风向量向量图

我正在变得非常密集的向量,这很难分析。 可以使用Quiver调整向量密度吗?

I'm new to python. I calculated the divergent wind part from netCDF4 file format 'u' wind and 'v' wind using windspharm python package and then I wanted to plot the divergent wind vector using 'quiver' command, and it is showing the vector on cartopy PlateCarree projection .

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()

But when I'm trying to transform this divergent wind vector on cartopy projection PlateCarree having central_longitude=180

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()

Now it is showing the error as-

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. 

Please help me in plotting the wind vector on PlateCarree projection having central longitude 180 degrees.

EDIT_1-
uchi1 and vchi1 are xarray data arrays

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-
Following the below answer, I plotted the divergent wind vector
Divergent wind vector plot

I'm getting very dense vector, which is difficult to analyze.
Can density of vector be adjusted using quiver ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺孤伤于心 2025-02-08 16:18:16

您确定是由于投影而造成的吗?例如,第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)

Are you sure it's due to the projection? What the vp_fill on line 8 for example?

Without your data it's impossible to replicate your example. But when I use some synthetic data as shown below it seems to work as expected for me, regardless the central_longitude of the projection.

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)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文