Note
Click here to download the full example code
Drawing a US map in pythonΒΆ
An example of drawing a simple map of the USA with cartopy
Using cartopy version 0.17.0
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import pandas as pd
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 200
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())
shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m', category='cultural',
name=shapename)
ax.outline_patch.set_visible(False) # don't draw the map border
ax.set_title('My map of the lower 48')
# example state coloring
colors = {
'Minnesota': [0, 1, 0],
'Texas': "#FF0000",
'Montana': "blue",
}
default_color = [0.9375, 0.9375, 0.859375]
for state in shpreader.Reader(states_shp).records():
facecolor = colors.get(state.attributes['name'], default_color)
ax.add_geometries([state.geometry], ccrs.PlateCarree(),
facecolor=facecolor, edgecolor='grey', alpha=0.5)
# example data
df = pd.DataFrame(columns=['city', 'lat', 'lon'], data=[
('Hoboken', 40.745255, -74.034775),
('Port Hueneme', 34.155834, -119.202789),
('Auburn', 42.933334, -76.566666),
('Jamestown', 42.095554, -79.238609),
('Fulton', 38.846668, -91.948059),
('Bedford', 41.392502, -81.534447)
])
ax.plot(df['lon'], df['lat'], transform=ccrs.PlateCarree(),
ms=8, ls='', marker='*')
plt.show()

Total running time of the script: ( 0 minutes 0.452 seconds)