You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Trying to plot some data using xarray's DataArray structure, cartopy fails because it expects (implicitly) the input data to be a numpy array. There is a similar discussion/issue here, here, and a fixing here.
We could follow the Numpy solution and apply it to Cartopy, calling asanyarray on the input data.
Code to reproduce
import xarray as xr
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy
w = 3
Y, X = np.mgrid[-w:w:100j, -w:w:100j]
U = -1 - X**2 + Y
V = 1 + X - Y**2
speed = np.sqrt(U**2 + V**2)
lons = np.linspace(0, 5, np.shape(Y)[0])
lats = np.linspace(37, 42, np.shape(Y)[0])
xarray_field = xr.Dataset(
{
"u_velocity": (["latitude", "longitude"], U),
"v_velocity": (["latitude", "longitude"], V),
"speed": (["latitude", "longitude"], speed)
},
coords={
"longitude": lons,
"latitude": lats
})
fig, ax = plt.subplots(nrows=1, ncols=1,
subplot_kw={'projection': ccrs.PlateCarree()})
xarray_field.speed.plot(ax=ax)
sp = ax.streamplot(xarray_field["longitude"], xarray_field["latitude"], xarray_field["u_velocity"], xarray_field["v_velocity"], color="w")
plt.show()
Traceback
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Input In [5], in <cell line: 4>()
1 fig, ax = plt.subplots(nrows=1, ncols=1,
2 subplot_kw={'projection': ccrs.PlateCarree()})
3 xarray_field.speed.plot(ax=ax)
----> 4 sp = ax.streamplot(xarray_field["longitude"], xarray_field["latitude"], xarray_field["u_velocity"], xarray_field["v_velocity"], color="w")
6 plt.show()
File ~/miniconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py:318, in _add_transform.<locals>.wrapper(self, *args, **kwargs)
313 raise ValueError(f'Invalid transform: Spherical {func.__name__} '
314 'is not supported - consider using '
315 'PlateCarree/RotatedPole.')
317 kwargs['transform'] = transform
--> 318 return func(self, *args, **kwargs)
File ~/miniconda3/lib/python3.8/site-packages/cartopy/mpl/geoaxes.py:2240, in GeoAxes.streamplot(self, x, y, u, v, **kwargs)
2237 # Lazy load vector_scalar_to_grid due to the optional
2238 # scipy dependency
2239 from cartopy.vector_transform import vector_scalar_to_grid
-> 2240 gridded = vector_scalar_to_grid(t, self.projection, regrid_shape,
2241 x, y, u, v, *scalars,
2242 target_extent=target_extent)
2243 x, y, u, v = gridded[:4]
2244 # If scalar fields were regridded then replace the appropriate keyword
2245 # arguments with the gridded arrays.
File ~/miniconda3/lib/python3.8/site-packages/cartopy/vector_transform.py:130, in vector_scalar_to_grid(src_crs, target_proj, regrid_shape, x, y, u, v, *scalars, **kwargs)
127 nx = ny = regrid_shape
128 if target_proj == src_crs:
129 # Just immediately regrid, interpolate and return
--> 130 return _interpolate_to_grid(nx, ny, x, y, u, v, *scalars, **kwargs)
132 # We need to transform the vectors from the source to target frame
133 # Convert coordinates to the target projection.
134 proj_xyz = target_proj.transform_points(src_crs, x, y)
File ~/miniconda3/lib/python3.8/site-packages/cartopy/vector_transform.py:57, in _interpolate_to_grid(nx, ny, x, y, *scalars, **kwargs)
55 s_grid_tuple = tuple()
56 for s in scalars:
---> 57 s_grid_tuple += (griddata(points, s.ravel(), (x_grid, y_grid),
58 method='linear'),)
59 return (x_grid * xr + x0, y_grid * yr + y0) + s_grid_tuple
File ~/miniconda3/lib/python3.8/site-packages/xarray/core/common.py:239, in AttrAccessMixin.__getattr__(self, name)
237 with suppress(KeyError):
238 return source[name]
--> 239 raise AttributeError(
240 f"{type(self).__name__!r} object has no attribute {name!r}"
241 )
AttributeError: 'DataArray' object has no attribute 'ravel'
Full environment definition
Operating system
Linux mini 5.4.0-110-generic #124-Ubuntu SMP Thu Apr 14 19:46:19 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Sure, I just create the PR #2050
I could create a test, but for that, I would need to import xarray, which would increase the library dependency.
If you think the new test is still necessary, I will provide it.
Description
Hi,
Trying to plot some data using xarray's DataArray structure, cartopy fails because it expects (implicitly) the input data to be a numpy array. There is a similar discussion/issue here, here, and a fixing here.
We could follow the Numpy solution and apply it to Cartopy, calling asanyarray on the input data.
Code to reproduce
Traceback
Full environment definition
Operating system
Linux mini 5.4.0-110-generic #124-Ubuntu SMP Thu Apr 14 19:46:19 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Cartopy version
0.20.2
conda list
The text was updated successfully, but these errors were encountered: