I have read two files using open_mfdataset
. What I’m trying to do is create a vector plot for the wind stress data for which the i and j components are stored in two different files.
JavaScript
x
4
1
paths= [ "i stress.nc", "j stress.nc"]
2
DS=xr.open_mfdataset(paths)
3
DS
4
This is the data description
But while trying to do the vector plotting using quiver I’m getting this unhashable typeerror
.
JavaScript
1
9
1
ax= plt.axes(projection=ccrs.PlateCarree())
2
U=DS.SOZOTAUX.mean(dim='TIME')
3
V=DS.SOMETAUY.mean(dim='TIME')
4
x="LON86_125"
5
y="LAT71_110"
6
X,Y=np.meshgrid(x,y)
7
8
plt.quiver(X,Y,U,V)
9
JavaScript
1
31
31
1
TypeError Traceback (most recent call last)
2
c:UserssougaOneDriveDesktopPython programsProject.ipynb Cell 5 in <cell line: 8>()
3
5 y="LAT71_110"
4
6 X,Y=np.meshgrid(y,x)
5
----> 8 plt.quiver(X,Y,U,V)
6
7
File c:Userssougaanaconda3libsite-packagesmatplotlibpyplot.py:2788, in quiver(data, *args, **kwargs)
8
2786 @_copy_docstring_and_deprecators(Axes.quiver)
9
2787 def quiver(*args, data=None, **kwargs):
10
-> 2788 __ret = gca().quiver(
11
2789 *args, **({"data": data} if data is not None else {}),
12
2790 **kwargs)
13
2791 sci(__ret)
14
2792 return __ret
15
16
File c:Userssougaanaconda3libsite-packagescartopymplgeoaxes.py:310, in _add_transform.<locals>.wrapper(self, *args, **kwargs)
17
305 raise ValueError('Invalid transform: Spherical {} '
18
306 'is not supported - consider using '
19
307 'PlateCarree/RotatedPole.'.format(func.__name__))
20
309 kwargs['transform'] = transform
21
--> 310 return func(self, *args, **kwargs)
22
23
File c:Userssougaanaconda3libsite-packagescartopymplgeoaxes.py:1842, in GeoAxes.quiver(self, x, y, u, v, *args, **kwargs)
24
1840 x, y = np.meshgrid(x, y)
25
26
227 _api.check_isinstance((str, bytes), value=val)
27
228 if convertible:
28
229 # this will only be called so long as convertible is True.
29
30
TypeError: unhashable type: 'numpy.ndarray'
31
Advertisement
Answer
You’re using matplotlib.pyplot.quiver
, which expects arrays of numbers x, y, u, v
. When you call the following:
JavaScript
1
4
1
x="LON86_125"
2
y="LAT71_110"
3
X,Y=np.meshgrid(x,y)
4
X and Y are 2D string arrays:
JavaScript
1
6
1
In [3]: X
2
Out[3]: array([['LON86_125']], dtype='<U9')
3
4
In [4]: Y
5
Out[4]: array([['LAT71_110']], dtype='<U9')
6
Instead, I think you’re looking for something along the lines of
JavaScript
1
2
1
X, Y = np.meshgrid(DS['LON86_125'].values, DS['LAT71_110'].values)
2
That said, you might try xarray.Dataset.plot.quiver
which can work directly with xarray objects, and does accept string arguments referring to the dataset’s variable and coordinate names:
JavaScript
1
7
1
DS.mean(dim='TIME').plot.quiver(
2
x='LON86_125',
3
y='LAT71_110',
4
u='SOZOTAUX',
5
v='SOMETAUY',
6
)
7