CartoPy¶
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¶
In [1]:
Copied!
import cartopy.feature as cfeature
land_50m = cfeature.NaturalEarthFeature('physical', 'land', '50m',
edgecolor='face',
facecolor=cfeature.COLORS['land'])
import cartopy.feature as cfeature
land_50m = cfeature.NaturalEarthFeature('physical', 'land', '50m',
edgecolor='face',
facecolor=cfeature.COLORS['land'])
Example 2¶
In [2]:
Copied!
import cartopy.feature
sorted(cartopy.feature.COLORS.keys())
import cartopy.feature
sorted(cartopy.feature.COLORS.keys())
Out[2]:
['land', 'land_alt1', 'water']
Example 3¶
In [3]:
Copied!
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from 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')
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from 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¶
In [4]:
Copied!
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_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())
import cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.PlateCarree())
ax.stock_img()
ny_lon, ny_lat = -75, 43
delhi_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())
Out[4]:
Text(80.23, 16.61, 'Delhi')
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.
In [5]:
Copied!
import matplotlib.pyplot as plt
from scipy.io import netcdf_file
from cartopy import config
import 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()
import matplotlib.pyplot as plt
from scipy.io import netcdf_file
from cartopy import config
import 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¶
Show a satellite image of Hurricane Miriam (from cartopy's sample data) in its geographic position
Image: NASA (Aqua/MODIS).
In [6]:
Copied!
import os
import matplotlib.pyplot as plt
from cartopy import config
import 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 data
ax.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 corner
ax.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 ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())
ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())
plt.show()
import os
import matplotlib.pyplot as plt
from cartopy import config
import 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 data
ax.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 corner
ax.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 ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())
ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())
plt.show()