Skip to content

Commit

Permalink
Added barbs method to GeoAxes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajdawson committed Sep 18, 2013
1 parent fba062f commit 62936b6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/cartopy/mpl/geoaxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,38 @@ def quiver(self, x, y, u, v, *args, **kwargs):
u, v = self.projection.transform_vectors(t, x, y, u, v)
return matplotlib.axes.Axes.quiver(self, x, y, u, v, *args, **kwargs)

def barbs(self, x, y, u, v, *args, **kwargs):
"""
Plot a 2-D field of barbs.
See :func:`matplotlib.pyplot.barbs` for details on arguments
and keyword arguments.
.. note::
The vector components must be defined as grid eastward and
grid northward.
"""
t = kwargs.get('transform', None)
if t is None:
t = self.projection
if isinstance(t, ccrs.CRS) and not isinstance(t, ccrs.Projection):
raise ValueError('invalid transform:'
' Spherical barbs are not supported - '
' consider using PlateCarree/RotatedPole.')
if isinstance(t, ccrs.Projection):
kwargs['transform'] = t._as_mpl_transform(self)
else:
kwargs['transform'] = t
if t != self.projection:
# Transform the vectors if the projection is not the same as the
# data transform.
if x.ndim == 1 and y.ndim == 1:
x, y = np.meshgrid(x, y)
u, v = self.projection.transform_vectors(t, x, y, u, v)
return matplotlib.axes.Axes.barbs(self, x, y, u, v, *args, **kwargs)


def _trigger_patch_reclip(event):
"""
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions lib/cartopy/tests/mpl/test_mpl_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,28 @@ def test_quiver_rotated_pole():
ax.quiver(x, y, u, v, mag, transform=rp)


@ImageTesting(['barbs_plate_carree'])
def test_barbs():
x = np.arange(-60, 45, 5)
y = np.arange(30, 75, 5)
x2d, y2d = np.meshgrid(x, y)
u = 40 * np.cos(np.deg2rad(y2d))
v = 40 * np.cos(2. * np.deg2rad(x2d))
mag = (u**2 + v**2)**.5
plot_extent = [-60, 40, 30, 70]
plt.figure(figsize=(6, 6))
# plot on native projection
ax = plt.subplot(211, projection=ccrs.PlateCarree())
ax.set_extent(plot_extent, crs=ccrs.PlateCarree())
ax.coastlines()
ax.barbs(x, y, u, v, length=4, linewidth=.25)
# plot on a different projection
ax = plt.subplot(212, projection=ccrs.NorthPolarStereo())
ax.set_extent(plot_extent, crs=ccrs.PlateCarree())
ax.coastlines()
ax.barbs(x, y, u, v, transform=ccrs.PlateCarree(), length=4, linewidth=.25)


if __name__ == '__main__':
import nose
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 comments on commit 62936b6

Please sign in to comment.