Skip to content

Instantly share code, notes, and snippets.

@dennissergeev
Last active May 18, 2020 14:19
Show Gist options
  • Save dennissergeev/d52c4265db78488ee66bff9ee9797585 to your computer and use it in GitHub Desktop.
Save dennissergeev/d52c4265db78488ee66bff9ee9797585 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from matplotlib.offsetbox import AnchoredText
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
# Set extent of the map
ax.set_extent([-130, -90, 10, 60], crs=ccrs.PlateCarree())
# Create a feature for States/Admin 1 regions at 1:50m from Natural Earth
states_provinces = cfeature.NaturalEarthFeature(
category="cultural",
name="admin_1_states_provinces_lines",
scale="50m",
facecolor="none",
)
ax.add_feature(states_provinces, edgecolor="red")
# Create and add a feature for Coastlines at 1:110m from Natural Earth
ax.add_feature(
cfeature.NaturalEarthFeature(
category="physical",
name="coastline",
scale="110m",
facecolor="none",
edgecolor="#333333",
)
)
# Show gridlines with labels
ax.gridlines(draw_labels=True)
# Add a label
my_label = AnchoredText("(a) the map", loc=2, prop=dict(size=18, color='b'))
ax.add_artist(my_label)
# ADD DATA TO THE MAP
ax.scatter(lons, lats, facecolor='C0', edgecolor='C9', transform=ccrs.PlateCarree())
@multilingualtricker
Copy link

Hey Denis,

Thanks for this!
What elegant code if I may say so?

Sorry if I didn't make this clear in the initial brief. Was thinking along the lines of the 'satellite' mode in Google Maps (as opposed to the 'maps' mode where you can literally see where mountains are). Essentially if any coordinates that are above the Rockies, I need to delete them, hence I need to see where the Rockies are exactly.

Can this be done as an add.feature easily?

@dennissergeev
Copy link
Author

If you want to make a plot with a satellite background a la Google Maps, you can do it in cartopy, here's an example: https://scitools.org.uk/cartopy/docs/latest/gallery/wmts_time.html#sphx-glr-gallery-wmts-time-py

However, I don't get why you would need such a plot - you only need to extract a polygon around the Rockies.
You can do it either manually by making a list of longitudes and latitudes around the Rockies, or you can ask around at the department, maybe someone already has it, ideally as a Shapely polygon. Then you can use this package to create a region mask:
https://regionmask.readthedocs.io/en/stable/notebooks/create_own_regions.html

@multilingualtricker
Copy link

Thanks Denis! Much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment