Description:
- Geospatial data processing to produce maps and other geospatial data analyses.
Resources:
- [GitHub] https://github.com/SciTools/cartopy/
- [Examples] https://github.com/SciTools/cartopy/tree/main/examples
- [Docs] https://scitools.org.uk/cartopy/docs/latest/
Source and license:
- Some of these examples are adapted from the Cartopy example gallery (https://cartopy.readthedocs.io/stable/gallery/index.html), Copyright (c) Crown and Cartopy contributors, licensed under BSD-3-Clause. That code stays under its own license; see THIRD_PARTY_NOTICES.md.
Example 1
Section titled “Example 1”import cartopy.feature as cfeatureland_50m = cfeature.NaturalEarthFeature('physical', 'land', '50m', edgecolor='face', facecolor=cfeature.COLORS['land'])Example 2
Section titled “Example 2”import cartopy.featuresorted(cartopy.feature.COLORS.keys())['land', 'land_alt1', 'water']Example 3
Section titled “Example 3”import cartopy.crs as ccrsimport matplotlib.pyplot as pltfrom pathlib import Path
ax = plt.axes(projection=ccrs.PlateCarree())ax.coastlines()
dir = Path('/tmp/ganymede_demo/')dir.mkdir(parents=True, exist_ok=True)
# Save the plot by calling plt.savefig() BEFORE plt.show()plt.savefig('/tmp/ganymede_demo/coastlines.pdf')plt.savefig('/tmp/ganymede_demo/coastlines.png')
Example 4
Section titled “Example 4”import cartopy.crs as ccrsimport matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())ax.stock_img()
ny_lon, ny_lat = -75, 43delhi_lon, delhi_lat = 77.23, 28.61
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], color='blue', linewidth=2, marker='o', transform=ccrs.Geodetic(), )
plt.plot([ny_lon, delhi_lon], [ny_lat, delhi_lat], color='gray', linestyle='--', transform=ccrs.PlateCarree(), )
plt.text(ny_lon - 3, ny_lat - 12, 'New York', horizontalalignment='right', transform=ccrs.Geodetic())
plt.text(delhi_lon + 3, delhi_lat - 12, 'Delhi', horizontalalignment='left', transform=ccrs.Geodetic())Text(80.23, 16.61, 'Delhi')
Example 5
Section titled “Example 5”Draw filled contours of sea surface temperature from a netCDF file (from cartopy’s sample data)
Data: HadISST, Met Office Hadley Centre (Rayner et al. 2003, doi:10.1029/2002JD002670), © British Crown Copyright, Met Office; see https://www.metoffice.gov.uk/hadobs/hadisst/ for its terms.
import matplotlib.pyplot as pltfrom scipy.io import netcdf_file
from cartopy import configimport cartopy.crs as ccrs
# get the path of the file. It ships in cartopy's sample data directory.fname = config["repo_data_dir"] / 'netcdf' / 'HadISST1_SST_update.nc'
dataset = netcdf_file(fname, maskandscale=True, mmap=False)sst = dataset.variables['sst'][0, :, :]lats = dataset.variables['lat'][:]lons = dataset.variables['lon'][:]
ax = plt.axes(projection=ccrs.PlateCarree())
plt.contourf(lons, lats, sst, 60, transform=ccrs.PlateCarree())
ax.coastlines()
Example 6
Section titled “Example 6”Show a satellite image of Hurricane Miriam (from cartopy’s sample data) in its geographic position
Image: NASA (Aqua/MODIS).
import osimport matplotlib.pyplot as plt
from cartopy import configimport cartopy.crs as ccrs
fig = plt.figure(figsize=(8, 12))
# get the path of the file. It ships in cartopy's sample data directory.fname = config["repo_data_dir"] / 'raster' / 'sample' / 'Miriam.A2012270.2050.2km.jpg'img_extent = (-120.67660000000001, -106.32104523100001, 13.2301484511245, 30.766899999999502)img = plt.imread(fname)
ax = plt.axes(projection=ccrs.PlateCarree())plt.title('Hurricane Miriam from the Aqua/MODIS satellite\n' '2012 09/26/2012 20:50 UTC')
ax.use_sticky_edges = False# set a margin around the dataax.set_xmargin(0.05)ax.set_ymargin(0.10)
# add the image. Because this image was a tif, the "origin" of the image is in the# upper left cornerax.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())ax.coastlines(resolution='50m', color='black', linewidth=1)
# mark a known place to help us geo-locate ourselvesax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())
plt.show()
ganymede is made by Eugo