Note
Click here to download the full example code
XArray Projection Handling¶
Use MetPy’s XArray accessors to simplify opening a data file and plotting data on a map using CartoPy.

Out:
/home/travis/virtualenv/python3.6.7/lib/python3.6/site-packages/pooch/core.py:352: UserWarning: Downloading data file 'narr_example.nc' from remote data store 'https://github.com/Unidata/MetPy/raw/v0.10.2/staticdata/narr_example.nc' to '/home/travis/build/Unidata/MetPy/staticdata/v0.10.2'.
action_word[action], fname, self.get_url(fname), str(self.path)
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import xarray as xr
# Any import of metpy will activate the accessors
from metpy.testing import get_test_data
ds = xr.open_dataset(get_test_data('narr_example.nc', as_file_obj=False))
data_var = ds.metpy.parse_cf('Temperature')
x = data_var.x
y = data_var.y
im_data = data_var.isel(time=0).sel(isobaric=1000.)
fig = plt.figure(figsize=(14, 14))
ax = fig.add_subplot(1, 1, 1, projection=data_var.metpy.cartopy_crs)
ax.imshow(im_data, extent=(x.min(), x.max(), y.min(), y.max()),
cmap='RdBu', origin='lower' if y[0] < y[-1] else 'upper')
ax.coastlines(color='tab:green', resolution='10m')
ax.add_feature(cfeature.LAKES.with_scale('10m'), facecolor='none', edgecolor='tab:blue')
ax.add_feature(cfeature.RIVERS.with_scale('10m'), edgecolor='tab:blue')
plt.show()
Total running time of the script: ( 0 minutes 12.781 seconds)