def print_label(self, linecontour, labelwidth): "if contours are too short, don't plot a label" lcsize = len(linecontour) if lcsize > 10 * labelwidth: return 1 xmax = amax(array(linecontour)[:, 0]) xmin = amin(array(linecontour)[:, 0]) ymax = amax(array(linecontour)[:, 1]) ymin = amin(array(linecontour)[:, 1]) lw = labelwidth if (xmax - xmin) > 1.2 * lw or (ymax - ymin) > 1.2 * lw: return 1 else: return 0
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 = asarray(value) if vmin is None or vmax is None: rval = ravel(val) if vmin is None: vmin = amin(rval) if vmax is None: vmax = amax(rval) if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin == vmax: return 0.0 * value else: val = where(val < vmin, vmin, val) val = where(val > vmax, vmax, val) result = (1.0 / (vmax - vmin)) * (val - vmin) if vtype == "scalar": result = result[0] return result
def autoscale(self, A): if not self.scaled(): rval = ravel(A) if self.vmin is None: self.vmin = amin(rval) if self.vmax is None: self.vmax = amax(rval)
def print_label(self, linecontour,labelwidth): "if contours are too short, don't plot a label" lcsize = len(linecontour) if lcsize > 10 * labelwidth: return 1 xmax = amax(array(linecontour)[:,0]) xmin = amin(array(linecontour)[:,0]) ymax = amax(array(linecontour)[:,1]) ymin = amin(array(linecontour)[:,1]) lw = labelwidth if (xmax - xmin) > 1.2* lw or (ymax - ymin) > 1.2 * lw: return 1 else: return 0
def _process_colors(self): """ Color argument processing for contouring. Note that we base the color mapping on the contour levels, not on the actual range of the Z values. This means we don't have to worry about bad values in Z, and we always have the full dynamic range available for the selected levels. The color is based on the midpoint of the layer, except for the end layers when clip_ends is True. """ self.monochrome = self.cmap.monochrome if self.colors is not None: self.cvalues = range(len(self.layers)) self.set_norm(no_norm()) else: self.cvalues = self.layers if self.filled and len(self.layers) > 2 and self.clip_ends: vmin = 2 * self.levels[1] - self.levels[2] vmax = 2 * self.levels[-2] - self.levels[-3] else: vmin = amin(self.levels) # alternative would be self.layers vmax = amax(self.levels) self.set_clim(vmin, vmax) self.set_array(self.layers) self.tcolors = [ (tuple(rgba),) for rgba in self.to_rgba(self.cvalues)]
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 = 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 = ravel(val) if vmin is None: vmin = amin(rval) if vmax is None: vmax = amax(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 = clip(val, vmin, vmax) result = (1.0 / (vmax - vmin)) * (val - vmin) 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 = asarray(value) if vmin is None or vmax is None: rval = ravel(val) if vmin is None: vmin = amin(rval) if vmax is None: vmax = amax(rval) if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin == vmax: return 0. * value else: val = where(val < vmin, vmin, val) val = where(val > vmax, vmax, val) result = (1.0 / (vmax - vmin)) * (val - vmin) 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 = 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 = ravel(val) if vmin is None: vmin = amin(rval) if vmax is None: vmax = amax(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 = clip(val,vmin, vmax) result = (1.0/(vmax-vmin))*(val-vmin) if vtype == 'scalar': result = result[0] return result
def _contour_args(self, *args): if self.filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z) elif Nargs <=4: x,y,z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn,fn)) z = ma.asarray(z) # Convert to native masked array format if necessary. self.zmax = ma.maximum(z) self.zmin = ma.minimum(z) self._auto = False if self.levels is None: if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError("Last %s arg must give levels; see help(%s)" % (fn,fn)) if self.filled and len(lev) < 2: raise ValueError("Filled contours require at least 2 levels.") # Workaround for cntr.c bug wrt masked interior regions: #if filled: # z = ma.masked_array(z.filled(-1e38)) # It's not clear this is any better than the original bug. self.levels = lev #if self._auto and self.extend in ('both', 'min', 'max'): # raise TypeError("Auto level selection is inconsistent " # + "with use of 'extend' kwarg") self._levels = list(self.levels) if self.extend in ('both', 'min'): self._levels.insert(0, self.zmin - 1) if self.extend in ('both', 'max'): self._levels.append(self.zmax + 1) self._levels = asarray(self._levels) self.vmin = amin(self.levels) # alternative would be self.layers self.vmax = amax(self.levels) if self.extend in ('both', 'min') or self.clip_ends: self.vmin = 2 * self.levels[0] - self.levels[1] if self.extend in ('both', 'max') or self.clip_ends: self.vmax = 2 * self.levels[-1] - self.levels[-2] self.layers = self._levels # contour: a line is a thin layer if self.filled: self.layers = 0.5 * (self._levels[:-1] + self._levels[1:]) if self.extend in ('both', 'min') or self.clip_ends: self.layers[0] = 0.5 * (self.vmin + self._levels[1]) if self.extend in ('both', 'max') or self.clip_ends: self.layers[-1] = 0.5 * (self.vmax + self._levels[-2]) return (x, y, z)
def _contour_args(self, *args): if self.filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z) elif Nargs <= 4: x, y, z = self._check_xyz(args[:3]) else: raise TypeError("Too many arguments to %s; see help(%s)" % (fn, fn)) z = ma.asarray( z) # Convert to native masked array format if necessary. self.zmax = ma.maximum(z) self.zmin = ma.minimum(z) self._auto = False if self.levels is None: if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg) elif iterable(level_arg) and len(shape(level_arg)) == 1: lev = array([float(fl) for fl in level_arg]) else: raise TypeError( "Last %s arg must give levels; see help(%s)" % (fn, fn)) if self.filled and len(lev) < 2: raise ValueError("Filled contours require at least 2 levels.") # Workaround for cntr.c bug wrt masked interior regions: #if filled: # z = ma.masked_array(z.filled(-1e38)) # It's not clear this is any better than the original bug. self.levels = lev #if self._auto and self.extend in ('both', 'min', 'max'): # raise TypeError("Auto level selection is inconsistent " # + "with use of 'extend' kwarg") self._levels = list(self.levels) if self.extend in ('both', 'min'): self._levels.insert(0, self.zmin - 1) if self.extend in ('both', 'max'): self._levels.append(self.zmax + 1) self._levels = asarray(self._levels) self.vmin = amin(self.levels) # alternative would be self.layers self.vmax = amax(self.levels) if self.extend in ('both', 'min') or self.clip_ends: self.vmin = 2 * self.levels[0] - self.levels[1] if self.extend in ('both', 'max') or self.clip_ends: self.vmax = 2 * self.levels[-1] - self.levels[-2] self.layers = self._levels # contour: a line is a thin layer if self.filled: self.layers = 0.5 * (self._levels[:-1] + self._levels[1:]) if self.extend in ('both', 'min') or self.clip_ends: self.layers[0] = 0.5 * (self.vmin + self._levels[1]) if self.extend in ('both', 'max') or self.clip_ends: self.layers[-1] = 0.5 * (self.vmax + self._levels[-2]) return (x, y, z)