导入shapefile外的掩码区域(basemap/matplotlib)

时间:2021-01-04 20:30:29

I'm plotting data on a basemap of the eastern seaboard of the U. S. and Canada through Matplotlib. In addition to the base layer (a filled contour plot), I overlayed a shapefile of this focus region atop the data using Matplotlib's readshapefile tool.

我正在通过Matplotlib在美国和加拿大东海岸的basemap上绘制数据。除了基本层(填充的等高线图)之外,我还使用Matplotlib的readshapefile工具在数据的顶部重叠了一个shapefile。

导入shapefile外的掩码区域(basemap/matplotlib)

I'm wondering how I can mask all the gridded data outside of the shapefile. I can obviously do a maskocean command through Matplotlib, but I'd still be left with the filled contours west of the St. Lawrence. Does anyone now how to do this? I haven't had much luck searching online.

我想知道如何屏蔽shapefile之外的所有网格数据。我显然可以通过Matplotlib做一个maskocean的命令,但我仍然会被留在圣劳伦斯的西部。有人知道怎么做吗?我在网上搜索运气不太好。

def make_map(lon,lat,param):
    fig, ax = plt.subplots()
    ax.axis('off')
    x1 = -83.
    x2 = -57.
    y1 = 37.
    y2 = 50.
    projection='merc'
    resolution='h'
    m = Basemap(projection=projection, llcrnrlat=y1, urcrnrlat=y2, llcrnrlon=x1,
                urcrnrlon=x2, resolution=resolution)
    x,y = m((lon-360.),lat)
    m.ax = ax
    my_cmap = cm.get_cmap('coolwarm')
    pp = m.contourf(x, y, param, 30, cmap=my_cmap, extend='both')  
    m.drawmapscale(-67, 39.5, -70, 43.5, 500, fontsize=8, barstyle='fancy') 
    return fig, m, x, y

def drawstates(ax, shapefile='../StateProv_UTMrp'):
        shp = m.readshapefile(shapefile, 'states',zorder = 1, drawbounds=True)
        for nshape, seg in enumerate(m.states):
            poly = Polygon(seg, facecolor='w',alpha=0.0, edgecolor='k')
            ax.add_patch(poly)

fig, m, x, y = make_map(lon, lat, param)
drawstates(m.ax)

1 个解决方案

#1


2  

I think this article I just found could give you some help. But I am not sure it is a full answer.

我认为我刚找到的这篇文章能给你一些帮助。但我不确定这是否是一个完整的答案。

http://basemaptutorial.readthedocs.org/en/latest/clip.html

http://basemaptutorial.readthedocs.org/en/latest/clip.html

#1


2  

I think this article I just found could give you some help. But I am not sure it is a full answer.

我认为我刚找到的这篇文章能给你一些帮助。但我不确定这是否是一个完整的答案。

http://basemaptutorial.readthedocs.org/en/latest/clip.html

http://basemaptutorial.readthedocs.org/en/latest/clip.html