def recache(self): #if self.axes is None: print 'recache no axes' #else: print 'recache units', self.axes.xaxis.units, self.axes.yaxis.units x = ma.asarray(self.convert_xunits(self._xorig), float) y = ma.asarray(self.convert_yunits(self._yorig), float) x = ma.ravel(x) y = ma.ravel(y) if len(x)==1 and len(y)>1: x = x * npy.ones(y.shape, float) if len(y)==1 and len(x)>1: y = y * npy.ones(x.shape, float) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not ma.nomask: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = npy.asarray(x, float) self._y = npy.asarray(y, float) self._logcache = None
def recache(self): #if self.axes is None: print 'recache no axes' #else: print 'recache units', self.axes.xaxis.units, self.axes.yaxis.units x = ma.asarray(self.convert_xunits(self._xorig), float) y = ma.asarray(self.convert_yunits(self._yorig), float) x = ma.ravel(x) y = ma.ravel(y) if len(x) == 1 and len(y) > 1: x = x * npy.ones(y.shape, float) if len(y) == 1 and len(x) > 1: y = y * npy.ones(x.shape, float) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not ma.nomask: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = npy.asarray(x, float) self._y = npy.asarray(y, float) self._logcache = None
def _update_x_y_logcache(self): # check cache need_update = False try: for key, var_id in self._cache_inputs.iteritems(): if (id(getattr(self, key)) != var_id): need_update = True break except: need_update = True if (not need_update): return # update cache for key in self._cache_inputs.keys(): try: self._cache_inputs[key] = id(getattr(self, key)) except: pass # make sure that result values exist and release # previous values self._cached__x = None self._cached__y = None self._cached__segments = None self._cached__logcache = None if (self.is_unitsmgr_set()): unitsmgr = self.get_unitsmgr() x, y = unitsmgr._convert_units((self._x_orig, self._xunits), (self._y_orig, self._yunits)) else: x, y = (self._x_orig, self._y_orig) x = ma.ravel(x) y = ma.ravel(y) if len(x) == 1 and len(y) > 1: x = x * ones(y.shape, Float) if len(y) == 1 and len(x) > 1: y = y * ones(x.shape, Float) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not ma.nomask: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._cached__segments = unmasked_index_ranges(mask) else: self._cached__segments = None self._cached__x = asarray(x, Float) self._cached__y = asarray(y, Float) self._cached__logcache = None
def _update_x_y_logcache(self): # check cache need_update = False try: for key, var_id in self._cache_inputs.iteritems(): if id(getattr(self, key)) != var_id: need_update = True break except: need_update = True if not need_update: return # update cache for key in self._cache_inputs.keys(): try: self._cache_inputs[key] = id(getattr(self, key)) except: pass # make sure that result values exist and release # previous values self._cached__x = None self._cached__y = None self._cached__segments = None self._cached__logcache = None if self.is_unitsmgr_set(): unitsmgr = self.get_unitsmgr() x, y = unitsmgr._convert_units((self._x_orig, self._xunits), (self._y_orig, self._yunits)) else: x, y = (self._x_orig, self._y_orig) x = ma.ravel(x) y = ma.ravel(y) if len(x) == 1 and len(y) > 1: x = x * ones(y.shape, Float) if len(y) == 1 and len(x) > 1: y = y * ones(x.shape, Float) if len(x) != len(y): raise RuntimeError("xdata and ydata must be the same length") mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not ma.nomask: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._cached__segments = unmasked_index_ranges(mask) else: self._cached__segments = None self._cached__x = asarray(x, Float) self._cached__y = asarray(y, Float) self._cached__logcache = None
def set_data(self, *args): """ Set the x and y data ACCEPTS: (array xdata, array ydata) """ if len(args) == 1: x, y = args[0] else: x, y = args try: del self._xc, self._yc except AttributeError: pass self._masked_x = None self._masked_y = None mx = ma.getmask(x) my = ma.getmask(y) if mx is not None: mx = ravel(mx) self._masked_x = x if my is not None: my = ravel(my) self._masked_y = y mask = ma.mask_or(mx, my) if mask is not None: x = ma.masked_array(ma.ravel(x), mask=mask).compressed() y = ma.masked_array(ma.ravel(y), mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = asarray(x, Float) self._y = asarray(y, Float) if len(self._x.shape) > 1: self._x = ravel(self._x) if len(self._y.shape) > 1: self._y = ravel(self._y) # What is the rationale for the following two lines? # And why is there not a similar pair with _x and _y reversed? if len(self._y) == 1 and len(self._x) > 1: self._y = self._y * ones(self._x.shape, Float) if len(self._x) != len(self._y): raise RuntimeError("xdata and ydata must be the same length") if self._useDataClipping: self._xsorted = self._is_sorted(self._x) self._logcache = None
def set_data(self, *args): """ Set the x and y data ACCEPTS: (array xdata, array ydata) """ if len(args) == 1: x, y = args[0] else: x, y = args try: del self._xc, self._yc except AttributeError: pass self._masked_x = None self._masked_y = None mx = ma.getmask(x) my = ma.getmask(y) if mx is not None: mx = ravel(mx) self._masked_x = x if my is not None: my = ravel(my) self._masked_y = y mask = ma.mask_or(mx, my) if mask is not None: x = ma.masked_array(ma.ravel(x), mask=mask).compressed() y = ma.masked_array(ma.ravel(y), mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = asarray(x, Float) self._y = asarray(y, Float) if len(self._x.shape) > 1: self._x = ravel(self._x) if len(self._y.shape) > 1: self._y = ravel(self._y) # What is the rationale for the following two lines? # And why is there not a similar pair with _x and _y reversed? if len(self._y) == 1 and len(self._x) > 1: self._y = self._y * ones(self._x.shape, Float) if len(self._x) != len(self._y): raise RuntimeError('xdata and ydata must be the same length') if self._useDataClipping: self._xsorted = self._is_sorted(self._x) self._logcache = None
def set_data(self, *args): """ Set the x and y data ACCEPTS: (array xdata, array ydata) """ if len(args)==1: x, y = args[0] else: x, y = args self._x_orig = x self._y_orig = y if (self._xunits and hasattr(x, 'convert_to')): x = x.convert_to(self._xunits).get_value() if (hasattr(x, 'get_value')): x = x.get_value() if (self._yunits and hasattr(y, 'convert_to')): y = y.convert_to(self._yunits).get_value() if (hasattr(y, 'get_value')): y = y.get_value() x = ma.ravel(x) y = ma.ravel(y) if len(x)==1 and len(y)>1: x = x * ones(y.shape, Float) if len(y)==1 and len(x)>1: y = y * ones(x.shape, Float) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not ma.nomask: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = asarray(x, Float) self._y = asarray(y, Float) self._logcache = None
def __call__(self, value, clip=None): if clip is None: clip = self.clip if isinstance(value, (int, float)): vtype = 'scalar' val = ma.array([value]) else: vtype = 'array' val = ma.asarray(value) self.autoscale(val) vmin, vmax = self.vmin, self.vmax if vmin > vmax: raise ValueError("minvalue must be less than or equal to maxvalue") elif vmin<=0: raise ValueError("values must all be positive") elif vmin==vmax: return 0.*value else: if clip: mask = ma.getmask(val) val = ma.array(nx.clip(val.filled(vmax), vmin, vmax), mask=mask) result = (ma.log(val)-nx.log(vmin))/(nx.log(vmax)-nx.log(vmin)) if vtype == 'scalar': result = result[0] return result
def set_data(self, *args): """ Set the x and y data ACCEPTS: (array xdata, array ydata) """ if len(args) == 1: x, y = args[0] else: x, y = args try: del self._xc, self._yc except AttributeError: pass self._x_orig = x self._y_orig = y x = ma.ravel(x) y = ma.ravel(y) if len(x) == 1 and len(y) > 1: x = x * ones(y.shape, Float) if len(y) == 1 and len(x) > 1: y = y * ones(x.shape, Float) if len(x) != len(y): raise RuntimeError("xdata and ydata must be the same length") mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not None: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = asarray(x, Float) self._y = asarray(y, Float) if self._useDataClipping: self._xsorted = self._is_sorted(self._x) self._logcache = None
def set_data(self, *args): """ Set the x and y data ACCEPTS: (array xdata, array ydata) """ if len(args) == 1: x, y = args[0] else: x, y = args try: del self._xc, self._yc except AttributeError: pass self._x_orig = x self._y_orig = y x = ma.ravel(x) y = ma.ravel(y) if len(x) == 1 and len(y) > 1: x = x * ones(y.shape, Float) if len(y) == 1 and len(x) > 1: y = y * ones(x.shape, Float) if len(x) != len(y): raise RuntimeError('xdata and ydata must be the same length') mx = ma.getmask(x) my = ma.getmask(y) mask = ma.mask_or(mx, my) if mask is not None: x = ma.masked_array(x, mask=mask).compressed() y = ma.masked_array(y, mask=mask).compressed() self._segments = unmasked_index_ranges(mask) else: self._segments = None self._x = asarray(x, Float) self._y = asarray(y, Float) if self._useDataClipping: self._xsorted = self._is_sorted(self._x) self._logcache = None
def __call__(self, X, alpha=1.0): """ X is either a scalar or an array (of any dimension). If scalar, a tuple of rgba values is returned, otherwise an array with the new shape = oldshape+(4,). If the X-values are integers, then they are used as indices into the array. If they are floating point, then they must be in the interval (0.0, 1.0). Alpha must be a scalar. """ if not self._isinit: self._init() alpha = min(alpha, 1.0) # alpha must be between 0 and 1 alpha = max(alpha, 0.0) self._lut[:-3, -1] = alpha mask_bad = None if isinstance(X, (int, float)): vtype = 'scalar' xa = array([X]) else: vtype = 'array' xma = ma.asarray(X) xa = xma.filled(0) mask_bad = ma.getmask(xma) if typecode(xa) in typecodes['Float']: xa = where(xa == 1.0, 0.9999999, xa) # Tweak so 1.0 is in range. xa = (xa * self.N).astype(Int) mask_under = xa < 0 mask_over = xa > self.N - 1 xa = where(mask_under, self._i_under, xa) xa = where(mask_over, self._i_over, xa) if mask_bad is not None: # and sometrue(mask_bad): xa = where(mask_bad, self._i_bad, xa) #print 'types', typecode(self._lut), typecode(xa), xa.shape rgba = take(self._lut, xa) if vtype == 'scalar': rgba = tuple(rgba[0, :]) #print rgba[0,1:10,:] # Now the same for numpy, numeric... return rgba
def __call__(self, X, alpha=1.0): """ X is either a scalar or an array (of any dimension). If scalar, a tuple of rgba values is returned, otherwise an array with the new shape = oldshape+(4,). If the X-values are integers, then they are used as indices into the array. If they are floating point, then they must be in the interval (0.0, 1.0). Alpha must be a scalar. """ if not self._isinit: self._init() alpha = min(alpha, 1.0) # alpha must be between 0 and 1 alpha = max(alpha, 0.0) self._lut[:-3, -1] = alpha mask_bad = None if isinstance(X, (int, float)): vtype = 'scalar' xa = array([X]) else: vtype = 'array' xma = ma.asarray(X) xa = xma.filled(0) mask_bad = ma.getmask(xma) if typecode(xa) in typecodes['Float']: xa = where(xa == 1.0, 0.9999999, xa) # Tweak so 1.0 is in range. xa = (xa * self.N).astype(Int) mask_under = xa < 0 mask_over = xa > self.N-1 xa = where(mask_under, self._i_under, xa) xa = where(mask_over, self._i_over, xa) if mask_bad is not None: # and sometrue(mask_bad): xa = where(mask_bad, self._i_bad, xa) #print 'types', typecode(self._lut), typecode(xa), xa.shape rgba = take(self._lut, xa) if vtype == 'scalar': rgba = tuple(rgba[0,:]) #print rgba[0,1:10,:] # Now the same for numpy, numeric... return rgba
def __call__(self, X, alpha=1.0): """ X is either a scalar or an array (of any dimension). If scalar, a tuple of rgba values is returned, otherwise an array with the new shape = oldshape+(4,). If the X-values are integers, then they are used as indices into the array. If they are floating point, then they must be in the interval (0.0, 1.0). Alpha must be a scalar. """ if not self._isinit: self._init() alpha = min(alpha, 1.0) # alpha must be between 0 and 1 alpha = max(alpha, 0.0) self._lut[:-3, -1] = alpha mask_bad = None if not iterable(X): vtype = 'scalar' xa = array([X]) else: vtype = 'array' xma = ma.asarray(X) xa = xma.filled(0) mask_bad = ma.getmask(xma) if typecode(xa) in typecodes['Float']: putmask(xa, xa==1.0, 0.9999999) #Treat 1.0 as slightly less than 1. xa = (xa * self.N).astype(Int) # Set the over-range indices before the under-range; # otherwise the under-range values get converted to over-range. putmask(xa, xa>self.N-1, self._i_over) putmask(xa, xa<0, self._i_under) if mask_bad is not None and mask_bad.shape == xa.shape: putmask(xa, mask_bad, self._i_bad) rgba = take(self._lut, xa) if vtype == 'scalar': rgba = tuple(rgba[0,:]) return rgba
def __init__(self, ax, *args, **kwargs): """ Draw contour lines or filled regions, depending on whether keyword arg 'filled' is False (default) or True. The first argument of the initializer must be an axes object. The remaining arguments and keyword arguments are described in ContourSet.contour_doc. """ self.ax = ax self.filled = kwargs.get('filled', False) self.linewidths = kwargs.get('linewidths', None) self.alpha = kwargs.get('alpha', 1.0) self.origin = kwargs.get('origin', None) self.extent = kwargs.get('extent', None) cmap = kwargs.get('cmap', None) self.colors = kwargs.get('colors', None) self.clip_ends = kwargs.get('clip_ends', True) self.antialiased = kwargs.get('antialiased', True) self.nchunk = kwargs.get('nchunk', 0) if self.origin is not None: assert(self.origin in ['lower', 'upper', 'image']) if self.extent is not None: assert(len(self.extent) == 4) if cmap is not None: assert(isinstance(cmap, Colormap)) if self.colors is not None and cmap is not None: raise ValueError('Either colors or cmap must be None') if self.origin == 'image': self.origin = rcParams['image.origin'] x, y, z = self._contour_args(*args) # also sets self.levels, # self.layers if self.colors is not None: cmap = ListedColormap(self.colors, N=len(self.layers)) if self.filled: self.collections = silent_list('PolyCollection') else: self.collections = silent_list('LineCollection') # label lists must be initialized here self.cl = [] self.cl_cvalues = [] ScalarMappable.__init__(self, cmap = cmap) # sets self.cmap; # default norm for now self._process_colors() if self.filled: if self.linewidths is None: self.linewidths = 0.05 # Good default for Postscript. if iterable(self.linewidths): self.linewidths = self.linewidths[0] #C = _contour.Cntr(x, y, z.filled(), z.mask()) C = _contour.Cntr(x, y, z.filled(), ma.getmask(z)) lowers = self.levels[:-1] uppers = self.levels[1:] for level, level_upper, color in zip(lowers, uppers, self.tcolors): nlist = C.trace(level, level_upper, points = 1, nchunk = self.nchunk) col = PolyCollection(nlist, linewidths = (self.linewidths,), antialiaseds = (self.antialiased,)) col.set_color(color) # sets both facecolor and edgecolor self.ax.add_collection(col) self.collections.append(col) else: tlinewidths = self._process_linewidths() #C = _contour.Cntr(x, y, z.filled(), z.mask()) C = _contour.Cntr(x, y, z.filled(), ma.getmask(z)) for level, color, width in zip(self.levels, self.tcolors, tlinewidths): nlist = C.trace(level, points = 1) col = LineCollection(nlist) col.set_color(color) col.set_linewidth(width) if level < 0.0 and self.monochrome: col.set_linestyle((0, (6.,6.)),) #print "setting dashed" col.set_label(str(level)) # only for self-documentation self.ax.add_collection(col) self.collections.append(col) ## check: seems like set_xlim should also be inside if not self.ax.ishold(): self.ax.cla() self.ax.set_xlim((ma.minimum(x), ma.maximum(x))) self.ax.set_ylim((ma.minimum(y), ma.maximum(y)))