Cartopy Projections

Cartopy supports several map projections. If you have data that’s on a particular map projection, you can easily project it onto a different map projection.

The purpose of this notebook is to generate a plot using every map projection in Cartopy.

A full list of supported projections can be found here.

%matplotlib inline
import warnings
warnings.simplefilter("ignore") # Silence warnings
import hvplot.xarray
import xarray as xr
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import geoviews as gv
import holoviews as hv
import numpy as np
#hv.extension('bokeh')

Read data

ds = xr.open_dataset("../data/cdf/uv300.nc").load()
ds
<xarray.Dataset>
Dimensions:  (lat: 64, lon: 128, time: 2)
Coordinates:
  * lat      (lat) float32 -87.8638 -85.09653 -82.31291 ... 85.09653 87.8638
  * lon      (lon) float32 -180.0 -177.1875 -174.375 ... 174.375 177.1875
  * time     (time) int32 1 7
Data variables:
    gw       (lat) float32 0.0017832807 0.004147033 ... 0.004147033 0.0017832807
    U        (time, lat, lon) float32 2.0942385 2.3456185 ... 1.3936923
    V        (time, lat, lon) float32 -4.421005 -4.3354754 ... -0.3070775
Attributes:
    title:          UV300: January and July
    source:         Climate Analysis Section, NCAR
    history:        Dataset uv300.hs from EZPLOT demo dataset
    Conventions:    None
    creation_date:  Mon Mar 29 09:24:57 MST 1999
    references:     \nEZPLOT for Publication Quality Plots\nChristian Guillem...
ds = ds.isel(time=1)
ds
<xarray.Dataset>
Dimensions:  (lat: 64, lon: 128)
Coordinates:
  * lat      (lat) float32 -87.8638 -85.09653 -82.31291 ... 85.09653 87.8638
  * lon      (lon) float32 -180.0 -177.1875 -174.375 ... 174.375 177.1875
    time     int32 7
Data variables:
    gw       (lat) float32 0.0017832807 0.004147033 ... 0.004147033 0.0017832807
    U        (lat, lon) float32 -1.8150927 -1.6226397 ... 1.3709666 1.3936923
    V        (lat, lon) float32 -4.1471963 -4.256537 ... -0.31886855 -0.3070775
Attributes:
    title:          UV300: January and July
    source:         Climate Analysis Section, NCAR
    history:        Dataset uv300.hs from EZPLOT demo dataset
    Conventions:    None
    creation_date:  Mon Mar 29 09:24:57 MST 1999
    references:     \nEZPLOT for Publication Quality Plots\nChristian Guillem...

Define plotting function with some default resources common to all map projections.

def plot(variable='U', projection=ccrs.PlateCarree(), width=600, height=540, levels=10, 
         cmap='Spectral', global_extent=True, **kwargs):
    
    """Plot filled countour plots for different cartopy projections while using hvplot/holoviews/geoviews"""
    long_name = ds[variable].long_name
    units = ds[variable].units

    spacing = 80 * " "
    title = f'{long_name} {spacing} {units}'
    p = ds.hvplot(x='lon', y='lat', z=variable, projection=projection,
                  width=width, height=height, cmap=cmap, dynamic=False, 
                  kind='contourf', levels=levels, title=title, 
                  legend='bottom', global_extent=global_extent, 
                  ylabel=f'{ds.lat.long_name}[{ds.lat.units}]', 
                  xlabel=f'{ds.lon.long_name}[{ds.lon.units}]',
                  **kwargs
    ) * gv.feature.coastline
    
    return p

PlateCarree

plot(projection=ccrs.PlateCarree())
plot(projection=ccrs.PlateCarree(), levels=10)

AlbersEqualArea

plot(projection=ccrs.AlbersEqualArea())

AzimuthalEquidistant

plot(projection=ccrs.AzimuthalEquidistant())