【问题标题】:how to overlay a shapefile in matplotlib如何在 matplotlib 中覆盖 shapefile
【发布时间】:2023-03-30 09:18:01
【问题描述】:

在matplotlib中如何覆盖shapefile(在文件夹中可用)如下图的右上角位置。

【问题讨论】:

  • 也许您可以使用 figimage,如 [此处][1] 所述。 [1]:stackoverflow.com/questions/3609585/…
  • 对所提供的答案有帮助吗?
  • @JSuar 感谢您的回答,对于我迟到的回复感到抱歉。在赏金到期之前我不能来这里,会给你新的赏金。
  • @viena 不用担心。很高兴我能帮上忙。
  • @viena 您是否为这个问题添加了另一个赏金?您仍然遇到问题吗?

标签: matplotlib


【解决方案1】:

banderkat引用的代码:

import matplotlib.pyplot as plt
import Image
import numpy as np

im = Image.open('Jbc4j.jpg')
width = im.size[0]
height = im.size[1]

# We need a float array between 0-1, rather than
# a uint8 array between 0-255
im = np.array(im).astype(np.float) / 255

a = np.random.randint(0,100,100)
b = range(100)
fig = plt.figure(1,figsize=(5, 7), dpi=80, facecolor='w')
ax = fig.add_subplot(111)
ax.scatter(a,b)
fig.canvas.draw()

# With newer (1.0) versions of matplotlib, you can 
# use the "zorder" kwarg to make the image overlay
# the plot, rather than hide behind it... (e.g. zorder=10)
fig.figimage(im, fig.bbox.xmax - width, fig.bbox.ymax - height, zorder=0)

# (Saving with the same dpi as the screen default to
#  avoid displacing the logo image)
fig.savefig('temp.png', dpi=80)

plt.show()

产生以下结果(图像被裁剪以节省空间)。

更改zorder=1 会将图像置于顶部。

其他有用的参考资料:

【讨论】:

    【解决方案2】:

    您可以使用底图工具包来加载和绘制 shapefile。在这里,我将 shapeFile 绘制在单独的轴上,并使用“subplot2grid”将其与其他轴图的右上角对齐。

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.basemap import Basemap
    import matplotlib.gridspec as gridspec
    
    
    def plotShapeFile():
        # Lambert Conformal Conic map.
        m = Basemap(llcrnrlon=-100.,llcrnrlat=0.,urcrnrlon=-20.,urcrnrlat=57.,
                    projection='lcc',lat_1=20.,lat_2=40.,lon_0=-60.,
                    resolution ='l',area_thresh=1000.)
        # read shapefile.
        shp_info = m.readshapefile('C:/basemap-1.0.6/basemap-1.0.6/examples/huralll020','hurrtracks',drawbounds=False)
        # find names of storms that reached Cat 4.
        names = []
        for shapedict in m.hurrtracks_info:
            cat = shapedict['CATEGORY']
            name = shapedict['NAME']
            if cat in ['H4','H5'] and name not in names:
                # only use named storms.
                if name != 'NOT NAMED':  names.append(name)
    
        # plot tracks of those storms.
        for shapedict,shape in zip(m.hurrtracks_info,m.hurrtracks):
            name = shapedict['NAME']
            cat = shapedict['CATEGORY']
            if name in names:
                xx,yy = zip(*shape)
                # show part of track where storm > Cat 4 as thick red.
                if cat in ['H4','H5']:
                    m.plot(xx,yy,linewidth=1.5,color='r')
                elif cat in ['H1','H2','H3']:
                    m.plot(xx,yy,color='k')
    
        # draw coastlines, meridians and parallels.
        m.drawcoastlines()
        m.drawcountries()
        m.drawmapboundary(fill_color='#99ffff')
        m.fillcontinents(color='#cc9966',lake_color='#99ffff')
        m.drawparallels(np.arange(10,70,20),labels=[1,1,0,0])
        m.drawmeridians(np.arange(-100,0,20),labels=[0,0,0,1])
    
    
    
    
    if __name__ == '__main__':
        fig=plt.figure()
        plt.subplots_adjust(wspace=0.001, hspace=0.001)
    
        ax1=plt.subplot2grid((5,5), (0,0), colspan=4, rowspan=4)
        labels = 'Frogs', 'Hogs', 'Dogs', 'Logs'
        fracs = [15,30,45, 10]
        explode=(0, 0.05, 0, 0)
        p1,t1,at1 = plt.pie(fracs, explode=explode, labels=labels, autopct='%1.1f%%', shadow=True)
        plt.title('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
    
        ax2=plt.subplot2grid((5,5), (0,4), colspan=1, rowspan=1)
        #draw shapeFile on the current active axes, i.e. ax2
        plotShapeFile()
    
        plt.tight_layout()
        plt.show()
    

    以下是我使用过的参考资料的链接:

    http://sourceforge.net/projects/matplotlib/files/matplotlib-toolkits/basemap-1.0.6/ http://matplotlib.org/basemap/users/examples.html

    输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 2015-11-30
      • 2020-04-27
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      相关资源
      最近更新 更多