From cpelley: https://gist.github.com/cpelley/6351152
In [1]:
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
In [2]:
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)
In [3]:
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
figure(figsize=(8,8))
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()