Skip to content

Instantly share code, notes, and snippets.

@secsilm
Last active September 4, 2017 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save secsilm/37db690ab9716f768d1a1e43d3f53e3f to your computer and use it in GitHub Desktop.
Save secsilm/37db690ab9716f768d1a1e43d3f53e3f to your computer and use it in GitHub Desktop.
Add image on the top of map using cartopy
'''
This code is a example for adding image on the top of map using cartopy.
The generated image can be found here: https://i.imgur.com/aTY1rYY.png
'''
import matplotlib.pyplot as plt
import cartopy.crs as crs
from matplotlib.offsetbox import AnnotationBbox, OffsetImage
from PIL import Image
# Read image
lat = 116
lon = 39
img = Image.open('Flag_of_China.png')
# Plot the map
fig = plt.figure(figsize=(10, 5))
ax = plt.axes(projection=crs.PlateCarree())
ax.coastlines()
ax.stock_img()
# Use `zoom` to control the size of the image
imagebox = OffsetImage(img, zoom=0.01)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, [lat, lon], pad=0, frameon=False)
ax.add_artist(ab)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment