Best way to plot grid point values on a map?

Simon Kammerer wrote:

Hi list,

what's the best (meaning most efficient/fastest) way to plot grid point values on a map created with basemap?

I'd like to plot the raw values of my data-array to the correspondig gridpoints, instead of having it transformed to something like contour or contourf. The ne plus ultra would be the ability to assing a colormap, to control the font color of the plotted values...


Simon: I don't know about efficiency, but this does what you want:

from pylab import show, title, arange, figure, title, arccos, pi, cm, text, sqrt
from matplotlib.colors import rgb2hex
from matplotlib.toolkits.basemap import Basemap
from matplotlib.numerix.random_array import uniform

# Plot a bunch of randomly distributed points on the earth.

# set up stereographic map centered on N. Pole.
m = Basemap(lon_0=-105,boundinglat=30.,resolution='l',
# number of points to plot.
npts = 300
# generate random points on a sphere,
# so that every small area on the sphere is expected
# to have the same number of points.
# Sphere Point Picking -- from Wolfram MathWorld
try: # this works for numpy
  u = uniform(0.,1.,size=npts)
  v = uniform(0.,1.,size=npts)
  z = uniform(0,100,size=npts)
except: # this works for Numeric/numarray
  u = uniform(0.,1.,shape=npts)
  v = uniform(0.,1.,shape=npts)
  z = uniform(0,100,shape=npts)
lons = 360.*u
lats = (180./pi)*arccos(2*v-1) - 90.
# transform lons and lats to map coordinates.
x,y = m(lons,lats)
# create a list of strings containing z values
zn = [ '%2i' % zz for zz in z ]
# plot numbers on map, colored by value.
vmin = 0; vmax = 100
cmap = cm.jet # use 'jet' colormap
for name,zval,xpt,ypt in zip(zn,z,x,y):
  # only plot values inside map region.
  if xpt > m.xmin and xpt < m.xmax and ypt > m.ymin and ypt < m.ymax:
      rgbcolor = cmap(1.-(zval-vmin)/(vmax-vmin))[:3]
      hexcolor = rgb2hex(rgbcolor)
# draw coasts and fill continents.
# draw parallels and meridians.
delat = 20.
circles = arange(0.,90.,delat).tolist()+\
delon = 45.
meridians = arange(0,360,delon)
title('Random Data Value at Random Points',y=1.075)




Jeffrey S. Whitaker Phone : (303)497-6313
NOAA/OAR/CDC R/PSD1 FAX : (303)497-6449
325 Broadway Boulder, CO, USA 80305-3328