ó
 m[c           @` s  d  Z  d d l m Z m Z m Z m Z d d l Z d d l m Z d d l	 Z
 d d l Z d d l j Z d d l j Z d d l j Z d d l j Z d d l j Z d g Z d d d d d d d d d d d d	 d
 d  Z d e f d     YZ d e f d     YZ d e f d     YZ d e f d     YZ  d e! f d     YZ" d e! f d     YZ# d   Z$ d   Z% d   Z& d   Z' d   Z( d S(   u,   
Streamline plotting for 2D vector fields.

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   xrangeu
   streamploti   u   -|>g¹?g      @u   bothc   5      C` sX  t  | |  } t |  } t | |  } | d k rE t j j } n  | d k r] |  j } n  | d k r{ |  j j	   } n  | d k r t
 j d } n  i  } t d | d d |
  } | d k rŪ d | } t |   n  | d k rō | d	 } n  t | t j  } | rH| j | j k r-t d
   n  g  } t j j |  } n | | d <| | d <t | t j  r| j | j k rt d   n  g  | d <n | | d <| | d <| | d <| | d <| j | j k sč| j | j k r÷t d   n  t j j |  } t j j |  } t | | | | | |  } g  } | d k rĒx³t | j  D]h \ } } | | | f d k rX| j | |  \ } } | | |  }  |  d k	 rĄ| j |   qĄqXqXWn7t j | d t j   }! x |! D]y \ }" }# | j |" k o| j | j k n oD| j |# k oB| j | j k n sét d j |" |#    qéqéW|! d d  d f c | j 8<|! d d  d f c | j 8<xW |! D]O \ }" }# | j  |" |#  \ } } | | |  }  |  d k	 r«| j |   q«q«W| rh|	 d k r1t! j" | j#   | j$    }	 n  | d k rVt% j& t
 j d  } qht% j& |  } n  g  }$ g  }% x| D]	}  t j' |  d  }& t j' |  d  }' | j( t j' |     \ }( }) |( | j 7}( |) | j 7}) t j) |( |) g  j* d d d  }* |$ j+ t j, |* d  |* d g   t j- t j. t j/ |(  d t j/ |)  d   }+ t j0 |+ |+ d d	  }, |( |, |) |, f }- t j1 |( |, |, d ! t j1 |) |, |, d ! f }. t | t j  r	t2 | |& |'  d  }/ | d j+ |/  |/ |, | d <n  | rOt2 | |& |'  d  }0 | j |0  | |	 |0 |,   | d <n  t3 j4 |- |. d | | }1 |  j5 |1  |% j |1  q{Wt6 j7 |$ d | | }2 | j | j | j g |2 j8 j9 (| j | j | j g |2 j8 j: (| r|2 j; t j j, |   |2 j< |  |2 j= |	  n  |  j> |2  |  j?   t
 j@ jA |%  }3 tB |2 |3  }4 |4 S(   uĮ	  Draws streamlines of a vector flow.

    *x*, *y* : 1d arrays
        an *evenly spaced* grid.
    *u*, *v* : 2d arrays
        x and y-velocities. Number of rows should match length of y, and
        the number of columns should match x.
    *density* : float or 2-tuple
        Controls the closeness of streamlines. When `density = 1`, the domain
        is divided into a 30x30 grid---*density* linearly scales this grid.
        Each cell in the grid can have, at most, one traversing streamline.
        For different densities in each direction, use [density_x, density_y].
    *linewidth* : numeric or 2d array
        vary linewidth when given a 2d array with the same shape as velocities.
    *color* : matplotlib color code, or 2d array
        Streamline color. When given an array with the same shape as
        velocities, *color* values are converted to colors using *cmap*.
    *cmap* : :class:`~matplotlib.colors.Colormap`
        Colormap used to plot streamlines and arrows. Only necessary when using
        an array input for *color*.
    *norm* : :class:`~matplotlib.colors.Normalize`
        Normalize object used to scale luminance data to 0, 1. If None, stretch
        (min, max) to (0, 1). Only necessary when *color* is an array.
    *arrowsize* : float
        Factor scale arrow size.
    *arrowstyle* : str
        Arrow style specification.
        See :class:`~matplotlib.patches.FancyArrowPatch`.
    *minlength* : float
        Minimum length of streamline in axes coordinates.
    *start_points*: Nx2 array
        Coordinates of starting points for the streamlines.
        In data coordinates, the same as the ``x`` and ``y`` arrays.
    *zorder* : int
        any number
    *maxlength* : float
        Maximum length of streamline in axes coordinates.
    *integration_direction* : ['forward', 'backward', 'both']
        Integrate the streamline in forward, backward or both directions.

    Returns:

        *stream_container* : StreamplotSet
            Container object with attributes

                - lines: `matplotlib.collections.LineCollection` of streamlines

                - arrows: collection of `matplotlib.patches.FancyArrowPatch`
                  objects representing arrows half-way along stream
                  lines.

            This container will probably change in the future to allow changes
            to the colormap, alpha, etc. for both lines and arrows, but these
            changes should be backward compatible.

    u   lines.linewidtht
   arrowstylet   mutation_scalei
   u   bothu   forwardu   backwarduT   Integration direction '%s' not recognised. Expected 'both', 'forward' or 'backward'.g       @u7   If 'color' is given, must have the shape of 'Grid(x,y)'u   coloru;   If 'linewidth' is given, must have the shape of 'Grid(x,y)'u	   linewidthu   zorderu(   'u' and 'v' must be of shape 'Grid(x,y)'i    t   dtypeu2   Starting point ({}, {}) outside of data boundariesNi   u
   image.cmapi’’’’i   t	   transform(   u   bothu   forwardu   backward(C   t   Gridt
   StreamMaskt	   DomainMapt   Nonet   mlinest   Line2Dt   zordert	   transDatat
   _get_linest   get_next_colort
   matplotlibt   rcParamst   dictt
   ValueErrort
   isinstancet   npt   ndarrayt   shapet   mat   masked_invalidt   get_integratort   _gen_starting_pointst	   mask2gridt   appendt
   asanyarrayt   floatt   copyt   x_origint   widtht   y_origint   heightt   formatt	   data2gridt   mcolorst	   Normalizet   mint   maxt   cmt   get_cmapt   arrayt	   grid2datat	   transposet   reshapet   extendt   hstackt   cumsumt   sqrtt   difft   searchsortedt   meant
   interpgridt   patchest   FancyArrowPatcht	   add_patcht   mcollectionst   LineCollectiont   sticky_edgest   xt   yt	   set_arrayt   set_cmapt   set_normt   add_collectiont   autoscale_viewt   collectionst   PatchCollectiont   StreamplotSet(5   t   axesRB   RC   t   ut   vt   densityt	   linewidtht   colort   cmapt   normt	   arrowsizeR   t	   minlengthR   R   t   start_pointst	   maxlengtht   integration_directiont   gridt   maskt   dmapt   line_kwt   arrow_kwt   errstrt   use_multicolor_linest   line_colorst	   integratet   trajectoriest   xmt   ymt   xgt   ygt   tt   sp2t   xst   yst   streamlinest   arrowst   tgxt   tgyt   txt   tyt   pointst   st   nt
   arrow_tailt
   arrow_headt   line_widthst   color_valuest   pt   lct   act   stream_container(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt
   streamplot   sŚ    <





$	))	!$$64  
RK   c           B` s   e  Z d    Z RS(   c         K` s   | |  _  | |  _ d  S(   N(   t   linesRl   (   t   selfR}   Rl   t   kwargs(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   __init__ė   s    	(   t   __name__t
   __module__R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyRK   é   s   R   c           B` s_   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z RS(
   u  Map representing different coordinate systems.

    Coordinate definitions:

    * axes-coordinates goes from 0 to 1 in the domain.
    * data-coordinates are specified by the input x-y coordinates.
    * grid-coordinates goes from 0 to N and 0 to M for an N x M grid,
      where N and M match the shape of the input data.
    * mask-coordinates goes from 0 to N and 0 to M for an N x M mask,
      where N and M are user-specified to control the density of streamlines.

    This class also has methods for adding trajectories to the StreamMask.
    Before adding a trajectory, run `start_trajectory` to keep track of regions
    crossed by a given trajectory. Later, if you decide the trajectory is bad
    (e.g., if the trajectory is very short) just call `undo_trajectory`.
    c         C` s   | |  _  | |  _ | j d | j |  _ | j d | j |  _ d |  j |  _ d |  j |  _ d | j |  _	 d | j
 |  _ d  S(   Ni   g      š?(   RY   RZ   t   nxt   x_grid2maskt   nyt   y_grid2maskt   x_mask2gridt   y_mask2gridt   dxt   x_data2gridt   dyt   y_data2grid(   R~   RY   RZ   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR     s    		c         C` s,   t  | |  j d  t  | |  j d  f S(   u;   Return nearest space in mask-coords from given grid-coords.g      ą?(   t   intR   R   (   R~   t   xit   yi(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt	   grid2mask  s    c         C` s   | |  j  | |  j f S(   N(   R   R   (   R~   Rc   Rd   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR     s    c         C` s   | |  j  | |  j f S(   N(   R   R   (   R~   t   xdt   yd(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR)     s    c         C` s   | |  j  | |  j f S(   N(   R   R   (   R~   Re   Rf   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR1     s    c         C` s/   |  j  | |  \ } } |  j j | |  d  S(   N(   R   RZ   t   _start_trajectory(   R~   Re   Rf   Rc   Rd   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   start_trajectory   s    c         C` s.   |  j  | |  \ } } | | f |  j _ d  S(   N(   R   RZ   t   _current_xy(   R~   Re   Rf   Rc   Rd   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   reset_start_point$  s    c         C` sM   |  j  j | |  s t  n  |  j | |  \ } } |  j j | |  d  S(   N(   RY   t   within_gridt   InvalidIndexErrorR   RZ   t   _update_trajectory(   R~   Re   Rf   Rc   Rd   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   update_trajectory(  s    	c         C` s   |  j  j   d  S(   N(   RZ   t   _undo_trajectory(   R~   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   undo_trajectory.  s    (   R   R   t   __doc__R   R   R   R)   R1   R   R   R   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   ó   s   								R	   c           B` s/   e  Z d  Z d   Z e d    Z d   Z RS(   u   Grid of data.c         C` sm  | j  d k r n[ | j  d k ra | d d  d   f } t j | |  sX t d   n  | } n t d   | j  d k r n^ | j  d k rŃ | d  d   d f } t j | | j  sČ t d   n  | } n t d   t |  |  _ t |  |  _ | d | d |  _ | d | d |  _	 | d |  _
 | d |  _ | d | d |  _ | d | d |  _ d  S(	   Ni   i   i    u   The rows of 'x' must be equalu$   'x' can have at maximum 2 dimensionsu    The columns of 'y' must be equalu$   'y' can have at maximum 2 dimensionsi’’’’(   t   ndimR   t   allcloseR   t   Tt   lenR   R   R   R   R$   R&   R%   R'   (   R~   RB   RC   t   x_rowt   y_col(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   4  s0    		c         C` s   |  j  |  j f S(   N(   R   R   (   R~   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   V  s    c         C` s<   | d k o; | |  j  d k o; | d k o; | |  j d k S(   u.   Return True if point is a valid index of grid.i    i   (   R   R   (   R~   R   R   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   Z  s    (   R   R   R   R   t   propertyR   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR	   2  s   	"R
   c           B` s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   uI  Mask to keep track of discrete regions crossed by streamlines.

    The resolution of this grid determines the approximate spacing between
    trajectories. Streamlines are only allowed to pass through zeroed cells:
    When a streamline enters a cell, that cell is set to 1, and no new
    streamlines are allowed to enter.
    c         C` sŠ   t  j |  rG | d k r* t d   n  t d |  |  _ |  _ nO t |  d k rh t d   n  t d | d  |  _ t d | d  |  _ t  j |  j |  j f  |  _ |  j j	 |  _	 d  |  _ d  S(   Ni    u'   If a scalar, 'density' must be positivei   i   u*   'density' can have at maximum 2 dimensionsi   (   R   t   isscalarR   R   R   R   R”   t   zerost   _maskR   R   R   (   R~   RO   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   j  s    c         G` s   |  j  j |   S(   N(   R§   t   __getitem__(   R~   t   args(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyRØ   y  s    c         C` s   g  |  _  |  j | |  d S(   u%   Start recording streamline trajectoryN(   t   _trajR   (   R~   Rc   Rd   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   |  s    	c         C` s+   x$ |  j  D] } |  j j | d  q
 Wd S(   u#   Remove current trajectory from maski    N(   RŖ   R§   t   __setitem__(   R~   Rg   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR     s    c         C` ss   |  j  | | f k ro |  | | f d k rf |  j j | | f  d |  j | | f <| | f |  _  qo t  n  d S(   u   Update current trajectory position in mask.

        If the new position has already been filled, raise `InvalidIndexError`.
        i    i   N(   R   RŖ   R    R§   R   (   R~   Rc   Rd   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR     s    (   R   R   R   R   RØ   R   R   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR
   a  s   				R   c           B` s   e  Z RS(    (   R   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR     s   t   TerminateTrajectoryc           B` s   e  Z RS(    (   R   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR¬     s   c   	      ` s    j     \     j j }   j j } t j j | d | d      f d     f d            f d   } | S(   Ni   c         ` sj   t    |  |  } | d k r* t    n  d | } t   |  |  } t   |  |  } | | | | f S(   Ni    g      š?(   R;   R¬   (   R   R   t   ds_dtt   dt_dst   uit   vi(   t   speedRM   RN   (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   forward_time©  s    
c         ` s!     |  |  \ } } | | f S(   N(    (   R   R   t   dxit   dyi(   R²   (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   backward_time²  s    c         ` sT  d g  g  } } } y  j  |  |  Wn t k
 r< d SX d	 k r„ t |  |      \ } } } | | 7} | | d d d  7} | | d d d  7} n   d
 k r, j |  |  t |  |     \ } } } t |  d k r| d } | d } n  | | 7} | | 7} | | 7} n  |  k rB| | f S j   d Sd S(   u  Return x, y grid-coordinates of trajectory based on starting point.

        Integrate both forward and backward in time from starting point in
        grid coordinates.

        Integration is terminated when a trajectory reaches a domain boundary
        or when it crosses into an already occupied cell in the StreamMask. The
        resulting trajectory is None if it is shorter than `minlength`.
        g        u   bothu   backwardNi’’’’u   forwardi    i   (   u   bothu   backward(   u   bothu   forward(   R   R   R   t   _integrate_rk12R   R”   R   (   t   x0t   y0t   stotalt   x_trajt   y_trajRr   t   xtt   yt(   Rµ   R[   R²   RX   RW   RU   (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyRa   ¶  s.    !
!




(   R)   RY   R   R   R   R   R7   (	   RM   RN   R[   RU   RW   RX   t   u_axt   v_axRa   (    (	   Rµ   R[   R²   RX   RW   RU   R±   RM   RN   s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR     s    	'c         C` s2  d } t  d | j j d | j j d  } | } d } |  }	 | }
 g  } g  } xŅ| j j |	 |
  r$| j |	  | j |
  y> | |	 |
  \ } } | |	 | | |
 | |  \ } } WnK t k
 rü t | | | |  \ } } } | | 7} Pn t	 k
 rPn X| | } | | } | d | | } | d | | } | j j
 \ } } t j | | | d | | | d  } | | k  rķ|	 | 7}	 |
 | 7}
 y | j |	 |
  Wn t k
 rĖPn X| | | k rąPn  | | 7} n  | d k r| } qS t  | d | | | d  } qS W| | | f S(   uA  2nd-order Runge-Kutta algorithm with adaptive step size.

    This method is also referred to as the improved Euler's method, or Heun's
    method. This method is favored over higher-order methods because:

    1. To get decent looking trajectories and to sample every mask cell
       on the trajectory we need a small timestep, so a lower order
       solver doesn't hurt us unless the data is *very* high resolution.
       In fact, for cases where the user inputs
       data smaller or of similar grid size to the mask grid, the higher
       order corrections are negligible because of the very fast linear
       interpolation used in `interpgrid`.

    2. For high resolution input data (i.e. beyond the mask
       resolution), we must reduce the timestep. Therefore, an adaptive
       timestep is more suited to the problem as this would be very hard
       to judge automatically otherwise.

    This integrator is about 1.5 - 2x as fast as both the RK4 and RK45
    solvers in most setups on my machine. I would recommend removing the
    other two to keep things simple.
    gś~j¼th?g      š?g¹?i    g      ą?i   g333333ė?(   R,   RZ   R   R   RY   R   R    t
   IndexErrort   _euler_stepR¬   R   R   R7   R   R   (   R·   Rø   R[   t   fRW   t   maxerrort   maxdst   dsR¹   R   R   t   xf_trajt   yf_trajt   k1xt   k1yt   k2xt   k2yt   dx1t   dy1t   dx2t   dy2R   R   t   error(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR¶   ą  sR    &


+

	#c         C` s	  | j  j \ } } |  d } | d } | | |  \ } }	 | d k rS t j }
 n, | d k  rm | | }
 n | d | | }
 |	 d k r t j } n, |	 d k  r± | |	 } n | d | |	 } t |
 |  } |  j | | |  | j | |	 |  | |  | f S(   uB   Simple Euler integration step that extends streamline to boundary.i’’’’i    i   (   RY   R   R   t   infR,   R    (   RĘ   RĒ   R[   RĀ   R   R   R   R   t   cxt   cyt   dsxt   dsyRÅ   (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyRĮ   9  s$    

c         C` sŖ  t  j |   \ } } t | t  j  r | j t  } | j t  } t  j | d d | d  } t  j | d d | d  } n^ t |  } t |  } | | d k r³ | } n
 | d } | | d k rÖ | } n
 | d } |  | | f }	 |  | | f }
 |  | | f } |  | | f } | | } | | } |	 d | |
 | } | d | | | } | d | | | } t | t  j  s¦t  j j |  r¦t	  q¦n  | S(   u0   Fast 2D, linear interpolation on an integer gridi   i    i   (
   R   R   R   R   t   astypeR   t   clipR   t	   is_maskedR¬   (   t   aR   R   t   Nyt   NxRB   RC   t   xnt   ynt   a00t   a01t   a10t   a11R¼   R½   t   a0t   a1t   ai(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR;   T  s6     	
	


c         c` sN  |  \ } } d } d } | d } | d } d \ } } d }	 d }
 xt  | |  D]ń }	 | | f V|
 d k r | d 7} | | k rF| d 8} d }
 qFqU |
 d k rÖ | d 7} | | k rF| d 8} d }
 qFqU |
 d k r| d 8} | | k rF| d 7} d }
 qFqU |
 d k rU | d 8} | | k rF| d 7} d }
 qFqU qU Wd S(	   u  Yield starting points for streamlines.

    Trying points on the boundary first gives higher quality streamlines.
    This algorithm starts with a point on the mask corner and spirals inward.
    This algorithm is inefficient, but fast compared to rest of streamplot.
    i    i   u   rightu   upu   leftu   downN(   i    i    (   R   (   R   R   R   t   xfirstt   yfirstt   xlastt   ylastRB   RC   t   it	   direction(    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyR   |  s<    









()   R   t
   __future__R    R   R   R   t   sixt	   six.movesR   t   numpyR   R   t   matplotlib.cmR.   t   matplotlib.colorst   colorsR*   t   matplotlib.collectionsRI   R?   t   matplotlib.linesR}   R   t   matplotlib.patchesR<   t   __all__R   R|   t   objectRK   R   R	   R
   t	   ExceptionR   R¬   R   R¶   RĮ   R;   R   (    (    (    s4   lib/python2.7/site-packages/matplotlib/streamplot.pyt   <module>   s4   "		Ļ
?/3	A	Y		(