knam
knam

Reputation: 17

add a image with given coordination (lat, lon) on the cartopy map

I want to mark some location (lat,lon) on a cartopy map by a small image/icon. how can I do that ?

import cartopy.crs as crs
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(10, 5))
ax = plt.axes(projection=crs.PlateCarree())
ax.stock_img()

img = plt.imread('flag.png') #the image I want to add on the map

plt.show()

I found a source here: https://gist.github.com/secsilm/37db690ab9716f768d1a1e43d3f53e3f But it doesn't work for me, the map show up without any flag. Is there any other way to do that ?

Thank a lot!.

Upvotes: 0

Views: 1103

Answers (2)

DopplerShift
DopplerShift

Reputation: 5843

The following worked just fine for me:

import matplotlib.pyplot as plt
import cartopy.crs as crs
from matplotlib.offsetbox import AnnotationBbox, OffsetImage

# Read image
lat = 39
lon = -95
img = plt.imread('/Users/rmay/Downloads/flag.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=.1)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, [lon, lat], pad=0, frameon=False)
ax.add_artist(ab)

You might want to try debugging by changing zoom to larger values or setting frameon to True. If you have further problem, be sure to post your values for lon/lat.

Upvotes: 3

Callum Rollo
Callum Rollo

Reputation: 605

I tried the code in the gist you linked as it uses cartopy to georeference the image. I got the following result:

world map with china flag

Is this the effect you desired? The only thing I changed in the code copied from the gist is the picture. I used this one

Upvotes: 0

Related Questions