How to plot an irregular spaced RGB image using python and basemap? -


given have 3 matrices describe data want plot:

  • lons - 2d matrix [n_lons,n_lats]
  • lats - 2d matrix [n_lons,n_lats]
  • datargb - 3d matrix [n_lons,n_lats,3]

what preferred way plot such data using python , basemap.

for pseudo-color data quite simple using pcolormesh method:

  • data - 2d matrix [n_lons,n_lats]

    m = basemap(...)

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

from reading documentation, seems me imshow command should used in case, method regularly gridded data needed , have regridd , interpolate data.

is there other way plot data?

i ran same issue awhile ago, , solution come with:

(note works matplotlib 1.3.0, but not 1.1.0)

from mpl_toolkits.basemap import basemap  import numpy.ma ma import numpy np  m = basemap() #define map projection here 

assuming var variable of interest (nxmx3),lats (n)x(m) , lons (n)x(m):

we need convert pixel center lat/lons pixel corner lat/lons (n+1)x(m+1)

cornerlats=getcorners(lat);cornerlons=getcorners(lon) 

get coordinate corners

xcorners,ycorners=m(cornerlats,cornerlons,inverse=true) 

mask data invalid

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

we need flattened tuple(n*m,3) pass pcolormesh

colortuple=tuple(np.array([var[:,:,0].flatten(),var[:,:,1].flatten(),var[:,:,2].flatten()]).transpose().tolist()) 

setting larger linewidth result in more edge distortion, , a

smaller linewidth result in screwed image reason.

m.pcolormesh(xcorners,ycorners,var[:,:,0],color=colortuple,clip_on=true,linewidth=0.05)  def getcorners(centers):      1 = centers[:-1,:]     2 = centers[1:,:]     d1 = (two - one) / 2.     1 = 1 - d1     2 = 2 + d1     stepone = np.zeros((centers.shape[0] + 1,centers.shape[1]))     stepone[:-2,:] = 1     stepone[-2:,:] = two[-2:,:]     1 = stepone[:,:-1]     2 = stepone[:,1:]     d2 = (two - one) / 2.     1 = 1 - d2     2 = 2 + d2     steptwo = np.zeros((centers.shape[0] + 1,centers.shape[1] + 1))     steptwo[:,:-2] = 1     steptwo[:,-2:] = two[:,-2:]     return steptwo 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -