Beispiel #1
0
def plotcoast(fn, pxlim, pylim, col='k'):

    coasts = tabarray.tabarray(fn, comchar='s')  # Read two columns from file
    for segment in coasts.segments:
        coastseg = coasts[segment].T
        xw = coastseg[1]
        yw = coastseg[0]  # First one appears to be Latitude
        xs = xw
        ys = yw  # Reset lists which store valid pos.
        if 1:
            # Mask arrays if outside plot box
            xp, yp = annim.projection.topixel(
                (numpy.array(xs), numpy.array(ys)))
            xp = numpy.ma.masked_where(
                numpy.isnan(xp) | (xp > pxlim[1]) | (xp < pxlim[0]), xp)
            yp = numpy.ma.masked_where(
                numpy.isnan(yp) | (yp > pylim[1]) | (yp < pylim[0]), yp)
            # Mask array could be of type numpy.bool_ instead of numpy.ndarray
            if numpy.isscalar(xp.mask):
                xp.mask = numpy.array(xp.mask, 'bool')
            if numpy.isscalar(yp.mask):
                yp.mask = numpy.array(yp.mask, 'bool')
            # Count the number of positions in these list that are inside the box
            j = 0
            for i in range(len(xp)):
                if not xp.mask[i] and not yp.mask[i]:
                    j += 1
            if j > 200:  # Threshold to prevent too much detail and big pdf's
                frame.plot(xp.data, yp.data, color=col)
Beispiel #2
0
def plotcoast(fn, pxlim, pylim, col='k'):
   
   coasts = tabarray.tabarray(fn, comchar='s')  # Read two columns from file
   for segment in coasts.segments:
      coastseg = coasts[segment].T
      xw = coastseg[1]; yw = coastseg[0]        # First one appears to be Latitude
      xs = xw; ys = yw                          # Reset lists which store valid pos.
      if 1:  
        # Mask arrays if outside plot box
        xp, yp = annim.projection.topixel((numpy.array(xs),numpy.array(ys)))
        xp = numpy.ma.masked_where(numpy.isnan(xp) | 
                           (xp > pxlim[1]) | (xp < pxlim[0]), xp)
        yp = numpy.ma.masked_where(numpy.isnan(yp) | 
                           (yp > pylim[1]) | (yp < pylim[0]), yp)
        # Mask array could be of type numpy.bool_ instead of numpy.ndarray
        if numpy.isscalar(xp.mask):
           xp.mask = numpy.array(xp.mask, 'bool')
        if numpy.isscalar(yp.mask):
           yp.mask = numpy.array(yp.mask, 'bool')
        # Count the number of positions in these list that are inside the box
        j = 0
        for i in range(len(xp)):
           if not xp.mask[i] and not yp.mask[i]:
              j += 1
        if j > 200:   # Threshold to prevent too much detail and big pdf's
           frame.plot(xp.data, yp.data, color=col)     
Beispiel #3
0
def plotcoast(fn, frame, grat, col='k', lim=100, decim=5, plotsym=None, sign=1.0):
   coasts = tabarray.tabarray(fn, comchar='s')  # Read two columns from file
   for segment in coasts.segments:
      coastseg = coasts[segment].T
      xw = sign * coastseg[1]; yw = coastseg[0] # First one appears to be Latitude
      xs = xw; ys = yw                          # Reset lists which store valid pos.
      if 1:
         # Mask arrays if outside plot box
         xp, yp = grat.gmap.topixel((numpy.array(xs),numpy.array(ys)))
         # Be sure you understand 
         # the operator precedence: (a > 2) | (a < 5) is the proper syntax 
         # because a > 2 | a < 5 will result in an error due to the fact 
         # that 2 | a is evaluated first.
         xp = numpy.ma.masked_where(numpy.isnan(xp) |
                              (xp > grat.pxlim[1]) | (xp < grat.pxlim[0]), xp)
         yp = numpy.ma.masked_where(numpy.isnan(yp) |
                              (yp > grat.pylim[1]) | (yp < grat.pylim[0]), yp)
         # Mask array could be of type numpy.bool_ instead of numpy.ndarray
         if numpy.isscalar(xp.mask):
            xp.mask = numpy.array(xp.mask, 'bool')
         if numpy.isscalar(yp.mask):
            yp.mask = numpy.array(yp.mask, 'bool')
         # Count the number of positions in this list that are inside the box
         xdc = []; ydc = []
         for i in range(len(xp)):
            if not xp.mask[i] and not yp.mask[i]:
               if not i%decim:
                  xdc.append(xp.data[i])
                  ydc.append(yp.data[i])
         if len(xdc) >= lim:
            if plotsym == None:
               frame.plot(xdc, ydc, color=col)
            else:
               frame.plot(xdc, ydc, '.', markersize=1, color=col)
Beispiel #4
0
def plotcoast(fn, frame, grat, col='k', lim=100, decim=5, plotsym=None, sign=1.0):
   coasts = tabarray.tabarray(fn, comchar='s')  # Read two columns from file
   for segment in coasts.segments:
      coastseg = coasts[segment].T
      xw = sign * coastseg[1]; yw = coastseg[0] # First one appears to be Latitude
      xs = xw; ys = yw                          # Reset lists which store valid pos.
      if 1:
         # Mask arrays if outside plot box
         xp, yp = grat.gmap.topixel((numpy.array(xs),numpy.array(ys)))
         # Be sure you understand 
         # the operator precedence: (a > 2) | (a < 5) is the proper syntax 
         # because a > 2 | a < 5 will result in an error due to the fact 
         # that 2 | a is evaluated first.
         xp = numpy.ma.masked_where(numpy.isnan(xp) |
                              (xp > grat.pxlim[1]) | (xp < grat.pxlim[0]), xp)
         yp = numpy.ma.masked_where(numpy.isnan(yp) |
                              (yp > grat.pylim[1]) | (yp < grat.pylim[0]), yp)
         # Mask array could be of type numpy.bool_ instead of numpy.ndarray
         if numpy.isscalar(xp.mask):
            xp.mask = numpy.array(xp.mask, 'bool')
         if numpy.isscalar(yp.mask):
            yp.mask = numpy.array(yp.mask, 'bool')
         # Count the number of positions in this list that are inside the box
         xdc = []; ydc = []
         for i in range(len(xp)):
            if not xp.mask[i] and not yp.mask[i]:
               if not i%decim:
                  xdc.append(xp.data[i])
                  ydc.append(yp.data[i])
         if len(xdc) >= lim:
            if plotsym == None:
               frame.plot(xdc, ydc, color=col)
            else:
               frame.plot(xdc, ydc, '.', markersize=1, color=col)
Beispiel #5
0
 def set_source(self, source):
    """
    Define an alternative source for the colormap.
    *source* can be any other matplotlib colormap object or its registered
    name, a NumPy array with one RGB triplet per row or the name of a textfile
    with one RGB triplet per line. Values should be between 0.0 and 1.0.
    """
    self.source = source
    try:
       source = cm.get_cmap(source)
       if source is None:
          source = self.source                            # restore source
    except:
       pass
    if isinstance(source, Colormap):
       if not source._isinit:
          source._init()
       self.baselut = source._lut
       self.N = source.N
    elif isinstance(source, numpy.ndarray):
       ncolors = source.shape[0]
       self.baselut = numpy.ones((ncolors+3,4), numpy.float)
       self.baselut[:ncolors,:3] = source
       self.N = ncolors                   # may have changed
    else:
       try:
          colors = tabarray(source)
       except:
          colors = tabarray(package_dir + '/lut/' + source)
       ncolors = colors.shape[0]
       self.baselut = numpy.ones((ncolors+3,4), numpy.float)
       self.baselut[:ncolors,:3] = colors
       self.N = ncolors                   # may have changed
    self._i_under = self.N
    self._i_over  = self.N+1
    self._i_bad   = self.N+2
    self.worklut = self.baselut.copy()
    self._lut = self.worklut.copy()       # existing may be inadequate
    if self.bad_set:
       badcolor, badalpha = self.bad_val
       self.set_bad(badcolor, badalpha)   # restore bad
    if self.name is not None:             # existing VariableColormap object?
       self.set_scale(self.scale)
Beispiel #6
0
    yr = xt * sinP + yt * cosP
    return (xr / major)**2 + (yr / minor)**2


def residuals(p, data):
    """
   Note that the function calculates the height z of the 3d landscape
   of the function z = (x/maj)**2 + (y/min)**2.
   An ellipse is defined where z=1. The residuals we use
   for the least squares fit to minimize, is then 1-z
   """
    x, y = data
    return 1.0 - func(p, data)


x, y = tabarray('ellipse.dat').columns()  # Read from file
x += numpy.random.normal(0.0, 0.05, len(x))
y += numpy.random.normal(0.0, 0.05, len(y))
# This ellipse data was made using a function. The parameters
# that were used are:
# xc = 5.0; yc = 4.0; major = 10.0; minor = 3.0; ang = 60.0
# Compare these values with the result of the esimate function.
p0 = getestimates(x, y)

fitter = kmpfit.Fitter(residuals=residuals, data=(x, y))
fitter.fit(params0=p0)
print("\n========= Fit results ellipse model ==========")
print("Initial params:", fitter.params0)
print("Params:        ", fitter.params)
print("Iterations:    ", fitter.niter)
print("Function ev:   ", fitter.nfev)
   yt = y - y0    
   xr = xt * cosP - yt * sinP
   yr = xt * sinP + yt * cosP
   return (xr/major)**2 + (yr/minor)**2

def residuals(p, data):
   """
   Note that the function calculates the height z of the 3d landscape
   of the function z = (x/maj)**2 + (y/min)**2.
   An ellipse is defined where z=1. The residuals we use
   for the least squares fit to minimize, is then 1-z
   """
   x, y = data
   return 1.0 - func(p,data)

x,y = tabarray('ellipse.dat').columns()  # Read from file
x += numpy.random.normal(0.0, 0.05, len(x))
y += numpy.random.normal(0.0, 0.05, len(y))
# This ellipse data was made using a function. The parameters 
# that were used are:
# xc = 5.0; yc = 4.0; major = 10.0; minor = 3.0; ang = 60.0
# Compare these values with the result of the esimate function.
p0 = getestimates(x, y)

fitter = kmpfit.Fitter(residuals=residuals, data=(x,y))
fitter.fit(params0=p0)
print("\n========= Fit results ellipse model ==========")
print("Initial params:", fitter.params0)
print("Params:        ", fitter.params)
print("Iterations:    ", fitter.niter)
print("Function ev:   ", fitter.nfev)