How to build an irregular RGB image using python and a base map?

Given that I have three matrices that describe the data I want to build:

  • lons - 2D matrix with [n_lons, n_lats]
  • lats - 2D matrix with [n_lons, n_lats]
  • dataRGB - three-dimensional matrix with [n_lons, n_lats, 3]

What is the preferred way to build such data using python and baseemap.

For pseudo-color data, this is quite simple using the pcolormesh method:

  • - 2D matrix with [n_lons, n_lats]

    m = Basemap (...)

    m.pcolormesh (Lon, lat, data, LatLon = True)

From reading the documentation, it seems to me that in this case you should use the imshow command, but this method requires regular data with a binding to the grid, and I will have to redraw and interpolate my data.

Is there any other way to build data?

+2
source share
1 answer

I ran into this problem some time ago, and this is the only solution I could come up with:

(Note that this works with matplotlib 1.3.0 , but not with 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 

Assuming var is your variable of interest (NxMx3), lats is (N) x (M) and lons (N) x (M):

We need to convert the pixel centers lat / lons to the pixel angles lat / lons (N + 1) x (M + 1)

 cornerLats=getCorners(lat);cornerLons=getCorners(lon) 

Get coordinate angles

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

Mask of invalid data

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

We need a flattened tuple (N * M, 3) to go to pcolormesh

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

Setting a larger line width will result in more distortion of the edge and

a smaller line width will result in a twisted image for some reason.

 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 
+2
source

Source: https://habr.com/ru/post/1263653/


All Articles