def _contour_args(self, filled, badmask, origin, extent, *args): if filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z, origin, extent) 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 = asarray(z) # Convert to native array format if necessary. if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7, filled, badmask) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg, filled, badmask) 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)) rx = ravel(x) ry = ravel(y) self.ax.set_xlim((min(rx), max(rx))) self.ax.set_ylim((min(ry), max(ry))) return (x, y, z, lev)
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 = 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) 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 get_xyz_where(Z, Cond): """ Z and Cond are MxN matrices. Z are data and Cond is a boolean matrix where some condition is satisfied. Return value is x,y,z where x and y are the indices into Z and z are the values of Z at those indices. x,y,z are 1D arrays """ M, N = Z.shape z = ravel(Z) ind = nonzero(ravel(Cond)) x = arange(M) x.shape = M, 1 X = repeat(x, N, 1) x = ravel(X) y = arange(N) y.shape = 1, N Y = repeat(y, M) y = ravel(Y) x = take(x, ind) y = take(y, ind) z = take(z, ind) return x, y, z
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 = 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) 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 _contour_args(self, filled, badmask, origin, extent, *args): if filled: fn = 'contourf' else: fn = 'contour' Nargs = len(args) if Nargs <= 2: z = args[0] x, y = self._initialize_x_y(z, origin, extent) 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 = asarray(z) # Convert to native array format if necessary. if Nargs == 1 or Nargs == 3: lev = self._autolev(z, 7, filled, badmask) else: # 2 or 4 args level_arg = args[-1] if type(level_arg) == int: lev = self._autolev(z, level_arg, filled, badmask) 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)) rx = ravel(x) ry = ravel(y) self.ax.set_xlim((min(rx), max(rx))) self.ax.set_ylim((min(ry), max(ry))) return (x, y, z, lev)
def colorbars(epsoutfile): x = (arange(50.0)-25)/2.0 y = (arange(50.0)-25)/2.0 r = sqrt(x[:,NewAxis]**2+y**2) z = 5.0*cos(r) colmap = ColMapper.ColorMapper("yellow-red", exponent=0.55, brightness=0.5) lut = colmap.generate_lut() c = pyx.canvas.canvas() g = pyxgraph(xlimits=(min(x), max(x)), ylimits=(min(y), max(y)), width=6, height=6) g.pyxplot("y(x)=sin(x)", style="p") # FIXME: can't do empty plots! g.pyxplotarray(z, colmap=lut) c.insert(g) minz = minvalue=min(ravel(z)) maxz = maxvalue=max(ravel(z)) # --- vertical bars dist = 1.2 for orientation, position in [("vertical", "right"), ("vertical", "middle"), ("vertical2", "middle")]: cb = pyxcolorbar(lut=lut, frame=g, pos=(dist, 0), orientation = orientation, position = position, minvalue = minz, maxvalue=maxz) # add a short note on the style: txt = orientation[0] if "2" in orientation: txt += "2" txt += ", "+position[0] cb.pyxlabel( (0.0, 1.3), txt, style=[pyx.text.halign.left]) c.insert(cb) dist = dist + 0.5 # horizontal ones: dist = -0.3 for orientation, position in [("horizontal", "middle"), ("horizontal2", "middle")]: cb = pyxcolorbar(lut=lut, frame=g, pos=(0.0, dist), orientation = orientation, position = position, minvalue = minz, maxvalue=maxz) # add a short note on the style: txt = orientation[0] if "2" in orientation: txt += "2" txt += ", "+position[0] cb.pyxlabel( (1.3, 0.5), txt, style=[pyx.text.halign.left]) c.insert(cb) dist = dist - 0.3 pyxsave(c, epsoutfile)
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 autoscale(self, A): if not self.scaled(): rval = ravel(A) if self.vmin is None: self.vmin = min(rval) if self.vmax is None: self.vmax = max(rval)
def hist(y, bins=10, normed=0): """ Return the histogram of y with bins equally sized bins. If bins is an array, use the bins. Return value is (n,x) where n is the count for each bin in x If normed is False, return the counts in the first element of the return tuple. If normed is True, return the probability density n/(len(y)*dbin) If y has rank>1, it will be raveled Credits: the Numeric 22 documentation """ y = asarray(y) if len(y.shape) > 1: y = ravel(y) if not iterable(bins): ymin, ymax = min(y), max(y) if ymin == ymax: ymin -= 0.5 ymax += 0.5 bins = linspace(ymin, ymax, bins) n = searchsorted(sort(y), bins) n = diff(concatenate([n, [len(y)]])) if normed: db = bins[1] - bins[0] return 1 / (len(y) * db) * n, bins else: return n, bins
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 = nxmin(rval) if vmax is None: vmax = nxmax(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 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 __call__(self, value): vmin = self.vmin vmax = self.vmax if type(value) in [IntType, FloatType]: vtype = 'scalar' val = array([value]) else: vtype = 'array' val = array(value) if vmin is None or vmax is None: rval = ravel(val) 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: val = where(val<vmin, vmin, val) val = where(val>vmax, vmax, val) result = divide(val-vmin, vmax-vmin) if vtype == 'scalar': result = result[0] return result
def hist(y, bins=10, normed=0): """ Return the histogram of y with bins equally sized bins. If bins is an array, use the bins. Return value is (n,x) where n is the count for each bin in x If normed is False, return the counts in the first element of the return tuple. If normed is True, return the probability density n/(len(y)*dbin) If y has rank>1, it will be raveled Credits: the Numeric 22 documentation """ y = asarray(y) if len(y.shape)>1: y = ravel(y) if not iterable(bins): ymin, ymax = min(y), max(y) if ymin==ymax: ymin -= 0.5 ymax += 0.5 bins = linspace(ymin, ymax, bins) n = searchsorted(sort(y), bins) n = diff(concatenate([n, [len(y)]])) if normed: db = bins[1]-bins[0] return 1/(len(y)*db)*n, bins else: return n, bins
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 sum_flat(a): """Return the sum of all the elements of a, flattened out. It uses a.flat, and if a is not contiguous, a call to ravel(a) is made.""" if a.iscontiguous(): return asum(a.flat) else: return asum(ravel(a))
def set_data(self, x, y): try: del self._xc, self._yc except AttributeError: pass 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) 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)
def sum_flat(a): """Return the sum of all the elements of a, flattened out. It uses a.flat, and if a is not contiguous, a call to ravel(a) is made.""" if iscontiguous(a): return asum(a.flat) else: return asum(ravel(a))
def set_data(self, x, y): try: del self._xc, self._yc except AttributeError: pass 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) 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)
def array_example2(epsoutfile): x = (arange(50.0)-25)/2.0 y = (arange(50.0)-25)/2.0 r = sqrt(x[:,NewAxis]**2+y**2) z = 5.0*cos(r) colmap = ColMapper.ColorMapper("yellow-red", exponent=0.55, brightness=0.5) lut = colmap.generate_lut() c = pyx.canvas.canvas() g = pyxgraph(xlimits=(min(x), max(x)), ylimits=(min(y), max(y)), width=6, height=6) g.pyxplot("y(x)=sin(x)", style="p") # FIXME: can't do empty plots! g.pyxplotarray(z, colmap=lut) c.insert(g) cb = pyxcolorbar(lut=lut, frame=g, pos=(1.1,0.0), minvalue=min(ravel(z)), maxvalue=max(ravel(z))) c.insert(cb) pyxsave(c, epsoutfile)
def get_xyz_where(Z, Cond): """ Z and Cond are MxN matrices. Z are data and Cond is a boolean matrix where some condition is satisfied. Return value is x,y,z where x and y are the indices into Z and z are the values of Z at those indices. x,y,z are 1D arrays """ M,N = Z.shape z = ravel(Z) ind = nonzero( ravel(Cond) ) x = arange(M); x.shape = M,1 X = repeat(x, N, 1) x = ravel(X) y = arange(N); y.shape = 1,N Y = repeat(y, M) y = ravel(Y) x = take(x, ind) y = take(y, ind) z = take(z, ind) return x,y,z
def prctile(x, p = (0.0, 25.0, 50.0, 75.0, 100.0)): """ Return the percentiles of x. p can either be a sequence of percentil values or a scalar. If p is a sequence the i-th element of the return sequence is the p(i)-th percentile of x """ x = sort(ravel(x)) Nx = len(x) if not iterable(p): return x[int(p*Nx/100.0)] p = multiply(array(p), Nx/100.0) ind = p.astype(Int) ind = where(ind>=Nx, Nx-1, ind) return take(x, ind)
def prctile(x, p=(0.0, 25.0, 50.0, 75.0, 100.0)): """ Return the percentiles of x. p can either be a sequence of percentil values or a scalar. If p is a sequence the i-th element of the return sequence is the p(i)-th percentile of x """ x = sort(ravel(x)) Nx = len(x) if not iterable(p): return x[int(p * Nx / 100.0)] p = multiply(array(p), Nx / 100.0) ind = p.astype(Int) ind = where(ind >= Nx, Nx - 1, ind) return take(x, ind)
def array_example3(epsoutfile): x = (arange(200.0) - 100) / 10.0 y = (arange(200.0) - 100) / 10.0 r = sqrt(x[:, NewAxis] ** 2 + y ** 2) z = 5.0 * cos(r) colmap1 = ColMapper.ColorMapper("red") colmap1.exponent = 0.9 colmap1.invert = True colmap2 = ColMapper.ColorMapper("green") colmap2.exponent = 0.9 colmap3 = ColMapper.ColorMapper("green") colmap3.invert = True colmap3.exponent = 0.9 colmap4 = ColMapper.ColorMapper("blue") colmap4.exponent = 0.9 colmap = ColMapper.SegmentedColorMapping( [(-5.0, -2.5, colmap1), (-2.5, 0.0, colmap2), (0.0, 2.5, colmap3), (2.5, 5.0, colmap4)], -5.0, 5.0 ) # colmap = ColMapper.example_SegmentedColorMapping(min(ravel(z)),max(ravel(z))) lut = colmap.generate_lut() pilbitmap = ColMapper.Array2PIL(z, lut=lut) c = pyx.canvas.canvas() g = pyxgraph(xlimits=(min(x), max(x)), ylimits=(min(y), max(y)), width=6, height=6, key=False) g.pyxplot("y(x)=sin(x)+20", style="p") # FIXME: can't do empty plots! g.pyxbitmap(pilbitmap) c.insert(g) cb = pyxcolorbar(lut=lut, frame=g, pos=(1.1, 0.0), minvalue=min(ravel(z)), maxvalue=max(ravel(z))) c.insert(cb) pyxsave(c, epsoutfile)