vtex-apps / login

Login docs and messages
173 stars 382 forks source link

wind vectors using python #12

Open haritha1022 opened 2 years ago

haritha1022 commented 2 years ago

when i used to plot wind vectors i was getting type error here is my code

import xarray as xr import cartopy.crs as ccrs from cartopy import feature as cf import matplotlib.pyplot as plt import numpy as np data=xr.open_dataset('guv.nc') print(data) latitude=data.variables['latitude'][:]

longitude=data.variables['longitude'][:]

time=data.variables['time'][:] u=data.variables['u'][:] v=data.variables['v'][:] x,y= np.meshgrid(longitude,latitude)

east = 100 west = 60 north = 40 south = 0 u=data.u[0,::20,::20] v=data.v[0,::20,::20]

ax = plt.axes(projection=ccrs.PlateCarree()) winds

ax.set_extent([west, east, south, north]) ax.coastlines() ax.add_feature(cf.BORDERS)

ax.barbs(longitude,latitude,u,v) plt.figure() plt.show()

raise ValueError('X and Y must be the same size, but ' ValueError: X and Y must be the same size, but X.size is 321 and Y.size is 201.

if didn't give any interval the figure is too bad i have attached please find below

my data contains Dimensions: (longitude: 321, latitude: 201, time: 16) Coordinates:

haritha1022 commented 2 years ago

the data i have taken from ERA5 0.25degree resolution

charzlwebz256 commented 1 year ago

The error message you received indicates that the X and Y arrays you passed to the barbs function have different sizes. Specifically, it says that X.size is 321 and Y.size is 201, which means that the longitude array (X) has 321 elements and the latitude array (Y) has 201 elements.

To fix this error, you should pass the x and y arrays you defined using np.meshgrid to the barbs function instead of the longitude and latitude arrays from the dataset. Here's the updated code: `import xarray as xr import cartopy.crs as ccrs from cartopy import feature as cf import matplotlib.pyplot as plt import numpy as np

data = xr.open_dataset('guv.nc') u = data.variables['u'][0, ::20, ::20] v = data.variables['v'][0, ::20, ::20] lon = data.variables['longitude'][:] lat = data.variables['latitude'][:] x, y = np.meshgrid(lon, lat)

east = 100 west = 60 north = 40 south = 0

ax = plt.axes(projection=ccrs.PlateCarree()) ax.set_extent([west, east, south, north]) ax.coastlines() ax.add_feature(cf.BORDERS)

ax.barbs(x, y, u, v)

plt.show() ` This should correctly plot the wind vectors using the barbs function. Note that I also removed the unused time variable and the winds line, which appeared to be undefined. Additionally, you may need to adjust the aspect ratio of the figure to make it look better. You can do this by adding the following line before calling plt.show():

plt.gca().set_aspect('equal', adjustable='box')