【问题标题】:add a image with given coordination (lat, lon) on the cartopy map在地图上添加具有给定坐标(纬度,经度)的图像
【发布时间】:2020-04-14 11:55:17
【问题描述】:

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

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


    【解决方案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 值。

    【讨论】:

      【解决方案2】:

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

      world map with china flag

      这是你想要的效果吗?我在从 gist 复制的代码中唯一更改的是图片。我用this one

      【讨论】:

      • 是的,这正是我想要的。我发现了我的愚蠢错误。我没有更改缩放参数,所以我的图像太小以至于它从地图上消失了。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      相关资源
      最近更新 更多