在Cartopy地图上添加具有给定坐标(纬度,经度)的图像

问题描述 投票:0回答:2

我想在小地图上用小图像/图标标记某个位置(纬度,经度)。我该怎么办?

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()

我在这里找到了消息来源:https://gist.github.com/secsilm/37db690ab9716f768d1a1e43d3f53e3f但这对我不起作用,显示的地图没有任何标志。还有其他方法吗?

非常感谢!

python matplotlib cartopy
2个回答
1
投票

以下内容对我来说很好:

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)

您可能想通过将zoom更改为较大的值或将frameon设置为True来尝试调试。如果您还有其他问题,请确保发布您的lon / lat值。


0
投票

我尝试了您链接的要点中的代码,因为它使用cartopy对图像进行地理配准。我得到以下结果:

world map with china flag

这是您想要的效果吗?我从要点复制的代码中唯一更改的是图片。我使用了this one

© www.soinside.com 2019 - 2024. All rights reserved.