【问题标题】:Python Matplotlib Basemap overlay small image on map plotPython Matplotlib Basemap 在地图图上叠加小图像
【发布时间】:2012-07-14 07:49:33
【问题描述】:

我正在地图上绘制来自飞机的数据,我想在绘图上最新数据点的坐标处插入这架 75 像素 x 29 像素的飞机 PNG 图像。

据我所知并已阅读,pyplot.imshow() 是实现此目的的最佳方式。但是,我在第 1 步上挂断了,让图像均匀显示。使用普通绘图而不是底图,使用 imshow 很容易让图像出现,但是使用底图时,我根本无法显示它。请参阅示例代码。

如果我可以让图像显示在地图上,我假设我可以通过反复试验,使用imshow()extent 属性为它设置它的位置和一些适当的尺寸,与情节从地图坐标x,y = m(lons,lats)转换而来的坐标。

这是示例代码(要尝试它,您可能需要下载上面的飞机图像)。

from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
import Image
from numpy import arange

lats = arange(26,29,0.5)
lons = arange(-90,-87,0.5)

m = Basemap(projection='cyl',llcrnrlon=min(lons)-2,llcrnrlat=min(lats)-2,
            urcrnrlon=max(lons)+2,urcrnrlat=max(lats)+2,resolution='i')

x,y = m(lons,lats)
u,v, = arange(0,51,10),arange(0,51,10)
barbs = m.barbs(x,y,u,v)
m.drawcoastlines(); m.drawcountries(); m.drawstates()

img = Image.open('c130j_75px.png')
im = plt.imshow(img, extent=(x[-1],x[-1]+50000,y[-1],y[-1]+50000))
plt.show()

这是生成的图像,其中不包含飞机的痕迹。我使用extent 尝试了几种不同的尺寸,我想我可能只是把它做得太小了,但没有成功。我也尝试设置zorder=10,但也没有运气。任何帮助将不胜感激。

更新:我现在至少可以使用m.imshow 而不是plt.imshow 来显示图像,因为前者传入地图的轴实例,但extent 参数似乎对尺寸没有影响图像的大小,因为无论我将extent 尺寸做得多么小,即使我将它们设置为零,它总是会填满整个绘图。如何适当缩放飞机图像并将其定位在最后一个数据点附近?

im = m.imshow(img, extent=(x[-1],x[-1]+5,y[-1],y[-1]+2))

【问题讨论】:

    标签: python image matplotlib overlay matplotlib-basemap


    【解决方案1】:

    使用底图,如果您首先使用地图实例转换坐标,则通常可以只使用普通的 pyplot 样式命令。在这种情况下,您只需将范围转换为 uv 坐标:

    x0, y0 = m(x[-1], y[-1])
    x1, y1 = m(x[-1] + 0.5, y[-1] + 0.5)
    

    然后你将能够做到:

    im = plt.imshow(img, extent=(x0, x1, y0, y1))
    

    我对此的完整解决方案如下:

    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    import numpy as np
    
    
    lats = np.arange(26, 29, 0.5)
    lons = np.arange(-90, -87, 0.5)
    
    m = Basemap(projection='cyl', llcrnrlon=min(lons)-2, llcrnrlat=min(lats)-2,
                urcrnrlon=max(lons)+2, urcrnrlat=max(lats)+2, resolution='h')
    
    x, y = m(lons,lats)
    u, v = np.arange(0, 51, 10), np.arange(0, 51, 10)
    barbs = m.barbs(x, y, u, v)
    
    m.drawcoastlines()
    m.fillcontinents()
    
    x_size, y_size = 0.8, 0.4
    x0, y0 = m(x[-1] - x_size/2., y[-1] - y_size/2.)
    x1, y1 = m(x[-1] + x_size/2., y[-1] + y_size/2.)
    im = plt.imshow(plt.imread('mslr86.png'), extent=(x0, x1, y0, y1))
    
    plt.show()
    

    生成的图像看起来像

    更新:如果您希望图像保持固定大小,独立于缩放,请参阅 Joe 的回答。

    【讨论】:

      【解决方案2】:

      实际上,为此,您想使用 matplotlib 的一些未记录的功能:matplotlib.offsetbox 模块。这里有一个例子:http://matplotlib.sourceforge.net/trunk-docs/examples/pylab_examples/demo_annotation_box.html

      在你的情况下,你会做这样的事情:

      import matplotlib.pyplot as plt
      import numpy as np
      import Image
      
      from mpl_toolkits.basemap import Basemap
      from matplotlib.offsetbox import OffsetImage, AnnotationBbox
      
      # Set up the basemap and plot the markers.
      lats = np.arange(26, 29, 0.5)
      lons = np.arange(-90, -87, 0.5)
      
      m = Basemap(projection='cyl',
                  llcrnrlon=min(lons) - 2, llcrnrlat=min(lats) - 2,
                  urcrnrlon=max(lons) + 2, urcrnrlat=max(lats) + 2,
                  resolution='i')
      
      x,y = m(lons,lats)
      u,v, = np.arange(0,51,10), np.arange(0,51,10)
      barbs = m.barbs(x,y,u,v)
      
      m.drawcoastlines()
      m.drawcountries()
      m.drawstates()
      
      # Add the plane marker at the last point.
      plane = np.array(Image.open('plane.jpg'))
      im = OffsetImage(plane, zoom=1)
      ab = AnnotationBbox(im, (x[-1],y[-1]), xycoords='data', frameon=False)
      
      # Get the axes object from the basemap and add the AnnotationBbox artist
      m._check_ax().add_artist(ab)
      
      plt.show()
      

      这样做的好处是平面位于轴坐标中,并且在放大时相对于图形的大小将保持相同的大小。

      【讨论】:

      • 我正在尝试使用此方法通过 matplotlib.animation 包为每帧多个图像设置动画,但收到错误 AttributeError: 'list' object has no attribute 'axes'。您是否推荐一种将这种方法用于动画的方法?
      • 错过了编辑截止日期,但我想将问题更改为:您是否推荐一种使用这种方法为每帧多张图像制作动画的方法?谢谢乔。
      • 对于任何想要绘制多个图像并控制其zorder 的人,请使用方法matplotlib.offsetbox.AnnotationBbox.set_zorder
      • 以防万一有人遇到同样的问题,要导入 Image 我必须安装包 pillow,然后将其导入为 from PIL import Image .
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 2014-08-10
      • 2021-12-11
      相关资源
      最近更新 更多