Last active
September 4, 2017 13:20
-
-
Save secsilm/37db690ab9716f768d1a1e43d3f53e3f to your computer and use it in GitHub Desktop.
Add image on the top of map using cartopy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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