鉴于我有三个矩阵描述了我想要绘制的数据:
使用python和底图绘制此类数据的首选方法是什么。
对于伪彩色数据,使用pcolormesh方法非常简单:
数据 - 带[n_lons,n_lats]的二维矩阵
m =底图(...)
m.pcolormesh(LONS,拉特,数据,latlon =真)
从阅读文档中,我觉得在这种情况下应该使用imshow命令,但对于这种方法,需要定期网格化数据,我必须重新编译和插入我的数据。
有没有其他方法可以绘制数据?
答案 0 :(得分:2)
我前一段时间遇到过同样的问题,这是我能提出的唯一解决方案:
(请注意,这适用于matplotlib 1.3.0,但不是1.1.0 )
from mpl_toolkits.basemap import Basemap
import numpy.ma as ma
import numpy as np
m = Basemap() #Define your map projection here
cornerLats=getCorners(lat);cornerLons=getCorners(lon)
xCorners,yCorners=m(cornerLats,cornerLons,inverse=True)
var=ma.masked_where(np.isnan(var),var)
colorTuple=tuple(np.array([var[:,:,0].flatten(),var[:,:,1].flatten(),var[:,:,2].flatten()]).transpose().tolist())
m.pcolormesh(xCorners,yCorners,var[:,:,0],color=colorTuple,clip_on=True,linewidth=0.05)
def getCorners(centers):
one = centers[:-1,:]
two = centers[1:,:]
d1 = (two - one) / 2.
one = one - d1
two = two + d1
stepOne = np.zeros((centers.shape[0] + 1,centers.shape[1]))
stepOne[:-2,:] = one
stepOne[-2:,:] = two[-2:,:]
one = stepOne[:,:-1]
two = stepOne[:,1:]
d2 = (two - one) / 2.
one = one - d2
two = two + d2
stepTwo = np.zeros((centers.shape[0] + 1,centers.shape[1] + 1))
stepTwo[:,:-2] = one
stepTwo[:,-2:] = two[:,-2:]
return stepTwo