Skip to content

Instantly share code, notes, and snippets.

@cpelley
Created August 27, 2013 08:40
Show Gist options
  • Save cpelley/6351152 to your computer and use it in GitHub Desktop.
Save cpelley/6351152 to your computer and use it in GitHub Desktop.
Plotting wind vectors (wind speed and direction)
import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import iris
from iris.coord_systems import GeogCS
from iris.cube import Cube
def create_random_wind_data(size):
# Create some random wind data
def random_dat(size):
return (np.random.random_sample(size)*2-1)*10
# Coord system
cs = GeogCS(6371229)
# Coordinates
lat_coord = iris.coords.DimCoord(
points=np.linspace(45, 60, size[0], endpoint=True),
standard_name='latitude',
units='degrees',
coord_system=cs)
lon_coord = iris.coords.DimCoord(
points=np.linspace(-10, 5, size[1], endpoint=True),
standard_name='longitude',
units='degrees',
coord_system=cs)
# U-wind cube
u_cube = Cube(random_dat(size),
standard_name='eastward_wind',
units='m s-1')
u_cube.add_dim_coord(lat_coord, 0)
u_cube.add_dim_coord(lon_coord, 1)
# V-wind cube
v_cube = Cube(random_dat(size),
standard_name='northward_wind',
units='m s-1')
v_cube.add_dim_coord(lat_coord, 0)
v_cube.add_dim_coord(lon_coord, 1)
return (u_cube, v_cube)
if __name__ == '__main__':
u_cube, v_cube = create_random_wind_data(size=(25, 25))
# Quiver plot
U = u_cube.data
V = v_cube.data
X = u_cube.coord('longitude').points
Y = v_cube.coord('latitude').points
ax = plt.axes(projection=ccrs.PlateCarree())
arrows = plt.quiver(X, Y, U, V, units='xy', headwidth=2,
transform=ccrs.Geodetic())
ax.coastlines()
ax.gridlines(draw_labels=True)
ax.stock_img()
ax.set_xlim(-10, 5)
ax.set_ylim(45, 60)
plt.show()
@jonnyhtw
Copy link

jonnyhtw commented Feb 1, 2018

Thanks for sharing this!

When I run this I get the following error. Any idea why?! When I comment out the transform=ccrs.Geodetic() bit, it works fine. Thanks!...


ValueError Traceback (most recent call last)
in ()
----> 1 execfile('global_4deg.py')

/scale_akl_persistent/filesets/home/williamsjh/veros-analysis/global_4deg.py in ()
80 ax = plt.axes(projection=ccrs.PlateCarree())
81 arrows = plt.quiver(X, Y, U, V, units='xy', headwidth=2,
---> 82 transform=ccrs.Geodetic())
83 ax.coastlines()
84 ax.gridlines(draw_labels=True)

/home/williamsjh/.conda/envs/my_root/lib/python2.7/site-packages/matplotlib/pyplot.pyc in quiver(*args, **kw)
3210 ax.hold(hold)
3211 try:
-> 3212 ret = ax.quiver(*args, **kw)
3213 finally:
3214 ax.hold(washold)

/home/williamsjh/.conda/envs/my_root/lib/python2.7/site-packages/cartopy/mpl/geoaxes.pyc in quiver(self, x, y, u, v, *args, **kwargs)
1678 t = self.projection
1679 if isinstance(t, ccrs.CRS) and not isinstance(t, ccrs.Projection):
-> 1680 raise ValueError('invalid transform:'
1681 ' Spherical quiver is not supported - '
1682 ' consider using PlateCarree/RotatedPole.')

ValueError: invalid transform: Spherical quiver is not supported - consider using PlateCarree/RotatedPole.

In [57]:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment