def __call__(self, scaledImageData, alpha=1): # assumes the data is already normalized (ignoring sentinels) # clip to be on the safe side rgbaValues = self.cmap(nx.clip(scaledImageData, 0.,1.)) #replace sentinel data with sentinel colors for sentinel,rgb in self.sentinels.items(): r,g,b = rgb rgbaValues[:,:,0] = nx.where(scaledImageData==sentinel, r, rgbaValues[:,:,0]) rgbaValues[:,:,1] = nx.where(scaledImageData==sentinel, g, rgbaValues[:,:,1]) rgbaValues[:,:,2] = nx.where(scaledImageData==sentinel, b, rgbaValues[:,:,2]) rgbaValues[:,:,3] = nx.where(scaledImageData==sentinel, alpha, rgbaValues[:,:,3]) return rgbaValues
def __call__(self, value): vmin = self.vmin vmax = self.vmax if type(value) in [IntType, FloatType]: vtype = 'scalar' val = array([value]) else: vtype = 'array' val = nx.asarray(value) # if both vmin is None and vmax is None, we'll automatically # norm the data to vmin/vmax of the actual data, so the # clipping step won't be needed. if vmin is None and vmax is None: needs_clipping = False else: needs_clipping = True if vmin is None or vmax is None: rval = nx.ravel(val) #do this if sentinels (values to ignore in data) if self.ignore: sortValues = nx.sort(rval) if vmin is None: # find the lowest non-sentinel value for thisVal in sortValues: if thisVal not in self.ignore: vmin = thisVal #vmin is the lowest non-sentinel value break else: vmin = 0. if vmax is None: for thisVal in sortValues[::-1]: if thisVal not in self.ignore: vmax = thisVal #vmax is the greatest non-sentinel value break else: vmax = 0. else: if vmin is None: vmin = min(rval) if vmax is None: vmax = max(rval) if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin == vmax: return 0. * value else: if needs_clipping: val = nx.clip(val, vmin, vmax) result = (1.0 / (vmax - vmin)) * (val - vmin) # replace sentinels with original (non-normalized) values for thisIgnore in self.ignore: result = nx.where(val == thisIgnore, thisIgnore, result) if vtype == 'scalar': result = result[0] return result
def __call__(self, value): vmin = self.vmin vmax = self.vmax if type(value) in [IntType, FloatType]: vtype = 'scalar' val = array([value]) else: vtype = 'array' val = nx.asarray(value) # if both vmin is None and vmax is None, we'll automatically # norm the data to vmin/vmax of the actual data, so the # clipping step won't be needed. if vmin is None and vmax is None: needs_clipping = False else: needs_clipping = True if vmin is None or vmax is None: rval = nx.ravel(val) #do this if sentinels (values to ignore in data) if self.ignore: sortValues=nx.sort(rval) if vmin is None: # find the lowest non-sentinel value for thisVal in sortValues: if thisVal not in self.ignore: vmin=thisVal #vmin is the lowest non-sentinel value break else: vmin=0. if vmax is None: for thisVal in sortValues[::-1]: if thisVal not in self.ignore: vmax=thisVal #vmax is the greatest non-sentinel value break else: vmax=0. else: if vmin is None: vmin = min(rval) if vmax is None: vmax = max(rval) if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin==vmax: return 0.*value else: if needs_clipping: val = nx.clip(val,vmin, vmax) result = (1.0/(vmax-vmin))*(val-vmin) # replace sentinels with original (non-normalized) values for thisIgnore in self.ignore: result = nx.where(val==thisIgnore,thisIgnore,result) if vtype == 'scalar': result = result[0] return result
def __call__(self, scaledImageData, alpha=1): # assumes the data is already normalized (ignoring sentinels) # clip to be on the safe side rgbaValues = self.cmap(nx.clip(scaledImageData, 0., 1.)) #replace sentinel data with sentinel colors for sentinel, rgb in self.sentinels.items(): r, g, b = rgb rgbaValues[:, :, 0] = nx.where(scaledImageData == sentinel, r, rgbaValues[:, :, 0]) rgbaValues[:, :, 1] = nx.where(scaledImageData == sentinel, g, rgbaValues[:, :, 1]) rgbaValues[:, :, 2] = nx.where(scaledImageData == sentinel, b, rgbaValues[:, :, 2]) rgbaValues[:, :, 3] = nx.where(scaledImageData == sentinel, alpha, rgbaValues[:, :, 3]) return rgbaValues
def _h_arrows(self, length): ''' length is in arrow width units ''' minsh = self.minshaft * self.headlength N = len(length) length = nx.reshape(length, (N,1)) x = nx.array([0, -self.headaxislength, -self.headlength, 0], nx.Float64) x = x + nx.array([0,1,1,1]) * length y = 0.5 * nx.array([1, 1, self.headwidth, 0], nx.Float64) y = nx.repeat(y[nx.newaxis,:], N) x0 = nx.array([0, minsh-self.headaxislength, minsh-self.headlength, minsh], nx.Float64) y0 = 0.5 * nx.array([1, 1, self.headwidth, 0], nx.Float64) ii = [0,1,2,3,2,1,0] X = nx.take(x, ii, 1) Y = nx.take(y, ii, 1) Y[:, 3:] *= -1 X0 = nx.take(x0, ii) Y0 = nx.take(y0, ii) Y0[3:] *= -1 shrink = length/minsh X0 = shrink * X0[nx.newaxis,:] Y0 = shrink * Y0[nx.newaxis,:] short = nx.repeat(length < minsh, 7, 1) #print 'short', length < minsh X = nx.where(short, X0, X) Y = nx.where(short, Y0, Y) if self.pivot[:3] == 'mid': X -= 0.5 * X[:,3, nx.newaxis] elif self.pivot[:3] == 'tip': X = X - X[:,3, nx.newaxis] #numpy bug? using -= does not # work here unless we multiply # by a float first, as with 'mid'. tooshort = length < self.minlength if nx.any(tooshort): th = nx.arange(0,7,1, nx.Float64) * (nx.pi/3.0) x1 = nx.cos(th) * self.minlength * 0.5 y1 = nx.sin(th) * self.minlength * 0.5 X1 = nx.repeat(x1[nx.newaxis, :], N, 0) Y1 = nx.repeat(y1[nx.newaxis, :], N, 0) tooshort = nx.repeat(tooshort, 7, 1) X = nx.where(tooshort, X1, X) Y = nx.where(tooshort, Y1, Y) return X, Y
def __draw_lines_hide(self, gc, x, y, transform=None): """ x and y are equal length arrays, draw lines connecting each point in x, y """ if debugPS: self._pswriter.write('% draw_lines \n') if transform: if transform.need_nonlinear(): x, y, mask = transform.nonlinear_only_numerix(x, y, returnMask=1) else: mask = ones(x.shape) vec6 = transform.as_vec6_val() a, b, c, d, tx, ty = vec6 sx, sy = get_vec6_scales(vec6) start = 0 end = 1000 points = zip(x, y) write = self._pswriter.write write('gsave\n') self.push_gc(gc) write('[%f %f %f %f %f %f] concat\n' % (a, b, c, d, tx, ty)) while start < len(x): # put moveto on all the bad data and on the first good # point after the bad data codes = where(mask[start:end + 1], 'l', 'm') ind = nonzero(mask[start:end + 1] == 0) + 1 if ind[-1] >= len(codes): ind = ind[:-1] put(codes, ind, 'm') thisx = x[start:end + 1] thisy = y[start:end + 1] to_draw = izip(thisx, thisy, codes) if not to_draw: break ps = ['%1.3f %1.3f m' % to_draw.next()[:2]] ps.extend(["%1.3f %1.3f %c" % tup for tup in to_draw]) # we don't want to scale the line width, etc so invert the # scale for the stroke ps.append('\ngsave %f %f scale stroke grestore\n' % (1. / sx, 1. / sy)) write('\n'.join(ps)) start = end end += 1000 write("grestore\n")
def __draw_lines_hide(self, gc, x, y, transform=None): """ x and y are equal length arrays, draw lines connecting each point in x, y """ if debugPS: self._pswriter.write('% draw_lines \n') if transform: if transform.need_nonlinear(): x, y, mask = transform.nonlinear_only_numerix(x, y, returnMask=1) else: mask = ones(x.shape) vec6 = transform.as_vec6_val() a,b,c,d,tx,ty = vec6 sx, sy = get_vec6_scales(vec6) start = 0 end = 1000 points = zip(x,y) write = self._pswriter.write write('gsave\n') self.push_gc(gc) write('[%f %f %f %f %f %f] concat\n'%(a,b,c,d,tx,ty)) while start < len(x): # put moveto on all the bad data and on the first good # point after the bad data codes = where(mask[start:end+1], 'l', 'm') ind = nonzero(mask[start:end+1]==0)+1 if ind[-1]>=len(codes): ind = ind[:-1] put(codes, ind, 'm') thisx = x[start:end+1] thisy = y[start:end+1] to_draw = izip(thisx, thisy, codes) if not to_draw: break ps = ['%1.3f %1.3f m' % to_draw.next()[:2]] ps.extend(["%1.3f %1.3f %c" % tup for tup in to_draw]) # we don't want to scale the line width, etc so invert the # scale for the stroke ps.append('\ngsave %f %f scale stroke grestore\n'%(1./sx,1./sy)) write('\n'.join(ps)) start = end end += 1000 write("grestore\n")
def _locate(self, x): """ Return the colorbar data coordinate(s) corresponding to the color value(s) in scalar or array x. Used for tick positioning. """ b = self._boundaries y = self._y N = len(b) ii = nx.minimum(nx.searchsorted(b, x), N - 1) isscalar = False if not iterable(ii): isscalar = True ii = nx.array((ii,)) i0 = nx.maximum(ii - 1, 0) # db = b[ii] - b[i0] db = nx.take(b, ii) - nx.take(b, i0) db = nx.where(i0 == ii, 1.0, db) # dy = y[ii] - y[i0] dy = nx.take(y, ii) - nx.take(y, i0) z = nx.take(y, i0) + (x - nx.take(b, i0)) * dy / db if isscalar: z = z[0] return z
def _locate(self, x): ''' Return the colorbar data coordinate(s) corresponding to the color value(s) in scalar or array x. Used for tick positioning. ''' b = self._boundaries y = self._y N = len(b) ii = nx.minimum(nx.searchsorted(b, x), N - 1) isscalar = False if not iterable(ii): isscalar = True ii = nx.array((ii, )) i0 = nx.maximum(ii - 1, 0) #db = b[ii] - b[i0] db = nx.take(b, ii) - nx.take(b, i0) db = nx.where(i0 == ii, 1.0, db) #dy = y[ii] - y[i0] dy = nx.take(y, ii) - nx.take(y, i0) z = nx.take(y, i0) + (x - nx.take(b, i0)) * dy / db if isscalar: z = z[0] return z
raise ImportError('this example requires numpy') import matplotlib.numerix.ma as MA import matplotlib.numerix as N from matplotlib.toolkits.basemap import Basemap # read in data from netCDF file. infile = 'ccsm_popgrid.nc' fpin = NetCDFFile(infile) tlat = fpin.variables['TLAT'][:] tlon = fpin.variables['TLONG'][:] temp = fpin.variables['TEMP'][:] fillvalue = fpin.variables['TEMP'].attributes['_FillValue'] fpin.close() # make longitudes monotonically increasing. tlon = N.where(N.greater_equal(tlon,min(tlon[:,0])),tlon-360,tlon) # create a masked array with temperature data (continents masked). temp = MA.masked_values(temp,fillvalue) # stack grids side-by-side (in longitiudinal direction), so # any range of longitudes may be plotted on a world map. tlon = N.concatenate((tlon,tlon+360),1) tlat = N.concatenate((tlat,tlat),1) temp = MA.concatenate((temp,temp),1) tlon = tlon-360. pl.figure(figsize=(8.5,11)) pl.subplot(2,1,1) # subplot 1 just shows POP grid cells. map = Basemap(projection='merc', lat_ts=20, llcrnrlon=-180, \
raise ImportError('this example requires numpy') import matplotlib.numerix.ma as MA import matplotlib.numerix as N from matplotlib.toolkits.basemap import Basemap # read in data from netCDF file. infile = 'ccsm_popgrid.nc' fpin = NetCDFFile(infile) tlat = fpin.variables['TLAT'][:] tlon = fpin.variables['TLONG'][:] temp = fpin.variables['TEMP'][:] fillvalue = fpin.variables['TEMP'].attributes['_FillValue'] fpin.close() # make longitudes monotonically increasing. tlon = N.where(N.greater_equal(tlon, min(tlon[:, 0])), tlon - 360, tlon) # create a masked array with temperature data (continents masked). temp = MA.masked_values(temp, fillvalue) # stack grids side-by-side (in longitiudinal direction), so # any range of longitudes may be plotted on a world map. tlon = N.concatenate((tlon, tlon + 360), 1) tlat = N.concatenate((tlat, tlat), 1) temp = MA.concatenate((temp, temp), 1) tlon = tlon - 360. pl.figure(figsize=(8.5, 11)) pl.subplot(2, 1, 1) # subplot 1 just shows POP grid cells. map = Basemap(projection='merc', lat_ts=20, llcrnrlon=-180, \