如何在Matplotlib中叠加Shapefile

3
在Matplotlib中,如何将文件夹中提供的shapefile(如下所示)叠加在图表外部的右上角位置。 enter image description here

1
也许你可以使用figimage,如这里所描述的那样。 - banderkat
@JSuar,感谢您的回答,很抱歉我回复晚了。在悬赏过期之前我没能来到这里,但我会给您一个新的悬赏奖励。 - 2964502
@viena,你是否为这个问题添加了另一个悬赏?你还在遇到问题吗? - JSuar
@JSuar 这只是为了奖励你,因为我的上一个50分赏金在赏金宽限期内无法检查stackoverflow而失去了。 - 2964502
@viena 我很感激。作为以后的参考,您可以直接向一个答案授予赏金,而不需要重新分配问题的赏金。请参阅:我能向旧答案授予赏金吗? - JSuar
显示剩余2条评论
2个回答

4

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 = 0

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

zorder = 1

其他有用的参考资料:


3
你可以使用basemap工具包来加载和绘制shapefile。这里我已经在单独的axes中绘制了shapeFile,并使用'subplot2grid'将其对齐到其他axes图的右上角。
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

输出:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接