Quiver

From Liki

Jump to: navigation, search

/usr/lib/python2.4/site-lib/matplotlib/axes.py

[edit] NEW DEFINITION OF QUIVER

def quiver(self, U, V, *args, **kwargs ):
       """
       QUIVER( X, Y, U, V )
       QUIVER( U, V )
       QUIVER( X, Y, U, V, S)
       QUIVER( U, V, S )
       QUIVER( ..., color=None, width=1.0, cmap=None,norm=None )
       Make a vector plot (U, V) with arrows on a grid (X, Y)
       The optional arguments color and width are used to specify the color and width
       of the arrow. color can be an array of colors in which case the arrows can be
       colored according to another dataset.
       
       If cm is specied and color is None, the colormap is used to give a color
       according to the vector's length.
       
       If color is a scalar field, the colormap is used to map the scalar to a color
       If a colormap is specified and color is an array of color triplets, then the
       colormap is ignored
       width is a scalar that controls the width of the arrows
       if S is specified it is used to scale the vectors. Use S=0 to disable automatic
       scaling.
       If S!=0, vectors are scaled to fit within the grid and then are multiplied by S.
       
       """
       if not self._hold: self.cla()
       do_scale = True
       S = 1.0
       if len(args)==0:
           # ( U, V )
           U = asarray(U)
           V = asarray(V)
           X,Y = meshgrid( arange(U.shape[0]), arange(U.shape[1]) )
       elif len(args)==1:
           # ( U, V, S )
           U = asarray(U)
           V = asarray(V)
           X,Y = meshgrid( arange(U.shape[0]), arange(U.shape[1]) )
           S = float(args[0])
           do_scale = ( S != 0.0 )
       elif len(args)==2:
           # ( X, Y, U, V )
           X = asarray(U)
           Y = asarray(V)
           U = asarray(args[0])
           V = asarray(args[1])
       elif len(args)==3:
           # ( X, Y, U, V )
           X = asarray(U)
           Y = asarray(V)
           U = asarray(args[0])
           V = asarray(args[1])
           S = float(args[2])
           do_scale = ( S != 0.0 )
       assert U.shape == V.shape
       assert X.shape == Y.shape
       assert U.shape == X.shape
       
       arrows = []
       N = sqrt( U**2+V**2 )
       if do_scale:
           Nmax = maximum.reduce(maximum.reduce(N))
           U *= (S/Nmax)
           V *= (S/Nmax)
           N /= Nmax
           
       alpha = kwargs.get('alpha', 1.0)
       width = kwargs.get('width', 0.25)
       norm = kwargs.get('norm', None)
       cmap = kwargs.get('cmap', None)
       vmin = kwargs.get('vmin', None)
       vmax = kwargs.get('vmax', None)
       color = kwargs.get('color', None)
       shading = kwargs.get('shading', 'faceted')
       C = None
       I,J = U.shape
       if color is not None and not looks_like_color(color):
           clr = asarray(color)
           if clr.shape==U.shape:
               C = array([ clr[i,j] for i in xrange(I)  for j in xrange(J)])
           elif clr.shape == () and color:
               # a scalar (1, True,...)
               C = array([ N[i,j] for i in xrange(I)  for j in xrange(J)])
           else:
               color = (0.,0.,0.,1.)
       elif color is None:
           color = (0.,0.,0.,1.)
       else:
           color = colorConverter.to_rgba( color, alpha )


       arrows = [ Arrow(X[i,j],Y[i,j],U[i,j],V[i,j],0.1*S ).get_verts()
                  for i in xrange(I) for j in xrange(J) ]
       collection = PolyCollection(
           arrows,
           edgecolors = 'None',
           facecolors = (color,),
           antialiaseds = (0,),
           linewidths = (width,),
           )
       if C is not None:
           collection.set_array( C )
       else:
           collection.set_facecolor( (color,) )
       collection.set_cmap(cmap)
       collection.set_norm(norm)
       if norm is not None:
           collection.set_clim( vmin, vmax )
       self.add_collection( collection )
       lims = asarray(arrows)
       _max = maximum.reduce( maximum.reduce( lims ))
       _min = minimum.reduce( minimum.reduce( lims ))
       self.update_datalim( [ tuple(_min), tuple(_max) ] )
       self.autoscale_view()
       return arrows
               

[edit] OLD DEFINITION OF QUIVER

def quiver_classic(self, U, V, *args, **kwargs ):
       """
       QUIVER( X, Y, U, V )
       QUIVER( U, V )
       QUIVER( X, Y, U, V, S)
       QUIVER( U, V, S )
       QUIVER( ..., color=None, width=1.0, cmap=None, norm=None )
       Make a vector plot (U, V) with arrows on a grid (X, Y)
       If X and Y are not specified, U and V must be 2D arrays.  Equally spaced
       X and Y grids are then generated using the meshgrid command.
       color can be a color value or an array of colors, so that the arrows can be
       colored according to another dataset.  If cmap is specified and color is 'length',
       the colormap is used to give a color according to the vector's length.
       If color is a scalar field, the colormap is used to map the scalar to a color
       If a colormap is specified and color is an array of color triplets, then the
       colormap is ignored
       width is a scalar that controls the width of the arrows
       if S is specified it is used to scale the vectors. Use S=0 to disable automatic
       scaling.
       If S!=0, vectors are scaled to fit within the grid and then are multiplied by S.


       """
       msg = This version of quiver is obsolete and will be
       phased out; please use the new quiver.
       
       warnings.warn(msg, DeprecationWarning)
       if not self._hold: self.cla()
       do_scale = True
       S = 1.0
       if len(args)==0:
           # ( U, V )
           U = asarray(U)
           V = asarray(V)
           X,Y = meshgrid( arange(U.shape[1]), arange(U.shape[0]) )
       elif len(args)==1:
           # ( U, V, S )
           U = asarray(U)
           V = asarray(V)
           X,Y = meshgrid( arange(U.shape[1]), arange(U.shape[0]) )
           S = float(args[0])
           do_scale = ( S != 0.0 )
       elif len(args)==2:
           # ( X, Y, U, V )
           X = asarray(U)
           Y = asarray(V)
           U = asarray(args[0])
           V = asarray(args[1])
       elif len(args)==3:
           # ( X, Y, U, V )
           X = asarray(U)
           Y = asarray(V)
           U = asarray(args[0])
           V = asarray(args[1])
           S = float(args[2])
           do_scale = ( S != 0.0 )
       assert U.shape == V.shape
       assert X.shape == Y.shape
       assert U.shape == X.shape
       U = ravel(U)
       V = ravel(V)
       X = ravel(X)
       Y = ravel(Y)
       arrows = []
       N = sqrt( U**2+V**2 )
       if do_scale:
           Nmax = maximum.reduce(N) or 1 # account for div by zero
           U = U*(S/Nmax)
           V = V*(S/Nmax)
           N = N*Nmax
       alpha = popd(kwargs,'alpha', 1.0)
       width = popd(kwargs,'width', .5)
       norm = popd(kwargs,'norm', None)
       cmap = popd(kwargs,'cmap', None)
       vmin = popd(kwargs,'vmin', None)
       vmax = popd(kwargs,'vmax', None)
       color = popd(kwargs,'color', None)
       shading = popd(kwargs,'shading', 'faceted')
       if len(kwargs):
           raise TypeError, "quiver() got an unexpected keyword argument '%s'"%kwargs.keys()[0]
       C = None
       if color == 'length' or color is True:
           if color is True:
               warnings.warn(Use "color='length'",
               not "color=True", DeprecationWarning)
           C = N
       elif color is None:
           color = (0,0,0,1)
       else:
           clr = ravel(asarray(color))
           if clr.shape == U.shape:
               C = clr
       I = U.shape[0]
       #arrows = []
       #for i in xrange(I):
       #    arrows.append( FancyArrow(X[i],Y[i],U[i],V[i],0.1*S ).get_verts() )
        arrows = [FancyArrow(X[i],Y[i],U[i],V[i],0.1*S ).get_verts()
                                                    for i in xrange(I)]
       collection = PolyCollection(
           arrows,
           edgecolors = 'None',
           antialiaseds = (1,),
           linewidths = (width,),
           )
       if C is not None:
           collection.set_array( ravel(C) )
           collection.set_cmap(cmap)
           collection.set_norm(norm)
           if norm is not None:
               collection.set_clim( vmin, vmax )
       else:
           collection.set_facecolor(color)
       self.add_collection( collection )
       lims = asarray(arrows)
       _max = maximum.reduce( maximum.reduce( lims ))
       _min = minimum.reduce( minimum.reduce( lims ))
       self.update_datalim( [ tuple(_min), tuple(_max) ] )
       self.autoscale_view()
       return collection
Personal tools