Andrew Dawson has made some excellent contributions to cartopy's vector quantity visualisations recently. As these features are un-released as yet, this notebook is working with cartopy master:
import cartopy
cartopy.__version__
'0.10.x'
import cartopy.crs as ccrs
import numpy as np
import matplotlib.pyplot as plt
Lets fake up some data on a rotated pole coordinate system:
x = np.linspace(312, 392, 20)
y = np.linspace(-23, 25, 25)
x2d, y2d = np.meshgrid(np.deg2rad(x), np.deg2rad(y))
u = (np.cos(2 * x2d + 1) * np.cos(4 * y2d + 1)) * 6
v = -8 * (np.cos(2. * y2d) * np.sin(3 * x2d))
data_cs = ccrs.RotatedPole(pole_longitude=177.5, pole_latitude=37.5)
mag = (u**2 + v**2)**.5
Lets do a basic visualisation of the data:
plt.figure(figsize=(12, 12))
ax = plt.axes(projection=data_cs)
ax.coastlines('50m')
plt.margins(0.3)
ax.quiver(x, y, u, v, mag, transform=data_cs)
#ax.stock_img()
plt.show()
We can also plot these vectors in any other coordinate system:
plt.figure(figsize=(12, 12))
ax = plt.axes(projection=ccrs.Orthographic(central_latitude=15))
ax.coastlines('50m')
plt.margins(0.3)
ax.quiver(x, y, u, v, mag, transform=data_cs)
ax.stock_img()
plt.show()
Sometimes we want the vectors to be resampled to have a regular spacing in the target projection:
plot_extent = [-60, 40, 30, 70]
plt.figure(figsize=(12, 12))
ax1 = plt.subplot(211, projection=ccrs.PlateCarree())
ax1.set_extent(plot_extent, crs=ccrs.PlateCarree())
ax1.coastlines('50m')
ax1.stock_img()
plt.title('No resampling')
ax1.quiver(x, y, u, v, mag, transform=data_cs)
ax2 = plt.subplot(212, projection=ccrs.PlateCarree())
ax2.set_extent(plot_extent, crs=ccrs.PlateCarree())
ax2.coastlines('50m')
ax2.stock_img()
plt.title('With resampling')
ax2.quiver(x, y, u, v, mag, transform=data_cs, regrid_shape=20)
plt.show()
Other vector visualisation techniques have also been added:
plt.figure(figsize=(12, 16))
ax = plt.axes(projection=ccrs.Orthographic(central_latitude=15))
ax.coastlines('50m')
plt.margins(0.3)
ax.barbs(x, y, u, v, transform=data_cs, length=5)
ax.stock_img()
plt.show()
With more to come in the near future:
plt.figure(figsize=(12, 16))
ax = plt.axes(projection=ccrs.Orthographic(central_latitude=15))
ax.coastlines('50m')
plt.margins(0.3)
ax.streamplot(x, y, u, v, transform=data_cs, density=1.5,
color=mag, linewidth=5 * (mag**2) / (mag**2).max())
ax.stock_img()
plt.show()