An explicit implementation of the polar stereographic map projection in cartesian (x, y) coordinates, for plotting data in Python with matplotlib.
Instead of implicitly transforming coordinates via arguments to matplotlib functions, the transformed coordinates are calculated for direct use in plotting commands on regular cartesian axes. The package also includes an interface to add decorators such as grid lines and land/coastlines, the latter using Natural Earth data.
This code was originally motivated by some issues encountered using implicit transformations, such as artefacts arising across the 0° meridian and at the pole. These do not occur in the explicit (x, y) coordinate system because the pole is just the origin (0, 0) and there are no discontinuities.
- matplotlib
- NumPy
- Python Shapefile Library (PyShp)
- requests (only needed for the script downloading Natural Earth data)
These may be addressed in the future:
- Limited API support for the south polar stereographic projection
- Latitudes are assumed to be geocentric, not geodetic
- Land/coastline overlays only work with 1:110m and 1:50m scale Natural Earth data
- Cursor coordinates in the interactive plot window display (x, y) rather than (λ, ϕ)
- Clone the repository
- Make the package visible on the python PATH (e.g., on UNIX,
export PYTHONPATH=${PYTHONPATH}:$(pwd)/lib, executed from the repository top-level directory) - Download the Natural Earth data (
scripts/ne_download.py)
Assuming data in an array data defined on coordinates lon and lat:
import matplotlib.pyplot as plt
import posterproxy as psp
fig, ax = plt.subplots()
psp.prepare_axes(ax)
x, y = psp.lonlat_to_xy_npsp(lon, lat)
ax.contour(x, y, data)
psp.xy_land_overlay(ax)
psp.xy_gridlines(ax)The equivalent using cartopy:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
ax = plt.axes(projection=ccrs.NorthPolarStereo())
ax.set_extent((0, 360, 60, 90), crs=ccrs.PlateCarree())
ax.contour(lon, lat, data, transform=ccrs.PlateCarree())
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.COASTLINE)
ax.gridlines()The default land, coastlines, and grid lines are set to resemble cartopy's defaults (but can also be changed via configuration files or on-the-fly using function arguments):
