我使用Matplotlib和底图在地图上绘制网格化数据。我使用此代码将pcolormesh方法与散点图进行比较:
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(width=12000000,height=9000000,
rsphere=(6378137.00,6356752.3142),\
resolution='l',area_thresh=1000.,projection='lcc',\
lat_1=projection['standard_parallel'][0],\
lat_2=projection['standard_parallel'][1],\
lat_0=projection['latitude_of_projection_origin'],\
lon_0=projection['longitude_of_central_meridian'])
x, y = m(lons, lats) # compute map proj coordinates.
# 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,20.)
m.drawparallels(parallels,labels=[1,0,0,1])
meridians = np.arange(10.,360.,30.)
m.drawmeridians(meridians,labels=[1,0,0,1])
#cs = m.pcolormesh(x,y,data)
cs = m.pcolormesh(x,y,data,shading='flat',cmap=plt.cm.rainbow)
cb = m.colorbar(cs,"right", size="5%", pad='2%', ticks=V[0::5])
m.scatter(x*data,y*data, marker='.', s=100, c='g')
ax.set_title(title)
plt.show(block=False)
我得到的情节如下:
请注意,lon-lat坐标对应于每个网格框的左下角。这是设计还是错误?我会按设计思考,但后来我看到的所有例子(http://matplotlib.org/basemap/users/examples.html)都没有提到它。我希望网格单元以lon lat坐标点为中心,它们本身位于不规则网格上(lons,lats变量是2d数组)。我如何实现这一目标?
NB。这里的数据变量只是一个对应于1或nans的掩码。
感谢。
已编辑:根据Tom的建议,如果我尝试使用contourf,我会得到以下图像(缩放约与第一张图像相同)。
它仍然没有很好地处理边缘,因为它无法在有限值和纳米之间绘制表面,因此存在许多缺失点。我想要渲染每个网格单元格。似乎imshow可以做我想要的,但这似乎只适用于常规(线性)网格。
答案 0 :(得分:1)
pcolormesh
将节点坐标作为X
和Y
参数。从append
(到pcolor
,但与pcolormesh
相同):
X
和Y
,如果给出,请指定彩色的(x, y)
坐标 四边形;C[i,j]
的四边形有以下角落:(X[i, j], Y[i, j]), (X[i, j+1], Y[i, j+1]), (X[i+1, j], Y[i+1, j]), (X[i+1, j+1], Y[i+1, j+1]).
理想情况下,X和Y的尺寸应大于C的尺寸;如果尺寸相同,则忽略C的最后一行和一列。
我建议对x
和y
中每个四边形点的x和y坐标进行平均,然后使用scatter
的坐标来平衡点。< / p>
答案 1 :(得分:0)
要计算2D中心网格点阵列的左下角,请使用以下平均例程:
# adjust xlon,xlat values so they represent corners of grid cells for mapping using pcolor
# calculate average between two points and reassign lat/lon pairs
# skip first row and column of data since there are no points outside of domain to average with
# define empty arrays for storing new lats, lons, and values
corner_lats=np.empty([len(xlat[:,0])-1, len(xlat[0,:])-1],float)
corner_lons=np.empty([len(xlat[:,0])-1, len(xlat[0,:])-1],float)
corner_values=np.zeros([len(xlat[:,0])-1, len(xlat[0,:])-1],float)
# go through each xlat and xlon array and calculate LL corners
for lat in range(1,len(xlat[:,0])-1):
for lon in range(1,len(xlat[0,:])-1):
corner_lats[lat,lon]=(xlat[lat,lon]+xlat[lat+1,lon])/2
corner_lons[lat,lon]=(xlon[lat,lon]+xlon[lat,lon+1])/2
corner_values[lat-1,lon-1]=data[lat,lon]
然后在映射时,使用新的角落,lons和值:
m.pcolor(corner_lons,corner_lats,corner_values,latlon=True)
答案 2 :(得分:0)
您可以手动调整lon和lat数据,将网格间距的1/2向下移动到左侧。然后当你绘制它时,原始的lons和lats将位于每个像素的中心。此代码还为lons和lats数组的每个维度添加了一个,因此它们比数据本身的维度大一个,documentation状态是理想的。
# Subtract 1/2 the grid size from both lon and lat arrays
lons = lons - dlon/2
lats = lats - dlat/2
# Add 1 grid spacing to the right column of lon array and concatenate it as an additional column to the right
lons = np.c_[ lons, lons[:,-1]+dlon ]
# Duplicate the bottom row of the lon array and concatenate it to the bottom
lons = np.r_[ lons, [lons[-1,:]] ]
# Duplicate the right-most column of lats array and concatenate it on the right
lats = np.c_[ lats, lats[:,-1] ]
# Add 1 grid spacing to the bottom row of lat array and concatenate it as an additional row below
lats = np.r_[ lats, [lats[-1,:]+dlat] ]
# Then plot as before
m.pcolormesh(lons, lats, data)