Issue
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
?
Solution
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)
Answered By - Rutger Kassies
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.