有很多类似的问题(How to draw rectangles on a Basemap和http://matplotlib.1069221.n5.nabble.com/display-a-filled-lat-lon-basemap-rectangle-td11562.html),但我仍然无法弄清楚如何做到这一点。
我想遮蔽两个方框所覆盖的区域,每个角的坐标(UR =右上角,LL =左下角......)由下式给出:
box 1:
UR_box1_lat = 72.9
UR_box1_lon = -160
LL_box1_lat = 71.2
LL_box1_lon = -176.5
box 2 :
UL_box2_lat = LL_box1_lat
UL_box2_lon = LL_box1_lon
LR_box2_lat = 69.304
LR_box2_lon = -164.5
这会产生我想要在以下位置遮蔽多边形的域的基础地图:
# make map with these (i_total, j_total) indices as a box shaded or outlined..
# read in etopo5 topography/bathymetry.
url = 'http://ferret.pmel.noaa.gov/thredds/dodsC/data/PMEL/etopo5.nc'
etopodata = Dataset(url)
topoin = etopodata.variables['ROSE'][:]
lons = etopodata.variables['ETOPO05_X'][:]
lats = etopodata.variables['ETOPO05_Y'][:]
# shift data so lons go from -180 to 180 instead of 20 to 380.
topoin,lons = shiftgrid(180.,topoin,lons,start=False)
# plot topography/bathymetry as an image.
# create the figure and axes instances.
fig = plt.figure()
ax = fig.add_axes([0.1,0.1,0.8,0.8])
# setup of basemap ('lcc' = lambert conformal conic).
# use major and minor sphere radii from WGS84 ellipsoid.
m = Basemap(llcrnrlon=175.,llcrnrlat=50.,urcrnrlon=-120.,urcrnrlat=75.,\
rsphere=(6378137.00,6356752.3142),\
resolution='l',area_thresh=1000.,projection='lcc',\
lat_1=66.,lon_0=-169.,ax=ax)
# transform to nx x ny regularly spaced 5km native projection grid
nx = int((m.xmax-m.xmin)/5000.)+1; ny = int((m.ymax-m.ymin)/5000.)+1
topodat = m.transform_scalar(topoin,lons,lats,nx,ny)
# plot image over map with imshow.
im = m.imshow(topodat,cm.GMT_haxby)
# draw coastlines and political boundaries.
m.drawcoastlines()
m.drawcountries()
m.drawstates()
# draw parallels and meridians.
# label on left and bottom of map.
parallels = np.arange(0.,80,15.)
m.drawparallels(parallels,labels=[1,0,0,0])
#m.drawparallels(np.array([50.,70]))
meridians = np.arange(10.,360.,30.)
m.drawmeridians(meridians,labels=[1,0,0,1])
# add colorbar
cb = m.colorbar(im,"right", size="5%", pad='2%')
我试过了(但这只是顶盒的尝试,但我希望两个盒子的整个区域都有阴影):
def draw_screen_poly( lats, lons, m):
x, y = m( lons, lats )
xy = zip(x,y)
poly = Polygon( xy, facecolor='red', alpha=0.4 )
plt.gca().add_patch(poly)
lats_box1 = np.linspace(71.2, 72.9, num=25)
lons_box1 = np.linspace(-176.5, -160, num=25)
#lats_box1 = [71.2,72.9,72.9,71.2]
#lons_box1 = [-160,-160,-176.5,-176.5]
#m = Basemap(projection='sinu',lon_0=0)
#m.drawcoastlines()
#m.drawmapboundary()
draw_screen_poly( lats_box1, lons_box1, m )
并且(这也是对顶部框的尝试):
x1,y1 = m(71.2,-176.5)
x2,y2 = m(72.9,-176.5)
x3,y3 = m(72.9,-160)
x4,y4 = m(71.2,-160)
p = Polygon([(x1,y1),(x2,y2),(x3,y3),(x4,y4)],facecolor='red',edgecolor='blue',linewidth=2)
plt.gca().add_patch(p)
plt.show()
非常感谢任何帮助,我已经坚持了很长一段时间