如何使用Python和Basemap绘制不规则间距的RGB图像?

2024-04-29 16:23:39 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有三个矩阵来描述我要绘制的数据:

  • lons-具有[n_lons,n_lats]的二维矩阵
  • lats-具有[n_lons,n_lats]的2D矩阵
  • dataRGB-带[n_lons,n_lats,3]的3D矩阵

使用python和basemap绘制这些数据的首选方法是什么。在

对于伪彩色数据,使用pcolormesh方法非常简单:

  • 数据-二维矩阵

    m=底图(…)

    m.pcolormesh(lons,lats,data,latlon=True)

从阅读文档来看,在我看来,在这种情况下应该使用imshow命令,但是对于这种方法来说,需要有规则的网格数据,我必须重新划分和插值数据。在

有没有其他方法来绘制数据?在


Tags: 数据方法文档truedata绘制矩阵latlon
1条回答
网友
1楼 · 发布于 2024-04-29 16:23:39

不久前我遇到了同样的问题,这是我唯一能想到的解决办法:

(请注意,这适用于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

假设var是您感兴趣的变量(NxMx3),lats是(N)x(M),lons是(N)x(M):

我们需要将像素中心lat/lons转换为pixel corner lat/lons(N+1)x(M+1)

^{pr2}$

获取坐标角点

xCorners,yCorners=m(cornerLats,cornerLons,inverse=True)

屏蔽无效的数据

var=ma.masked_where(np.isnan(var),var)

我们需要一个平坦的元组(N*M,3)来传递给pcolormesh

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

相关问题 更多 >