3
$ßL]åt ã               @   s�  d Z ddlZddlmZ ddlZddlmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZ G d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd „ d eƒZG d!d"„ d"eƒZG d#d$„ d$eƒZG d%d&„ d&eƒZ G d'd(„ d(e ƒZ!G d)d*„ d*eƒZ"G d+d,„ d,eƒZ#G d-d.„ d.eƒZ$dS )/a…  
GUI neutral widgets
===================

Widgets that are designed to work for any of the GUI backends.
All of these widgets require you to predefine a :class:`matplotlib.axes.Axes`
instance and pass that as the first arg.  matplotlib doesn't try to
be too smart with respect to layout -- you will have to figure out how
wide and tall you want your Axes to be to accommodate your widget.
é    N)ÚIntegralé   )ÚcbookÚrcParams)ÚLine2D)ÚCircleÚ	RectangleÚEllipse)Úblended_transform_factoryc               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )ÚLockDrawa®  
    Some widgets, like the cursor, draw onto the canvas, and this is not
    desirable under all circumstances, like when the toolbar is in zoom-to-rect
    mode and drawing a rectangle.  To avoid this, a widget can acquire a
    canvas' lock with ``canvas.widgetlock(widget)`` before drawing on the
    canvas; this will prevent other widgets from doing so at the same time (if
    they also try to acquire the lock first).
    c             C   s
   d | _ d S )N)Ú_owner)Úself© r   ú:/tmp/pip-install-edjban26/matplotlib/matplotlib/widgets.pyÚ__init__!   s    zLockDraw.__init__c             C   s   | j |ƒstdƒ‚|| _dS )zReserve the lock for *o*.zalready lockedN)Ú	availableÚ
ValueErrorr   )r   Úor   r   r   Ú__call__$   s    
zLockDraw.__call__c             C   s   | j |ƒstdƒ‚d| _dS )zRelease the lock from *o*.zyou do not own this lockN)r   r   r   )r   r   r   r   r   Úrelease*   s    
zLockDraw.releasec             C   s   | j ƒ  p| j|ƒS )z+Return whether drawing is available to *o*.)ÚlockedÚisowner)r   r   r   r   r   r   0   s    zLockDraw.availablec             C   s
   | j |kS )z"Return whether *o* owns this lock.)r   )r   r   r   r   r   r   4   s    zLockDraw.isownerc             C   s
   | j dk	S )z6Return whether the lock is currently held by an owner.N)r   )r   r   r   r   r   8   s    zLockDraw.lockedN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r      s   r   c               @   sF   e Zd ZdZdZdZdZdd„ Zdd„ Ze	edd„ d	d
�Z
dd„ ZdS )ÚWidgetz5
    Abstract base class for GUI neutral widgets
    Tc             C   s
   || _ dS )z*Set whether the widget is active.
        N)Ú_active)r   Úactiver   r   r   Ú
set_activeE   s    zWidget.set_activec             C   s   | j S )z*Get whether the widget is active.
        )r   )r   r   r   r   Ú
get_activeJ   s    zWidget.get_activec             C   s
   | j |ƒS )N)r   )r   r   r   r   r   Ú<lambda>P   s    zWidget.<lambda>zIs the widget active?)Údocc             C   s   | j  S )z›Return True if event should be ignored.

        This method (or a version of it) should be called at the beginning
        of any event callback.
        )r   )r   Úeventr   r   r   ÚignoreS   s    zWidget.ignoreN)r   r   r   r   ÚdrawonÚeventsonr   r   r    Úpropertyr   r$   r   r   r   r   r   =   s   
r   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	Ú
AxesWidgeta  Widget that is connected to a single
    :class:`~matplotlib.axes.Axes`.

    To guarantee that the widget remains responsive and not garbage-collected,
    a reference to the object should be maintained by the user.

    This is necessary because the callback registry
    maintains only weak-refs to the functions, which are member
    functions of the widget.  If there are no references to the widget
    object it may be garbage collected which will disconnect the
    callbacks.

    Attributes:

    *ax* : :class:`~matplotlib.axes.Axes`
        The parent axes for the widget
    *canvas* : :class:`~matplotlib.backend_bases.FigureCanvasBase` subclass
        The parent figure canvas for the widget.
    *active* : bool
        If False, the widget does not respond to events.
    c             C   s   || _ |jj| _g | _d S )N)ÚaxÚfigureÚcanvasÚcids)r   r)   r   r   r   r   r   s    
zAxesWidget.__init__c             C   s   | j j||ƒ}| jj|ƒ dS )z°Connect callback with an event.

        This should be used in lieu of `figure.canvas.mpl_connect` since this
        function stores callback ids for later clean up.
        N)r+   Úmpl_connectr,   Úappend)r   r#   ÚcallbackÚcidr   r   r   Úconnect_eventw   s    zAxesWidget.connect_eventc             C   s    x| j D ]}| jj|ƒ qW dS )z-Disconnect all events created by this widget.N)r,   r+   Úmpl_disconnect)r   Úcr   r   r   Údisconnect_events€   s    zAxesWidget.disconnect_eventsN)r   r   r   r   r   r1   r4   r   r   r   r   r(   \   s   	r(   c               @   sB   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dS )ÚButtona¿  
    A GUI neutral button.

    For the button to remain responsive you must keep a reference to it.
    Call :meth:`on_clicked` to connect to the button.

    Attributes
    ----------
    ax
        The :class:`matplotlib.axes.Axes` the button renders into.
    label
        A :class:`matplotlib.text.Text` instance.
    color
        The color of the button when not hovering.
    hovercolor
        The color of the button when hovering.
    Nú0.85ú0.95c             C   s¬   t j| |ƒ |dk	r|j|ƒ |jdd|dd|jd�| _d| _i | _| jd| j	ƒ | jd| j
ƒ | jd| jƒ |jd	ƒ |j|ƒ |jg ƒ |jg ƒ || _|| _|| _dS )
a}  
        Parameters
        ----------
        ax : matplotlib.axes.Axes
            The :class:`matplotlib.axes.Axes` instance the button
            will be placed into.

        label : str
            The button text. Accepts string.

        image : array, mpl image, Pillow Image
            The image to place in the button, if not *None*.
            Can be any legal arg to imshow (numpy array,
            matplotlib Image instance, or Pillow Image).

        color : color
            The color of the button when not activated

        hovercolor : color
            The color of the button when the mouse is over it
        Ng      à?Úcenter)ÚverticalalignmentÚhorizontalalignmentÚ	transformr   Úbutton_press_eventÚbutton_release_eventÚmotion_notify_eventF)r(   r   ZimshowÚtextÚ	transAxesÚlabelÚcntÚ	observersr1   Ú_clickÚ_releaseÚ_motionÚset_navigateÚset_facecolorÚ
set_xticksÚ
set_yticksÚcolorÚ
hovercolorÚ
_lastcolor)r   r)   rA   ÚimagerK   rL   r   r   r   r   ™   s&    





zButton.__init__c             C   sH   | j |ƒrd S |j| jkrd S | js(d S |jj| jkrD|jj| jƒ d S )N)r$   Úinaxesr)   r&   r+   Úmouse_grabberÚ
grab_mouse)r   r#   r   r   r   rD   È   s    
zButton._clickc             C   sl   | j |ƒrd S |jj| jkr d S |jj| jƒ | js8d S |j| jkrHd S x| jjƒ D ]\}}||ƒ qTW d S )N)	r$   r+   rP   r)   Úrelease_mouser&   rO   rC   Úitems)r   r#   r0   Úfuncr   r   r   rE   Ò   s    
zButton._releasec             C   s\   | j |ƒrd S |j| jkr"| j}n| j}|| jkrX| jj|ƒ || _| jrX| jjj	j
ƒ  d S )N)r$   rO   r)   rL   rK   rM   rH   r%   r*   r+   Údraw)r   r#   r3   r   r   r   rF   ß   s    

zButton._motionc             C   s"   | j }|| j|< |  j d7  _ |S )zž
        Connect the callback function *func* to button click events.

        Returns a connection id, which can be used to disconnect the callback.
        r   )rB   rC   )r   rT   r0   r   r   r   Ú
on_clickedì   s    
zButton.on_clickedc             C   s&   y| j |= W n tk
r    Y nX dS )z6Remove the callback function with connection id *cid*.N)rC   ÚKeyError)r   r0   r   r   r   Ú
disconnect÷   s    zButton.disconnect)Nr6   r7   )
r   r   r   r   r   rD   rE   rF   rV   rX   r   r   r   r   r5   †   s    
.
r5   c            	   @   sJ   e Zd ZdZddd„Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dS )ÚSlideraB  
    A slider representing a floating point range.

    Create a slider from *valmin* to *valmax* in axes *ax*. For the slider to
    remain responsive you must maintain a reference to it. Call
    :meth:`on_changed` to connect to the slider event.

    Attributes
    ----------
    val : float
        Slider value.
    ç      à?ú%1.2fTNÚ
horizontalc             K   s<  t j| |ƒ |	dk	r2t|	dƒ r2tdjt|	ƒƒƒ‚|
dk	rXt|
dƒ rXtdjt|
ƒƒƒ‚|dkrntdj|ƒƒ‚|| _|| _|| _|	| _	|
| _
d| _|| _|| _|| _| j|ƒ}|dkrº|}|| _|| _|dk� rþ|j||d	d
f|Ž| _|j|d	d
dd
d�| _n,|j||d	d
f|Ž| _|j|d	d
dd
d�| _|| _|jg ƒ |dk�rT|j||fƒ n|j||fƒ |jg ƒ |jdƒ | jd| j ƒ | jd| j ƒ |�r¦| jd| j ƒ |dk�rê|j!dd||j"ddd�| _#|j!dd|| |j"ddd�| _$n8|j!dd||j"ddd�| _#|j!dd|| |j"ddd�| _$d	| _%i | _&| j'|ƒ dS )aå  
        Parameters
        ----------
        ax : Axes
            The Axes to put the slider in.

        label : str
            Slider label.

        valmin : float
            The minimum value of the slider.

        valmax : float
            The maximum value of the slider.

        valinit : float, optional, default: 0.5
            The slider initial position.

        valfmt : str, optional, default: "%1.2f"
            Used to format the slider value, fprint format string.

        closedmin : bool, optional, default: True
            Indicate whether the slider interval is closed on the bottom.

        closedmax : bool, optional, default: True
            Indicate whether the slider interval is closed on the top.

        slidermin : Slider, optional, default: None
            Do not allow the current slider to have a value less than
            the value of the Slider `slidermin`.

        slidermax : Slider, optional, default: None
            Do not allow the current slider to have a value greater than
            the value of the Slider `slidermax`.

        dragging : bool, optional, default: True
            If True the slider can be dragged by the mouse.

        valstep : float, optional, default: None
            If given, the slider will snap to multiples of `valstep`.

        orientation : str, 'horizontal' or 'vertical', default: 'horizontal'
            The orientation of the slider.

        Notes
        -----
        Additional kwargs are passed on to ``self.poly`` which is the
        :class:`~matplotlib.patches.Rectangle` that draws the slider
        knob.  See the :class:`~matplotlib.patches.Rectangle` documentation for
        valid property names (e.g., `facecolor`, `edgecolor`, `alpha`).
        NÚvalz$Argument slidermin ({}) has no 'val'z$Argument slidermax ({}) has no 'val'r\   ÚverticalzBArgument orientation ({}) must be either'horizontal' or 'vertical'Fr   r   Úr)rK   Úlwr<   r=   r>   g      à?gR¸…ëQð?Úbottomr8   )r;   r9   r:   g{®Gáz”?ÚtopÚrightÚleft)r\   r^   g{®Gáz”¿g{®Gáz”¿)(r(   r   Úhasattrr   ÚformatÚtypeÚorientationÚ	closedminÚ	closedmaxÚ	sliderminÚ	slidermaxÚdrag_activeÚvalminÚvalmaxÚvalstepÚ_value_in_boundsr]   ÚvalinitZaxhspanÚpolyÚaxhlineZhlineZaxvspanÚaxvlineZvlineÚvalfmtrJ   Úset_ylimÚset_xlimrI   rG   r1   Ú_updater?   r@   rA   ÚvaltextrB   rC   Úset_val)r   r)   rA   rn   ro   rr   rv   ri   rj   rk   rl   Zdraggingrp   rh   Úkwargsr   r   r   r     st    7









zSlider.__init__c             C   s¶   | j r,tj|| j | j  ƒ| j  }|| j7 }|| jkrH| js@dS | j}n|| jkrb| js\dS | j}| jdk	rŠ|| jjkrŠ| js‚dS | jj}| j	dk	r²|| j	jkr²| jsªdS | j	j}|S )z&Makes sure *val* is with given bounds.N)
rp   ÚnpÚroundrn   ri   ro   rj   rk   r]   rl   )r   r]   r   r   r   rq   ‰  s(    


zSlider._value_in_boundsc             C   sÄ   | j |ƒs|jdkrdS |jdkrB|j| jkrBd| _|jj| jƒ | jsLdS |jdksl|jdkr„|j| jkr„d| _|jj| jƒ dS | j	dkrœ| j
|jƒ}n| j
|jƒ}|d| jgkrÀ| j|ƒ dS )zUpdate the slider position.r   Nr<   Tr=   Fr^   )r$   ÚbuttonÚnamerO   r)   rm   r+   rQ   rR   rh   rq   ÚydataÚxdatar]   r{   )r   r#   r]   r   r   r   ry   £  s$    


zSlider._updatec             C   s¦   | j j}| jdkr,d|f|d< d|f|d< n|df|d< |df|d< || j _| jj| j| ƒ | jrr| jjj	j
ƒ  || _| js‚dS x| jjƒ D ]\}}||ƒ qŽW dS )zf
        Set slider value to *val*

        Parameters
        ----------
        val : float
        r^   r   r   é   é   N)rs   Úxyrh   rz   Zset_textrv   r%   r)   r*   r+   Ú	draw_idler]   r&   rC   rS   )r   r]   r…   r0   rT   r   r   r   r{   ¼  s    
zSlider.set_valc             C   s"   | j }|| j|< |  j d7  _ |S )a�  
        When the slider value is changed call *func* with the new
        slider value

        Parameters
        ----------
        func : callable
            Function to call when slider is changed.
            The function must accept a single float as its arguments.

        Returns
        -------
        cid : int
            Connection id (which can be used to disconnect *func*)
        r   )rB   rC   )r   rT   r0   r   r   r   Ú
on_changedÕ  s    
zSlider.on_changedc             C   s&   y| j |= W n tk
r    Y nX dS )z¯
        Remove the observer with connection id *cid*

        Parameters
        ----------
        cid : int
            Connection id of the observer to be removed
        N)rC   rW   )r   r0   r   r   r   rX   ê  s    	zSlider.disconnectc             C   s   | j | jkr| j| jƒ dS )z%Reset the slider to the initial valueN)r]   rr   r{   )r   r   r   r   Úresetø  s    zSlider.reset)	rZ   r[   TTNNTNr\   )r   r   r   r   r   rq   ry   r{   r‡   rX   rˆ   r   r   r   r   rY   ÿ   s      
zrY   c               @   sB   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Z	dS )ÚCheckButtonsaµ  
    A GUI neutral set of check buttons.

    For the check buttons to remain responsive you must keep a
    reference to this object.

    The following attributes are exposed

     *ax*
        The :class:`matplotlib.axes.Axes` instance the buttons are
        located in

     *labels*
        List of :class:`matplotlib.text.Text` instances

     *lines*
        List of (line1, line2) tuples for the x's in the check boxes.
        These lines exist for each box, but have ``set_visible(False)``
        when its box is not checked.

     *rectangles*
        List of :class:`matplotlib.patches.Rectangle` instances

    Connect to the CheckButtons with the :meth:`on_clicked` method
    Nc          	   C   sÐ  t j| |ƒ |jg ƒ |jg ƒ |jdƒ |dkr@dgt|ƒ }t|ƒdkrtdt|ƒd  }tjd| |t|ƒƒ}n
d}dg}|jƒ }g | _	g | _
g | _dd|jd	d
œ}�xt|||ƒD ]ö\}}	}
|jd||	|jddd�}|d |d  }}d||d   }}t||f||d||jd�}t||| g|| |gf|Ž}t||| g||| gf|Ž}|j|
ƒ |j|
ƒ | j	j|ƒ | jj|ƒ | j
j||fƒ |j|ƒ |j|ƒ |j|ƒ q¸W | jd| jƒ d| _i | _dS )aÕ  
        Add check buttons to :class:`matplotlib.axes.Axes` instance *ax*

        Parameters
        ----------
        ax : `~matplotlib.axes.Axes`
            The parent axes for the widget.

        labels : List[str]
            The labels of the check buttons.

        actives : List[bool], optional
            The initial check states of the buttons. The list must have the
            same length as *labels*. If not given, all buttons are unchecked.
        FNr   g      ð?g      Ð?g      à?Úkg      ô?Zbutt)rK   Ú	linewidthr;   Zsolid_capstylerd   r8   )r;   r:   r9   rƒ   gš™™™™™©?Úblack)r…   ÚwidthÚheightÚ	edgecolorÚ	facecolorr;   r<   r   )r(   r   rI   rJ   rG   Úlenr}   ÚlinspaceÚget_facecolorÚlabelsÚlinesÚ
rectanglesr@   Úzipr?   r   r   Úset_visibler.   Ú	add_patchÚadd_liner1   Ú_clickedrB   rC   )r   r)   r”   ZactivesÚdyÚysÚaxcolorZ
lineparamsÚyrA   r   ÚtÚwÚhÚxÚpÚl1Úl2r   r   r   r     sL    






zCheckButtons.__init__c             C   s€   | j |ƒs |jdks |j| jkr$d S xVtt| j| jƒƒD ]@\}\}}|jƒ j	|j
|jƒsl|jƒ j	|j
|jƒr8| j|ƒ P q8W d S )Nr   )r$   r   rO   r)   Ú	enumerater—   r–   r”   Úget_window_extentÚcontainsr£   rŸ   r   )r   r#   Úir¤   r    r   r   r   r›   \  s      
zCheckButtons._clickedc             C   s¤   d|  kot | jƒkn  r*td| ƒ‚| j| \}}|j|jƒ  ƒ |j|jƒ  ƒ | jrl| jjj	j
ƒ  | jsvdS x(| jjƒ D ]\}}|| j| jƒ ƒ q‚W dS )a%  
        Directly (de)activate a check button by index.

        *index* is an index into the original label list
            that this object was constructed with.
            Raises ValueError if *index* is invalid.

        Callbacks will be triggered if :attr:`eventson` is True.

        r   zInvalid CheckButton index: %dN)r‘   r”   r   r•   r˜   Úget_visibler%   r)   r*   r+   rU   r&   rC   rS   Úget_text)r   Úindexr¥   r¦   r0   rT   r   r   r   r   e  s    zCheckButtons.set_activec             C   s   dd„ | j D ƒS )zX
        Return a tuple of the status (True/False) of all of the check buttons.
        c             S   s   g | ]\}}|j ƒ ‘qS r   )r«   )Ú.0r¥   r¦   r   r   r   ú
<listcomp>ƒ  s    z+CheckButtons.get_status.<locals>.<listcomp>)r•   )r   r   r   r   Ú
get_status  s    zCheckButtons.get_statusc             C   s"   | j }|| j|< |  j d7  _ |S )zž
        Connect the callback function *func* to button click events.

        Returns a connection id, which can be used to disconnect the callback.
        r   )rB   rC   )r   rT   r0   r   r   r   rV   …  s    
zCheckButtons.on_clickedc             C   s&   y| j |= W n tk
r    Y nX dS )z,remove the observer with connection id *cid*N)rC   rW   )r   r0   r   r   r   rX   �  s    zCheckButtons.disconnect)N)
r   r   r   r   r   r›   r   r°   rV   rX   r   r   r   r   r‰   þ  s   
D	r‰   c               @   sš   e Zd ZdZd)dd„Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd(S )*ÚTextBoxao  
    A GUI neutral text input box.

    For the text box to remain responsive you must keep a reference to it.

    The following attributes are accessible:

      *ax*
        The :class:`matplotlib.axes.Axes` the button renders into.

      *label*
        A :class:`matplotlib.text.Text` instance.

      *color*
        The color of the text box when not hovering.

      *hovercolor*
        The color of the text box when hovering.

    Call :meth:`on_text_change` to be updated whenever the text changes.

    Call :meth:`on_submit` to be updated whenever the user hits enter or
    leaves the text entry field.
    Ú ú.95Ú1ç{®Gáz„?c             C   s.  t j| |ƒ d| _dd„ tD ƒ| _|| _|j| d|dd|jd�| _| j| jƒ| _	d| _
i | _i | _| jjdd	ƒ | jjdd	ƒ d| _| jjdddƒ| _| jjd
ƒ | jd| jƒ | jd| jƒ | jd| jƒ | jd| jƒ | jd| jƒ |jd
ƒ |j|ƒ |jg ƒ |jg ƒ || _|| _|| _ d
| _!dS )a6  
        Parameters
        ----------
        ax : matplotlib.axes.Axes
            The :class:`matplotlib.axes.Axes` instance the button
            will be placed into.

        label : str
            Label for this text box. Accepts string.

        initial : str
            Initial value in the text box

        color : color
            The color of the box

        hovercolor : color
            The color of the box when the mouse is over it

        label_pad : float
            the distance between the label and the right side of the textbox
        gš™™™™™©?c             S   s   g | ]}d |kr|‘qS )Zkeymapr   )r®   Úkeyr   r   r   r¯   Î  s    z$TextBox.__init__.<locals>.<listcomp>g      à?r8   rc   )r9   r:   r;   r   r   Fr<   r=   r>   Úkey_press_eventZresize_eventN)"r(   r   ÚDIST_FROM_LEFTr   Úparams_to_disabler?   r@   rA   Ú_make_text_dispÚ	text_disprB   Úchange_observersÚsubmit_observersr)   rx   rw   Úcursor_indexÚvlinesÚcursorr˜   r1   rD   rE   rF   Ú	_keypressÚ_resizerG   rH   rI   rJ   rK   rL   rM   Úcapturekeystrokes)r   r)   rA   ÚinitialrK   rL   Z	label_padr   r   r   r   ²  s<    



zTextBox.__init__c             C   s   | j j| jd|dd| j jd�S )Ng      à?r8   rd   )r9   r:   r;   )r)   r?   r¸   r@   )r   Ústringr   r   r   rº   ÷  s    zTextBox._make_text_dispc             C   sÄ   | j d | j… }d}|dks,|dks,|dkr8|dk}d}| j|ƒ}| jjjjƒ  |jƒ }| jjj	ƒ }|j
|ƒ}|jdƒ |rˆ|d |d	< | jjdƒ | jj|d
 |d |d ƒ| _| jjjjƒ  d S )NFr²   ú z  ú,r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r?   r¾   rº   r)   r*   r+   rU   r¨   Ú	transDataÚinvertedr;   r˜   rÀ   r¿   )r   Z	widthtextZno_textZwt_dispÚbbÚinvr   r   r   Ú_rendercursorý  s     


zTextBox._rendercursorc             C   s,   | j r(x | jjƒ D ]\}}|| jƒ qW d S )N)r&   r½   rS   r?   )r   r0   rT   r   r   r   Ú_notify_submit_observers  s    z TextBox._notify_submit_observersc             C   s2   | j |ƒrd S |jj| jkr d S |jj| jƒ d S )N)r$   r+   rP   r)   rR   )r   r#   r   r   r   rE     s
    
zTextBox._releasec             C   sš  | j |ƒrd S | j�r–|j}t|ƒdkr^| jd | j… | | j| jd …  | _|  jd7  _nþ|dkr†| jt| jƒkr„|  jd7  _nÖ|dkr¨| jdkr¦|  jd8  _n´|dkr¸d| _n¤|dkrÎt| jƒ| _nŽ|dk�r| jdk�r\| jd | jd … | j| jd …  | _|  jd8  _nB|dk�r\| jt| jƒk�r\| jd | j… | j| jd d …  | _| jjƒ  | j| jƒ| _| j	ƒ  | j
ƒ  |d	k�r–| jƒ  d S )
Nr   rc   rd   r   ÚhomeÚendZ	backspaceÚdeleteZenter)r$   rÃ   r¶   r‘   r?   r¾   r»   Úremoverº   rÌ   Ú_notify_change_observersrÍ   )r   r#   r¶   r   r   r   rÁ   $  sB    





zTextBox._keypressc             C   sP   t |ƒ}| j|krd S || _| jjƒ  | j| jƒ| _| jƒ  | jƒ  | jƒ  d S )N)Ústrr?   r»   rÑ   rº   rÌ   rÒ   rÍ   )r   r]   Znewvalr   r   r   r{   I  s    

zTextBox.set_valc             C   s,   | j r(x | jjƒ D ]\}}|| jƒ qW d S )N)r&   r¼   rS   r?   )r   r0   rT   r   r   r   rÒ   T  s    z TextBox._notify_change_observersc             C   s6   d| _ i | _x$| jD ]}t| | j|< g t|< qW d S )NT)rÃ   Úreset_paramsr¹   r   )r   r£   r¶   r   r   r   Úbegin_typingY  s
    zTextBox.begin_typingc             C   s\   d}| j r,x| jD ]}| j| t|< qW d}d| _ | jjdƒ | jjjj	ƒ  |rX| j
ƒ  d S )NFT)rÃ   r¹   rÔ   r   rÀ   r˜   r)   r*   r+   rU   rÍ   )r   Znotifysubmitr¶   r   r   r   Ústop_typingb  s    zTextBox.stop_typingc             C   sœ   t | jƒdkrd| _nz| jjƒ }| jj}| jjjƒ }|j|j|ƒƒ}|d }|d }|| ||  }|dk rpd}|dkr|d}t	t | jƒ| ƒ| _| j
ƒ  d S )Nr   r   )r   r   )r   r   )r‘   r?   r¾   r»   r¨   r)   rÈ   rÉ   r;   ÚintrÌ   )r   r£   rÊ   ÚtransrË   Z
text_startZtext_endZratior   r   r   Úposition_cursors  s    
zTextBox.position_cursorc             C   sn   | j |ƒrd S |j| jkr&| jƒ  d S | js0d S |jj| jkrL|jj| jƒ | js^| j	|j
ƒ | j|j
ƒ d S )N)r$   rO   r)   rÖ   r&   r+   rP   rQ   rÃ   rÕ   r£   rÙ   )r   r#   r   r   r   rD   �  s    
zTextBox._clickc             C   s   | j ƒ  d S )N)rÖ   )r   r#   r   r   r   rÂ   ›  s    zTextBox._resizec             C   s\   | j |ƒrd S |j| jkr"| j}n| j}|| jkrX| jj|ƒ || _| jrX| jjj	j
ƒ  d S )N)r$   rO   r)   rL   rK   rM   rH   r%   r*   r+   rU   )r   r#   r3   r   r   r   rF   ž  s    

zTextBox._motionc             C   s"   | j }|| j|< |  j d7  _ |S )z‹
        When the text changes, call this *func* with event.

        A connection id is returned which can be used to disconnect.
        r   )rB   r¼   )r   rT   r0   r   r   r   Úon_text_change«  s    
zTextBox.on_text_changec             C   s"   | j }|| j|< |  j d7  _ |S )z³
        When the user hits enter or leaves the submission box, call this
        *func* with event.

        A connection id is returned which can be used to disconnect.
        r   )rB   r½   )r   rT   r0   r   r   r   Ú	on_submit¶  s    
zTextBox.on_submitc             C   s:   x4| j | jgD ]$}y
||= W q tk
r0   Y qX qW dS )z-Remove the observer with connection id *cid*.N)r¼   r½   rW   )r   r0   Úregr   r   r   rX   Â  s
    
zTextBox.disconnectN)r²   r³   r´   rµ   )r   r   r   r   r   rº   rÌ   rÍ   rE   rÁ   r{   rÒ   rÕ   rÖ   rÙ   rD   rÂ   rF   rÚ   rÛ   rX   r   r   r   r   r±   ˜  s&    
D%	r±   c               @   s:   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )ÚRadioButtonsaI  
    A GUI neutral radio button.

    For the buttons to remain responsive you must keep a reference to this
    object.

    Connect to the RadioButtons with the :meth:`on_clicked` method.


    Attributes
    ----------
    ax
        The containing `~.axes.Axes` instance.
    activecolor
        The color of the selected button.
    labels
        A list of `~.text.Text` instances containing the button labels.
    circles
        A list of `~.patches.Circle` instances defining the buttons.
    value_selected : str
        The label text of the currently selected button.

    r   Úbluec          	   C   s4  t j| |ƒ || _d| _|jg ƒ |jg ƒ |jdƒ dt|ƒd  }tj	d| |t|ƒƒ}d}|j
ƒ }|d d }	|	dkr€d}	g | _g | _xˆt||ƒD ]z\}
}|jd	|
||jd
dd�}||krÌ|| _|}n|}td|
f|	d||jd�}| jj|ƒ | jj|ƒ |j|ƒ |d7 }q˜W | jd| jƒ d| _i | _dS )aƒ  
        Add radio buttons to an `~.axes.Axes`.

        Parameters
        ----------
        ax : `~matplotlib.axes.Axes`
            The axes to add the buttons to.
        labels : list of str
            The button labels.
        active : int
            The index of the initially selected button.
        activecolor : color
            The color of the selected button.
        NFg      ð?r   r   rƒ   g{®Gáz„?gš™™™™™©?g      Ð?rd   r8   )r;   r:   r9   g333333Ã?rŒ   )r…   Úradiusr�   r�   r;   r<   )r(   r   ÚactivecolorÚvalue_selectedrI   rJ   rG   r‘   r}   r’   r“   r”   Úcirclesr—   r?   r@   r   r.   r™   r1   r›   rB   rC   )r   r)   r”   r   rà   rœ   r�   rB   rž   Zcircle_radiusrŸ   rA   r    r�   r¤   r   r   r   r   ã  s@    



zRadioButtons.__init__c       	      C   sæ   | j |ƒs |jdks |j| jkr$d S | jjjƒ j|j|jfƒ}t	j
|d |d gƒ}i }xdtt| j| jƒƒD ]N\}\}}|jƒ j|j|jƒs¤t	jj||j ƒ|jk rlt	jj||j ƒ||< qlW t|ƒdkrât||jd�}| j|ƒ d S )Nr   r   )r¶   )r$   r   rO   r)   r@   rÉ   Útransform_pointr£   rŸ   r}   Úarrayr§   r—   râ   r”   r¨   r©   ZlinalgZnormr8   rß   r‘   ÚminÚgetr   )	r   r#   r…   ZpclickedZ	distancesrª   r¤   r    Úclosestr   r   r   r›     s      zRadioButtons._clickedc             C   sÂ   d|  kot | jƒkn  r*td| ƒ‚| j| jƒ | _x:t| jƒD ],\}}||kr^| j}n
| jj	ƒ }|j
|ƒ qFW | jrŠ| jjjjƒ  | js”dS x(| jjƒ D ]\}}|| j| jƒ ƒ q W dS )zv
        Select button with number *index*.

        Callbacks will be triggered if :attr:`eventson` is True.
        r   zInvalid RadioButton index: %dN)r‘   r”   r   r¬   rá   r§   râ   rà   r)   r“   rH   r%   r*   r+   rU   r&   rC   rS   )r   r­   rª   r¤   rK   r0   rT   r   r   r   r   -  s    
zRadioButtons.set_activec             C   s"   | j }|| j|< |  j d7  _ |S )zž
        Connect the callback function *func* to button click events.

        Returns a connection id, which can be used to disconnect the callback.
        r   )rB   rC   )r   rT   r0   r   r   r   rV   G  s    
zRadioButtons.on_clickedc             C   s&   y| j |= W n tk
r    Y nX dS )z-Remove the observer with connection id *cid*.N)rC   rW   )r   r0   r   r   r   rX   R  s    zRadioButtons.disconnectN)r   rÞ   )	r   r   r   r   r   r›   r   rV   rX   r   r   r   r   rÝ   Ë  s   
<rÝ   c               @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )ÚSubplotToolzU
    A tool to adjust the subplot params of a :class:`matplotlib.figure.Figure`.
    c                sF  |ˆ _ ˆjddd� ˆjdƒˆ _ˆ jjdƒ ˆ jjdƒ tˆ jddd	|jjdd
�ˆ _	ˆ j	j
ˆ jƒ ˆjdƒˆ _ˆ jjdƒ tˆ jddd	|jjdd
�ˆ _ˆ jj
ˆ jƒ ˆjdƒˆ _ˆ jjdƒ tˆ jddd	|jjdd�ˆ _ˆ jj
ˆ jƒ ˆjdƒˆ _ˆ jjdƒ tˆ jddd	|jjdd�ˆ _ˆ jj
ˆ jƒ ˆjdƒˆ _ˆ jjdƒ tˆ jddd	|jjdd
�ˆ _ˆ jj
ˆ jƒ ˆjdƒˆ _ˆ jjdƒ tˆ jddd	|jjdd
�ˆ _ˆ jj
ˆ jƒ ˆ jˆ j	_ ˆ j	ˆ j_!ˆ jˆ j_ ˆ jˆ j_!ˆj"ddddgƒ}t#|dƒˆ _$ˆ j	ˆ jˆ jˆ jˆ jˆ jf‰‡ ‡‡fdd„}ˆjj%}dˆj_%ˆ j$j&|ƒ |ˆj_%dS )a  
        *targetfig*
            The figure instance to adjust.

        *toolfig*
            The figure instance to embed the subplot tool into. If
            *None*, a default figure will be created. If you are using
            this from the GUI
        gš™™™™™É?gÍÌÌÌÌÌì?)rd   rc   iÇ  z'Click on slider to adjust subplot paramFrd   r   r   )rj   iÈ  ra   iÉ  rc   )ri   iÊ  rb   iË  ÚwspaceiÌ  Úhspacegš™™™™™é?gš™™™™™©?g333333Ã?g333333³?ZResetc                sŠ   ˆ j }dˆ _ g }xˆD ]}|j|j ƒ d|_ qW xˆD ]}|jƒ  q6W xtˆ|ƒD ]\}}||_ qRW |ˆ _ ˆ j r†ˆjjƒ  ˆ jjjƒ  d S )NF)r%   r.   rˆ   r—   r+   rU   Ú	targetfig)r#   Z
thisdrawonÚbsZsliderÚb)r   ÚslidersÚtoolfigr   r   rT   ¦  s    




z"SubplotTool.__init__.<locals>.funcN)'rë   Úsubplots_adjustZadd_subplotZaxleftÚ	set_titlerG   rY   Zsubplotparsrd   Z
sliderleftr‡   ÚfuncleftZaxbottomra   ZsliderbottomÚ
funcbottomZaxrightrc   ZsliderrightÚ	funcrightZaxtoprb   Z	slidertopÚfunctopZaxwspaceré   ZsliderwspaceÚ
funcwspaceZaxhspacerê   ZsliderhspaceÚ
funchspacerl   rk   Zadd_axesr5   ZbuttonresetÚvalidaterV   )r   rë   rï   ZbaxrT   rø   r   )r   rî   rï   r   r   ^  sj    












zSubplotTool.__init__c             C   s$   | j j|d� | jr | j jjƒ  d S )N)rd   )rë   rð   r%   r+   rU   )r   r]   r   r   r   rò   Ç  s    zSubplotTool.funcleftc             C   s$   | j j|d� | jr | j jjƒ  d S )N)rc   )rë   rð   r%   r+   rU   )r   r]   r   r   r   rô   Ì  s    zSubplotTool.funcrightc             C   s$   | j j|d� | jr | j jjƒ  d S )N)ra   )rë   rð   r%   r+   rU   )r   r]   r   r   r   ró   Ñ  s    zSubplotTool.funcbottomc             C   s$   | j j|d� | jr | j jjƒ  d S )N)rb   )rë   rð   r%   r+   rU   )r   r]   r   r   r   rõ   Ö  s    zSubplotTool.functopc             C   s$   | j j|d� | jr | j jjƒ  d S )N)ré   )rë   rð   r%   r+   rU   )r   r]   r   r   r   rö   Û  s    zSubplotTool.funcwspacec             C   s$   | j j|d� | jr | j jjƒ  d S )N)rê   )rë   rð   r%   r+   rU   )r   r]   r   r   r   r÷   à  s    zSubplotTool.funchspaceN)r   r   r   r   r   rò   rô   ró   rõ   rö   r÷   r   r   r   r   rè   Z  s   irè   c               @   s2   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚCursora  
    A crosshair cursor that spans the axes and moves with mouse cursor.

    For the cursor to remain responsive you must keep a reference to it.

    Parameters
    ----------
    ax : `matplotlib.axes.Axes`
        The `~.axes.Axes` to attach the cursor to.
    horizOn : bool, optional, default: True
        Whether to draw the horizontal line.
    vertOn : bool, optional, default: True
        Whether to draw the vertical line.
    useblit : bool, optional, default: False
        Use blitting for faster drawing if supported by the backend.

    Other Parameters
    ----------------
    **lineprops
        `.Line2D` properties that control the appearance of the lines.
        See also `~.Axes.axhline`.

    Examples
    --------
    See :doc:`/gallery/widgets/cursor`.
    TFc             K   s¦   t j| |ƒ | jd| jƒ | jd| jƒ d| _|| _|| _|oD| jj	| _
| j
rVd|d< |j|jƒ d fddi|—Ž| _|j|jƒ d fddi|—Ž| _d | _d| _d S )Nr>   Ú
draw_eventTÚanimatedr   ÚvisibleF)r(   r   r1   ÚonmoveÚclearrü   ÚhorizOnÚvertOnr+   Úsupports_blitÚuseblitrt   Ú
get_yboundÚlinehru   Ú
get_xboundÚlinevÚ
backgroundÚ	needclear)r   r)   rÿ   r   r  Ú	linepropsr   r   r   r     s      zCursor.__init__c             C   sB   | j |ƒrdS | jr&| jj| jjƒ| _| jjdƒ | j	jdƒ dS )z+Internal event handler to clear the cursor.NF)
r$   r  r+   Úcopy_from_bboxr)   Úbboxr  r  r˜   r  )r   r#   r   r   r   rþ     s    
zCursor.clearc             C   sÊ   | j |ƒrdS | jjj| ƒs dS |j| jkr^| jjdƒ | jjdƒ | j	rZ| jj
ƒ  d| _	dS d| _	| jsndS | jj|j|jfƒ | jj|j|jfƒ | jj| jo¦| jƒ | jj| joº| jƒ | jƒ  dS )z?Internal event handler to draw the cursor when the mouse moves.NFT)r$   r+   Ú
widgetlockr   rO   r)   r  r˜   r  r  rU   rü   Ú	set_xdatar‚   Ú	set_ydatar�   r   rÿ   ry   )r   r#   r   r   r   rý     s&    

zCursor.onmovec             C   sZ   | j rL| jd k	r| jj| jƒ | jj| jƒ | jj| jƒ | jj| jj	ƒ n
| jj
ƒ  dS )NF)r  r  r+   Úrestore_regionr)   Údraw_artistr  r  Úblitr  r†   )r   r   r   r   ry   8  s    

zCursor._updateN)TTF)r   r   r   r   r   rþ   rý   ry   r   r   r   r   rù   æ  s
   
	rù   c               @   sB   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )ÚMultiCursora„  
    Provide a vertical (default) and/or horizontal line cursor shared between
    multiple axes.

    For the cursor to remain responsive you must keep a reference to
    it.

    Example usage::

        from matplotlib.widgets import MultiCursor
        import matplotlib.pyplot as plt
        import numpy as np

        fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True)
        t = np.arange(0.0, 2.0, 0.01)
        ax1.plot(t, np.sin(2*np.pi*t))
        ax2.plot(t, np.sin(4*np.pi*t))

        multi = MultiCursor(fig.canvas, (ax1, ax2), color='r', lw=1,
                            horizOn=False, vertOn=True)
        plt.show()

    TFc                sÎ   || _ || _|| _|| _|d	 jƒ \}}|d
 jƒ \}	}
d||  ‰d|	|
  ‰d| _|o`| j j| _d | _	d| _
| jr~dˆ d< |rš‡ ‡fdd„|D ƒ| _ng | _|r¼‡ ‡fdd„|D ƒ| _ng | _| jƒ  d S )Nr   g      à?TFrû   c                s"   g | ]}|j ˆfd diˆ —Ž‘qS )rü   F)ru   )r®   r)   )r	  Úxmidr   r   r¯   r  s   z(MultiCursor.__init__.<locals>.<listcomp>c                s"   g | ]}|j ˆfd diˆ —Ž‘qS )rü   F)rt   )r®   r)   )r	  Úymidr   r   r¯   x  s   éÿÿÿÿr  )r+   Úaxesrÿ   r   Úget_xlimÚget_ylimrü   r  r  r  r  r¿   ÚhlinesÚconnect)r   r+   r  r  rÿ   r   r	  ÚxminÚxmaxÚyminÚymaxr   )r	  r  r  r   r   \  s.    zMultiCursor.__init__c             C   s(   | j jd| jƒ| _| j jd| jƒ| _dS )zconnect eventsr>   rú   N)r+   r-   rý   Ú
_cidmotionrþ   Ú_ciddraw)r   r   r   r   r    s    
zMultiCursor.connectc             C   s    | j j| jƒ | j j| jƒ dS )zdisconnect eventsN)r+   r2   r  r   )r   r   r   r   rX   …  s    zMultiCursor.disconnectc             C   sL   | j |ƒrdS | jr(| jj| jjjƒ| _x| j| j D ]}|j	dƒ q6W dS )zclear the cursorNF)
r$   r  r+   r
  r*   r  r  r¿   r  r˜   )r   r#   Úliner   r   r   rþ   Š  s    
zMultiCursor.clearc             C   s²   | j |ƒrd S |jd krd S | jjj| ƒs.d S d| _| js>d S | jrrx,| jD ]"}|j	|j
|j
fƒ |j| jƒ qLW | jr¦x,| jD ]"}|j|j|jfƒ |j| jƒ q€W | jƒ  d S )NT)r$   rO   r+   r  r   r  rü   r   r¿   r  r‚   r˜   rÿ   r  r  r�   ry   )r   r#   r!  r   r   r   rý   ”  s$    

zMultiCursor.onmovec             C   s˜   | j rŠ| jd k	r| jj| jƒ | jrJx$t| j| jƒD ]\}}|j|ƒ q4W | j	rvx$t| j| j
ƒD ]\}}|j|ƒ q`W | jj| jjjƒ n
| jjƒ  d S )N)r  r  r+   r  r   r—   r  r¿   r  rÿ   r  r  r*   r  r†   )r   r)   r!  r   r   r   ry   ¨  s    
zMultiCursor._updateN)TFT)
r   r   r   r   r   r  rX   rþ   rý   ry   r   r   r   r   r  D  s   
#
r  c               @   s¶   e Zd Zd-dd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ ZdS ).Ú_SelectorWidgetFNc             C   s”   t j| |ƒ d| _|| _|o"| jj| _| jƒ  tddddd�| _	| j	j
|pLi ƒ d | _g | _t|tƒrp|g| _n|| _d | _d | _d | _tƒ | _d S )NTrÆ   ÚescapeÚshiftÚcontrol)Úmoverþ   Úsquarer8   )r(   r   rü   Úonselectr+   r  r  Úconnect_default_eventsÚdictÚstate_modifier_keysÚupdater  ÚartistsÚ
isinstancer   ÚvalidButtonsÚ
eventpressÚeventreleaseÚ_prev_eventÚsetÚstate)r   r)   r(  r  r   r+  r   r   r   r   ¹  s"    

z_SelectorWidget.__init__c             C   s   t j| |ƒ |r| jd ƒ d S )N)r(   r   Úupdate_background)r   r   r   r   r   r   Õ  s    z_SelectorWidget.set_activec             C   s   | j r| jj| jjƒ| _dS )z!force an update of the backgroundN)r  r+   r
  r)   r  r  )r   r#   r   r   r   r5  Ú  s    z!_SelectorWidget.update_backgroundc             C   sf   | j d| jƒ | j d| jƒ | j d| jƒ | j d| jƒ | j d| jƒ | j d| jƒ | j d| jƒ dS )	z+Connect the major canvas events to methods.r>   r<   r=   rú   r·   Zkey_release_eventZscroll_eventN)r1   rý   Úpressr   r5  Úon_key_pressÚon_key_releaseÚ	on_scroll)r   r   r   r   r)  á  s    z&_SelectorWidget.connect_default_eventsc             C   s–   | j  s| jjƒ  rdS | jjj| ƒs*dS t|dƒs:d|_| jdk	rT|j| jkrTdS | j	dkrj|j
| jkS |j| j	jkr|dS |j
| jkp”|j| j	jkS )z*return *True* if *event* should be ignoredTr   NF)r   r)   r«   r+   r  r   re   r   r/  r0  rO   )r   r#   r   r   r   r$   ë  s    


z_SelectorWidget.ignorec             C   sh   | j jƒ sdS | jrZ| jdk	r,| jj| jƒ x| jD ]}| j j|ƒ q4W | jj| j j	ƒ n
| jj
ƒ  dS )zTdraw using newfangled blit or oldfangled draw depending on
        useblit

        FN)r)   r«   r  r  r+   r  r-  r  r  r  r†   )r   Úartistr   r   r   r,    s    


z_SelectorWidget.updatec             C   s^   |j dkrdS | jjƒ \}}| jjƒ \}}t||j ƒ}t||ƒ}t||jƒ}t||ƒ}||fS )z.Get the xdata and ydata for event, with limitsN)NN)r‚   r)   r  r  Úmaxrå   r�   )r   r#   Úx0Úx1Úy0Úy1r‚   r�   r   r   r   Ú	_get_data   s    


z_SelectorWidget._get_datac             C   s8   |j dkr| j}n
tj|ƒ}| j|ƒ\|_ |_|| _|S )z—Clean up an event

        Use prev event if there is no xdata
        Limit the xdata and ydata to the axes limits
        Set the prev event
        N)r‚   r2  Úcopyr@  r�   )r   r#   r   r   r   Ú_clean_event,  s    

z_SelectorWidget._clean_eventc             C   sb   | j |ƒs^| j|ƒ}|| _|| _|jp(d}|jddƒ}|| jd krP| jjdƒ | j	|ƒ dS dS )z"Button press handler and validatorr²   Úctrlr%  r&  TF)
r$   rB  r0  r2  r¶   Úreplacer+  r4  ÚaddÚ_press)r   r#   r¶   r   r   r   r6  <  s    



z_SelectorWidget.pressc             C   s   dS )zButton press handlerNr   )r   r#   r   r   r   rF  K  s    z_SelectorWidget._pressc             C   sL   | j |ƒ rH| jrH| j|ƒ}|| _| j|ƒ d| _d| _| jjdƒ dS dS )z*Button release event handler and validatorNr&  TF)r$   r0  rB  r1  rE   r4  Údiscard)r   r#   r   r   r   r   O  s    

z_SelectorWidget.releasec             C   s   dS )zButton release event handlerNr   )r   r#   r   r   r   rE   [  s    z_SelectorWidget._releasec             C   s.   | j |ƒ r*| jr*| j|ƒ}| j|ƒ dS dS )z'Cursor move event handler and validatorTF)r$   r0  rB  Ú_onmove)r   r#   r   r   r   rý   _  s
    

z_SelectorWidget.onmovec             C   s   dS )zCursor move event handlerNr   )r   r#   r   r   r   rH  g  s    z_SelectorWidget._onmovec             C   s   | j |ƒs| j|ƒ dS )z(Mouse scroll event handler and validatorN)r$   Ú
_on_scroll)r   r#   r   r   r   r9  k  s    
z_SelectorWidget.on_scrollc             C   s   dS )zMouse scroll event handlerNr   )r   r#   r   r   r   rI  p  s    z_SelectorWidget._on_scrollc             C   sŠ   | j r†|jpd}|jddƒ}|| jd krPx| jD ]}|jdƒ q2W | jƒ  dS x*| jjƒ D ]\}}||kr\| jj	|ƒ q\W | j
|ƒ dS )z?Key press event handler and validator for all selection widgetsr²   rC  r%  rþ   FN)r   r¶   rD  r+  r-  r˜   r,  rS   r4  rE  Ú_on_key_press)r   r#   r¶   r:  r4  Úmodifierr   r   r   r7  t  s    
z_SelectorWidget.on_key_pressc             C   s   dS )zMKey press event handler - use for widget-specific key press actions.
        Nr   )r   r#   r   r   r   rJ  ƒ  s    z_SelectorWidget._on_key_pressc             C   sJ   | j rF|jpd}x*| jjƒ D ]\}}||kr| jj|ƒ qW | j|ƒ dS )z(Key release event handler and validator.r²   N)r   r¶   r+  rS   r4  rG  Ú_on_key_release)r   r#   r¶   r4  rK  r   r   r   r8  ˆ  s    
z_SelectorWidget.on_key_releasec             C   s   dS )zKey release event handler.Nr   )r   r#   r   r   r   rL  ‘  s    z_SelectorWidget._on_key_releasec             C   s$   || _ x| jD ]}|j|ƒ qW dS )z"Set the visibility of our artists.N)rü   r-  r˜   )r   rü   r:  r   r   r   r˜   ”  s    z_SelectorWidget.set_visible)FNN)r   r   r   r   r   r5  r)  r$   r,  r@  rB  r6  rF  r   rE   rý   rH  r9  rI  r7  rJ  r8  rL  r˜   r   r   r   r   r"  ·  s,    

!	r"  c               @   s`   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Ze	j
dƒedd„ ƒƒZdd„ Zdd„ ZdS )ÚSpanSelectora(  
    Visually select a min/max range on a single axis and call a function with
    those values.

    To guarantee that the selector remains responsive, keep a reference to it.

    In order to turn off the SpanSelector, set `span_selector.active=False`. To
    turn it back on, set `span_selector.active=True`.

    Parameters
    ----------
    ax :  :class:`matplotlib.axes.Axes` object

    onselect : func(min, max), min/max are floats

    direction : "horizontal" or "vertical"
      The axis along which to draw the span selector

    minspan : float, default is None
     If selection is less than *minspan*, do not call *onselect*

    useblit : bool, default is False
      If True, use the backend-dependent blitting features for faster
      canvas updates.

    rectprops : dict, default is None
      Dictionary of :class:`matplotlib.patches.Patch` properties

    onmove_callback : func(min, max), min/max are floats, default is None
      Called on mouse move while the span is being selected

    span_stays : bool, default is False
      If True, the span stays visible after the mouse is released

    button : int or list of ints
      Determines which mouse buttons activate the span selector
        1 = left mouse button

        2 = center mouse button (scroll wheel)

        3 = right mouse button


    Examples
    --------
    >>> import matplotlib.pyplot as plt
    >>> import matplotlib.widgets as mwidgets
    >>> fig, ax = plt.subplots()
    >>> ax.plot([1, 2, 3], [10, 50, 100])
    >>> def onselect(vmin, vmax):
    ...     print(vmin, vmax)
    >>> rectprops = dict(facecolor='blue', alpha=0.5)
    >>> span = mwidgets.SpanSelector(ax, onselect, 'horizontal',
    ...                              rectprops=rectprops)
    >>> fig.show()

    See also: :doc:`/gallery/widgets/span_selector`

    NFc
       
      C   sˆ   t j| ||||	d� |d kr(tddd�}| j|d< tjddg|d� || _d | _d | _|| _	|| _
|| _|| _d
| _d | _| j|ƒ d S )N)r  r   Úredg      à?)r�   Úalpharû   r\   r^   )Ú	directionr   )r   r   )r"  r   r*  r  r   Ú_check_in_listrP  ÚrectÚpressvÚ	rectpropsÚonmove_callbackÚminspanÚ
span_staysÚprevr+   Únew_axes)
r   r)   r(  rP  rV  r  rT  rU  rW  r   r   r   r   r   Õ  s     
zSpanSelector.__init__c             C   sð   || _ | j|jjk	r8| jdk	r&| jƒ  |jj| _| jƒ  | jdkr^t| j j| j jƒ}d\}}nt| j j| j jƒ}d\}}t	d	||f|ddœ| j
—Ž| _| jrÔt	d
||f|ddœ| j
—Ž| _| jjdƒ | j j| jƒ | j j| jƒ | jg| _dS )z)Set SpanSelector to operate on a new AxesNr\   r   r   F)r;   rü   )r   r   )r   r   )r   r   )r   r   )r)   r+   r*   r4   r)  rP  r
   rÈ   r@   r   rT  rR  rW  Ú	stay_rectÚset_animatedr™   r-  )r   r)   rØ   r¡   r¢   r   r   r   rY  ó  s4    







zSpanSelector.new_axesc             C   s   t j| |ƒp| j S )z*return *True* if *event* should be ignored)r"  r$   rü   )r   r#   r   r   r   r$     s    zSpanSelector.ignorec             C   sd   | j j| jƒ | jr0| jjdƒ | jr0| jjƒ  | j|ƒ\}}| j	dkrP|| _
n|| _
| j|ƒ dS )zon button press eventFr\   )rR  r˜   rü   rW  rZ  r  r+   rU   r@  rP  rS  Ú_set_span_xy)r   r#   r‚   r�   r   r   r   rF    s    


zSpanSelector._pressc             C   s  | j dkrdS | jjdƒ | jrt| jj| jjƒ ƒ | jj| jjƒ ƒ | jj	| jj
ƒ ƒ | jj| jjƒ ƒ | jjdƒ | jjƒ  | j }| j|ƒ\}}| jdkr¬|p¨| jd }n|p¸| jd }||krÌ|| }}|| }| jdk	rì|| jk rìdS | j||ƒ d| _ dS )zon button release eventNFTr\   r   r   )rS  rR  r˜   rW  rZ  Úset_xÚget_xÚset_yÚget_yÚ	set_widthÚ	get_widthÚ
set_heightÚ
get_heightr+   r†   r@  rP  rX  rV  r(  )r   r#   Úvminr‚   r�   ÚvmaxÚspanr   r   r   rE   *  s.    



zSpanSelector._releasez3.1c             C   s   dS )NFr   )r   r   r   r   Ú
buttonDownI  s    zSpanSelector.buttonDownc             C   sˆ   | j dkrdS | j|ƒ | jdk	r|| j }| j|ƒ\}}| jdkrP|pL| jd }n|p\| jd }||krp|| }}| j||ƒ | jƒ  dS )zon motion notify eventNr\   r   r   F)rS  r\  rU  r@  rP  rX  r,  )r   r#   re  r‚   r�   rf  r   r   r   rH  N  s    




zSpanSelector._onmovec             C   sž   | j |ƒ\}}|dkrdS ||f| _| jdkr4|}n|}|| j }}||krV|| }}| jdkr~| jj|ƒ | jj|| ƒ n| jj|ƒ | jj|| ƒ dS )zSetting the span coordinatesNr\   )	r@  rX  rP  rS  rR  r]  ra  r_  rc  )r   r#   r£   rŸ   ÚvZminvZmaxvr   r   r   r\  d  s    



zSpanSelector._set_span_xy)NFNNFN)r   r   r   r   r   rY  r$   rF  rE   r   Ú
deprecatedr'   rh  rH  r\  r   r   r   r   rM  ›  s   8  
!rM  c               @   sT   e Zd ZdZddd„Zedd„ ƒZed	d
„ ƒZddd„Zdd„ Z	dd„ Z
dd„ ZdS )ÚToolHandlesa¦  Control handles for canvas tools.

    Parameters
    ----------
    ax : :class:`matplotlib.axes.Axes`
        Matplotlib axes where tool handles are displayed.
    x, y : 1D arrays
        Coordinates of control handles.
    marker : str
        Shape of marker used to display handle. See `matplotlib.pyplot.plot`.
    marker_props : dict
        Additional marker properties. See :class:`matplotlib.lines.Line2D`.
    r   NTc          	   C   sd   || _ t|ddddddd�}|j|d k	r,|ni ƒ t||fd|i|—Ž| _| j j| jƒ | j| _d S )	Né   r¡   Únoneg      à?FZ
_nolegend_)ÚmarkerZ
markersizeÚmfcZlsrO  rü   rA   rû   )r)   r*  r,  r   Ú_markersrš   r:  )r   r)   r£   rŸ   rn  Úmarker_propsr  Úpropsr   r   r   r   Š  s    
zToolHandles.__init__c             C   s
   | j jƒ S )N)rp  Z	get_xdata)r   r   r   r   r£   ”  s    zToolHandles.xc             C   s
   | j jƒ S )N)rp  Z	get_ydata)r   r   r   r   rŸ   ˜  s    zToolHandles.yc             C   s*   |dk	r|}t j||gƒ}| jj|ƒ dS )z Set x and y positions of handlesN)r}   rä   rp  Úset_data)r   ÚptsrŸ   r£   r   r   r   rs  œ  s    zToolHandles.set_datac             C   s   | j j|ƒ d S )N)rp  r˜   )r   r]   r   r   r   r˜   £  s    zToolHandles.set_visiblec             C   s   | j j|ƒ d S )N)rp  r[  )r   r]   r   r   r   r[  ¦  s    zToolHandles.set_animatedc             C   s~   t j| j| jfƒ}| jjj|ƒ}|||f }|jdkrbt jt j	|d dd�ƒ}t j
|ƒt j|ƒfS dt jt j	|d ƒƒfS dS )z1Return index and pixel distance to closest index.rƒ   r   )Zaxisr   N)r}   Z	transposer£   rŸ   r)   rÈ   r;   ÚndimÚsqrtÚsumZargminrå   )r   r£   rŸ   rt  ZdiffÚdistr   r   r   rç   ©  s    
zToolHandles.closest)r   NT)N)r   r   r   r   r   r'   r£   rŸ   rs  r˜   r[  rç   r   r   r   r   rk  {  s   


rk  c               @   sœ   e Zd ZdZeZd dd„Zd	d
„ Zdd„ Zdd„ Z	e
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZe
dd„ ƒZejdd„ ƒZdd„ Zdd„ Ze
dd„ ƒZdS )!ÚRectangleSelectora  
    Select a rectangular region of an axes.

    For the cursor to remain responsive you must keep a reference to
    it.

    Example usage::

        import numpy as np
        import matplotlib.pyplot as plt
        from matplotlib.widgets import RectangleSelector

        def onselect(eclick, erelease):
            "eclick and erelease are matplotlib events at press and release."
            print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata))
            print('endposition  : (%f, %f)' % (erelease.xdata, erelease.ydata))
            print('used button  : ', eclick.button)

        def toggle_selector(event):
            print('Key pressed.')
            if event.key in ['Q', 'q'] and toggle_selector.RS.active:
                print('RectangleSelector deactivated.')
                toggle_selector.RS.set_active(False)
            if event.key in ['A', 'a'] and not toggle_selector.RS.active:
                print('RectangleSelector activated.')
                toggle_selector.RS.set_active(True)

        x = np.arange(100.) / 99
        y = np.sin(x)
        fig, ax = plt.subplots()
        ax.plot(x, y)

        toggle_selector.RS = RectangleSelector(ax, onselect, drawtype='line')
        fig.canvas.mpl_connect('key_press_event', toggle_selector)
        plt.show()
    ÚboxNFÚdataé
   c             C   s  t j| ||||
|d� d| _d| _|| _|dkr:d}d| _|dkr�|dkrZtdd	d
dd�}| j|d< || _| jd&ddi| j—Ž| _| j	j
| jƒ |dkrð|dkr°td	dddd�}| j|d< || _tddgddgfddi| j—Ž| _| j	j| jƒ || _|| _tjddg|	d� |	| _|| _|| _|dk�r6tdd�}nt|jddƒd�}ddddg| _| j\}}t| j	|||| jd�| _dd d!d"g| _| j\}}t| j	||d#|| jd$�| _| j\}}t| j	|g|gd#|| jd$�| _d| _| j| jj | jj | jj g| _!| j�s| jg| _!d| _"dS )'aä  
        Create a selector in *ax*.  When a selection is made, clear
        the span and call onselect with::

          onselect(pos_1, pos_2)

        and clear the drawn box/line. The ``pos_1`` and ``pos_2`` are
        arrays of length 2 containing the x- and y-coordinate.

        If *minspanx* is not *None* then events smaller than *minspanx*
        in x direction are ignored (it's the same for y).

        The rectangle is drawn with *rectprops*; default::

          rectprops = dict(facecolor='red', edgecolor = 'black',
                           alpha=0.2, fill=True)

        The line is drawn with *lineprops*; default::

          lineprops = dict(color='black', linestyle='-',
                           linewidth = 2, alpha=0.5)

        Use *drawtype* if you want the mouse to draw a line,
        a box or nothing between click and actual position by setting

        ``drawtype = 'line'``, ``drawtype='box'`` or ``drawtype = 'none'``.
        Drawing a line would result in a line from vertex A to vertex C in
        a rectangle ABCD.

        *spancoords* is one of 'data' or 'pixels'.  If 'data', *minspanx*
        and *minspanx* will be interpreted in the same coordinates as
        the x and y axis. If 'pixels', they are in pixels.

        *button* is a list of integers indicating which mouse buttons should
        be used for rectangle selection.  You can also specify a single
        integer if only a single button is desired.  Default is *None*,
        which does not limit which button can be used.

        Note, typically:
         1 = left mouse button
         2 = center mouse button (scroll wheel)
         3 = right mouse button

        *interactive* will draw a set of handles and allow you interact
        with the widget after it is drawn.

        *state_modifier_keys* are keyboard modifiers that affect the behavior
        of the widget.

        The defaults are:
        dict(move=' ', clear='escape', square='shift', center='ctrl')

        Keyboard modifiers, which:
        'move': Move the existing shape.
        'clear': Clear the current shape.
        'square': Makes the shape square.
        'center': Make the initial point the center of the shape.
        'square' and 'center' can be combined.
        )r  r   r+  NTrm  r!  Frz  rN  rŒ   gš™™™™™É?)r�   r�   rO  Úfillrû   r   r   rü   ú-rƒ   g      à?)rK   Ú	linestyler‹   rO  r{  Úpixels)Ú
spancoordsr_   )Úmecr�   ÚNWÚNEZSEÚSW)rq  r  ÚWÚNÚEÚSÚs)rn  rq  r  ©r   r   )r‹  r   r   )#r"  r   Úto_drawrü   Úinteractiver*  r  rT  Ú_shape_klassr)   r™   r	  r   rš   ÚminspanxÚminspanyr   rQ  r�  ÚdrawtypeÚmaxdistræ   Ú_corner_orderÚcornersrk  Ú_corner_handlesÚ_edge_orderÚedge_centersÚ_edge_handlesr8   Ú_center_handleÚactive_handler:  r-  Ú_extents_on_press)r   r)   r(  r‘  r�  r�  r  r	  rT  r�  r   r’  rq  r�  r+  rr  ÚxcÚycÚxeÚyer   r   r   r   Þ  sp    A









zRectangleSelector.__init__c             C   sl   | j r| jjƒ r| j|ƒ nd| _| jdks4| j  r<| jƒ  | j s\|j}|j}||||f| _| j	| j
ƒ dS )zon button press eventN)r�  rŒ  r«   Ú_set_active_handlerš  r,  r‚   r�   Úextentsr˜   rü   )r   r#   r£   rŸ   r   r   r   rF  f  s    zRectangleSelector._pressc             C   s¸  | j s| jjdƒ | j\}}}}|| j_|| j_| jjj	||gƒ}|\| j_
| j_|| j_|| j_| jjj	||gƒ}|\| j_
| j_| jdkr´| jj| jj }}	| jj| jj }
}nD| jdkrä| jj
| jj }}	| jj
| jj }
}ntjddg| jd� ||
k�r|
| }}
|	|k�r ||	 }	}|
| }||	 }| jdk	�oD|| jk }| jdk	�oZ|| jk }| jdk�rœ|�st|�rœx| jD ]}|jdƒ �q|W | jƒ  dS | j| j| jƒ | jƒ  dS )zon button release eventFr{  r€  )r�  Nrm  )r�  rŒ  r˜   r¡  r0  r‚   r�   r)   rÈ   rã   r£   rŸ   r1  r�  r   rQ  r�  r�  r‘  r-  r,  r(  )r   r#   r=  Úx2r?  Úy2Zxy1Zxy2r  r  r  r  ZspanxZspanyZ	xproblemsZ	yproblemsr:  r   r   r   rE   z  sF    







zRectangleSelector._releasec             C   s  | j rX| j dk rX| j\}}}}| j ddg| j kr:|j}| j ddg| j krT|j}�n²d| jksl| j dkrÄ| jdk	rÄ| j\}}}}|j| jj }|j| jj }||7 }||7 }||7 }||7 }�nF| jj| jjg}| jj| jjg}	|j|d  d	 }|j|d
  d	 }d| jk�r˜t	|j|	d  ƒ}
t	|j|	d
  ƒ}|
�sBdS t
t	|
ƒt	|ƒƒ}t	|
ƒ|k �rv||t	|
ƒd  9 }t	|ƒ|k �r˜||t	|ƒd  9 }d| jk�r¶|d9 }|d9 }n |d  |7  < |d
  |7  < |d | |d | |d
 | |d
 | f\}}}}||||f| _dS )z,on motion notify event if box/line is wantedÚCrˆ  r†  r‡  r‰  r&  Nr   g       @r   r'  g�íµ ÷Æ°>r8   rƒ   )rš  r›  r“  r‚   r�   r4  r0  r£   rŸ   Úabsr;  r¡  )r   r#   r=  r¢  r?  r£  Údxrœ   r8   Z
center_pixZdx_pixZdy_pixZmaxdr   r   r   rH  ®  sJ    


 zRectangleSelector._onmovec       	      C   sˆ   | j dkr>| jjƒ }| jjƒ }| jjƒ }| jjƒ }||||fS | jjƒ \}}t|ƒt|ƒ }}t|ƒt|ƒ }}|||| || fS d S )Nrz  )	r‘  rŒ  r^  r`  rb  rd  Úget_datarå   r;  )	r   r<  r>  r�   rŽ   r£   rŸ   r=  r?  r   r   r   Ú
_rect_bboxå  s    




zRectangleSelector._rect_bboxc             C   s>   | j \}}}}||| || |f}|||| || f}||fS )z7Corners of rectangle from lower left, moving clockwise.)r¨  )r   r<  r>  r�   rŽ   rœ  r�  r   r   r   r”  ó  s    zRectangleSelector.cornersc       	      C   sV   | j \}}}}|d }|d }||| || || f}|| ||| || f}||fS )z8Midpoint of rectangle edges from left, moving clockwise.g       @)r¨  )	r   r<  r>  r�   rŽ   r¡   r¢   rž  rŸ  r   r   r   r—  û  s    zRectangleSelector.edge_centersc             C   s&   | j \}}}}||d  ||d  fS )zCenter of rectangleg       @)r¨  )r   r<  r>  r�   rŽ   r   r   r   r8   	  s    zRectangleSelector.centerc       	      C   sB   | j \}}}}t||| gƒ\}}t||| gƒ\}}||||fS )z Return (xmin, xmax, ymin, ymax).)r¨  Úsorted)	r   r<  r>  r�   rŽ   r  r  r  r  r   r   r   r¡  	  s    zRectangleSelector.extentsc             C   sL   | j |ƒ | jj| jŽ  | jj| jŽ  | jj| jŽ  | j| j	ƒ | j
ƒ  d S )N)Ú
draw_shaper•  rs  r”  r˜  r—  r™  r8   r˜   rü   r,  )r   r¡  r   r   r   r¡  	  s    
c             C   sè   |\}}}}t ||gƒ\}}t ||gƒ\}}	t | jjƒ ƒ}
t | jjƒ ƒ}t|
d |ƒ}t|d |ƒ}t||
d ƒ}t|	|d ƒ}	| jdkrÄ| jj|ƒ | jj	|ƒ | jj
|| ƒ | jj|	| ƒ n | jdkrä| jj||g||	gƒ d S )Nr   r   rz  r!  )r©  r)   r  r  r;  rå   r‘  rŒ  r]  r_  ra  rc  rs  )r   r¡  r<  r=  r>  r?  r  r  r  r  ZxlimZylimr   r   r   rª  	  s     

zRectangleSelector.draw_shapec             C   sþ   | j j|j|jƒ\}}| jj|j|jƒ\}}| jj|j|jƒ\}}d| jkr\d| _| j| _	nV|| j
d k rrd| _n@|| j
kr�|| j
kr�d| _dS ||k r¦| j| | _n| j| | _| j\}}	}
}| jd
krÖ|	|j }}	| jdkrì||j }
}||	|
|f| _	dS )z:Set active handle based on the location of the mouse eventr&  r¤  rƒ   Nr†  r…  rƒ  r‡  r„  )r†  r…  rƒ  )r‡  rƒ  r„  )r•  rç   r£   rŸ   r˜  r™  r4  rš  r¡  r›  r’  r“  r–  r‚   r�   )r   r#   Zc_idxZc_distZe_idxZe_distZm_idxZm_distr=  r¢  r?  r£  r   r   r   r   3	  s(    



z$RectangleSelector._set_active_handlec             C   sP   t | jdƒr<| jjjƒ }|j| jjƒ ƒj\}}tj	||gƒS tj	| jj
ƒ ƒS dS )a  
        Returns numpy.ndarray of shape (2,5) containing
        x (``RectangleSelector.geometry[1,:]``) and
        y (``RectangleSelector.geometry[0,:]``)
        coordinates of the four corners of the rectangle starting
        and ending in the top left corner.
        Ú	get_vertsN)re   rŒ  r)   rÈ   rÉ   r;   r«  ÚTr}   rä   r§  )r   ZxfmrŸ   r£   r   r   r   ÚgeometryR	  s
    	zRectangleSelector.geometry)rz  NNFNNr{  Nr|  NFN)r   r   r   r   r   rŽ  r   rF  rE   rH  r'   r¨  r”  r—  r8   r¡  Úsetterrª  r   r­  r   r   r   r   ry  ¶  s(   $    
 47
ry  c               @   s(   e Zd ZdZeZdd„ Zedd„ ƒZdS )ÚEllipseSelectora  
    Select an elliptical region of an axes.

    For the cursor to remain responsive you must keep a reference to
    it.

    Example usage::

        import numpy as np
        import matplotlib.pyplot as plt
        from matplotlib.widgets import EllipseSelector

        def onselect(eclick, erelease):
            "eclick and erelease are matplotlib events at press and release."
            print('startposition: (%f, %f)' % (eclick.xdata, eclick.ydata))
            print('endposition  : (%f, %f)' % (erelease.xdata, erelease.ydata))
            print('used button  : ', eclick.button)

        def toggle_selector(event):
            print(' Key pressed.')
            if event.key in ['Q', 'q'] and toggle_selector.ES.active:
                print('EllipseSelector deactivated.')
                toggle_selector.RS.set_active(False)
            if event.key in ['A', 'a'] and not toggle_selector.ES.active:
                print('EllipseSelector activated.')
                toggle_selector.ES.set_active(True)

        x = np.arange(100.) / 99
        y = np.sin(x)
        fig, ax = plt.subplots()
        ax.plot(x, y)

        toggle_selector.ES = EllipseSelector(ax, onselect, drawtype='line')
        fig.canvas.mpl_connect('key_press_event', toggle_selector)
        plt.show()
    c             C   sâ   |\}}}}t ||gƒ\}}t ||gƒ\}}	||| d  ||| d  g}
|| d }|	| d }| jdkr�|
| j_d| | j_d| | j_nNtjtjdƒd ƒ}|tj	|ƒ |
d  }|tj
|ƒ |
d  }| jj||ƒ d S )Ng       @rz  rƒ   é   é   r   r   )r©  r‘  rŒ  r8   r�   rŽ   r}   Zdeg2radZarangeÚcosÚsinrs  )r   r¡  r=  r¢  r?  r£  r  r  r  r  r8   Úarí   Zradr£   rŸ   r   r   r   rª  Š	  s     
zEllipseSelector.draw_shapec       	      C   sŒ   | j dkrB| jj\}}| jj}| jj}||d  ||d  ||fS | jjƒ \}}t|ƒt|ƒ }}t|ƒt|ƒ }}|||| || fS d S )Nrz  g       @)r‘  rŒ  r8   r�   rŽ   r§  rå   r;  )	r   r£   rŸ   r�   rŽ   r<  r=  r>  r?  r   r   r   r¨  œ	  s    
zEllipseSelector._rect_bboxN)	r   r   r   r   r	   rŽ  rª  r'   r¨  r   r   r   r   r¯  c	  s   $r¯  c               @   sB   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dS )ÚLassoSelectora   
    Selection curve of an arbitrary shape.

    For the selector to remain responsive you must keep a reference to it.

    The selected path can be used in conjunction with `~.Path.contains_point`
    to select data points from an image.

    In contrast to `Lasso`, `LassoSelector` is written with an interface
    similar to `RectangleSelector` and `SpanSelector`, and will continue to
    interact with the axes until disconnected.

    Example usage::

        ax = subplot(111)
        ax.plot(x,y)

        def onselect(verts):
            print(verts)
        lasso = LassoSelector(ax, onselect)

    Parameters
    ----------
    ax : :class:`~matplotlib.axes.Axes`
        The parent axes for the widget.
    onselect : function
        Whenever the lasso is released, the *onselect* function is called and
        passed the vertices of the selected path.
    button : List[Int], optional
        A list of integers indicating which mouse buttons should be used for
        rectangle selection. You can also specify a single integer if only a
        single button is desired.  Default is ``None``, which does not limit
        which button can be used.

        Note, typically:

        - 1 = left mouse button
        - 2 = center mouse button (scroll wheel)
        - 3 = right mouse button

    NTc             C   sl   t j| ||||d� d | _|d kr(tƒ }|r4d|d< tg g f|Ž| _| jjdƒ | jj| jƒ | jg| _	d S )N)r  r   Trû   F)
r"  r   Úvertsr*  r   r!  r˜   r)   rš   r-  )r   r)   r(  r  r	  r   r   r   r   r   Õ	  s    zLassoSelector.__init__c             C   s   | j |ƒ d S )N)r6  )r   r#   r   r   r   Úonpresså	  s    zLassoSelector.onpressc             C   s   | j |ƒg| _| jjdƒ d S )NT)r@  r¶  r!  r˜   )r   r#   r   r   r   rF  è	  s    zLassoSelector._pressc             C   s   | j |ƒ d S )N)r   )r   r#   r   r   r   Ú	onreleaseì	  s    zLassoSelector.onreleasec             C   sN   | j d k	r(| j j| j|ƒƒ | j| j ƒ | jjg g gƒ | jjdƒ d | _ d S )NF)r¶  r.   r@  r(  r!  rs  r˜   )r   r#   r   r   r   rE   ï	  s    
zLassoSelector._releasec             C   sB   | j d krd S | j j| j|ƒƒ | jjtt| j Ž ƒƒ | jƒ  d S )N)r¶  r.   r@  r!  rs  Úlistr—   r,  )r   r#   r   r   r   rH  ÷	  s
    
zLassoSelector._onmove)NTNN)
r   r   r   r   r   r·  rF  r¸  rE   rH  r   r   r   r   rµ  ª	  s   ) 
rµ  c               @   s^   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Zedd„ ƒZdS )ÚPolygonSelectoraq  Select a polygon region of an axes.

    Place vertices with each mouse click, and make the selection by completing
    the polygon (clicking on the first vertex). Hold the *ctrl* key and click
    and drag a vertex to reposition it (the *ctrl* key is not necessary if the
    polygon has already been completed). Hold the *shift* key and click and
    drag anywhere in the axes to move all vertices. Press the *esc* key to
    start a new polygon.

    For the selector to remain responsive you must keep a reference to
    it.

    Parameters
    ----------
    ax : :class:`~matplotlib.axes.Axes`
        The parent axes for the widget.
    onselect : function
        When a polygon is completed or modified after completion,
        the `onselect` function is called and passed a list of the vertices as
        ``(xdata, ydata)`` tuples.
    useblit : bool, optional
    lineprops : dict, optional
        The line for the sides of the polygon is drawn with the properties
        given by `lineprops`. The default is ``dict(color='k', linestyle='-',
        linewidth=2, alpha=0.5)``.
    markerprops : dict, optional
        The markers for the vertices of the polygon are drawn with the
        properties given by `markerprops`. The default is ``dict(marker='o',
        markersize=7, mec='k', mfc='k', alpha=0.5)``.
    vertex_select_radius : float, optional
        A vertex is selected (to complete the polygon or to move a vertex)
        if the mouse click is within `vertex_select_radius` pixels of the
        vertex. The default radius is 15 pixels.

    Examples
    --------
    :doc:`/gallery/widgets/polygon_selector_demo`
    FNé   c             C   sæ   t ddddddd�}tj| ||||d� dgdg | _| _d| _|d krXt d	d
ddd�}| j|d< t| j| jf|Ž| _| j	j
| jƒ |d kr t d	|jdd	ƒd�}t| j	| j| j| j|d�| _d| _|| _| j| jjg| _| jdƒ d S )Nr#  r%  r$  znot-applicable)rþ   Úmove_vertexÚmove_allr&  r'  r8   )r  r+  r   FrŠ   r~  rƒ   g      à?)rK   r  r‹   rO  rû   rK   )r‚  ro  )r  rq  r   Tr  )r*  r"  r   Ú_xsÚ_ysÚ_polygon_completedr  r   r!  r)   rš   ræ   rk  Ú_polygon_handlesÚ_active_handle_idxÚvertex_select_radiusr:  r-  r˜   )r   r)   r(  r  r	  ZmarkerpropsrÃ  r+  r   r   r   r   )
  s,    

zPolygonSelector.__init__c             C   sj   | j sd| jkrDt| jƒdkrD| jj|j|jƒ\}}|| jk rD|| _	| jdd… | j
dd…  | _| _dS )zButton press event handlerr¼  r   N)rÀ  r4  r‘   r¾  rÁ  rç   r£   rŸ   rÃ  rÂ  r¿  Ú_xs_at_pressÚ_ys_at_press)r   r#   Zh_idxZh_distr   r   r   rF  L
  s    
zPolygonSelector._pressc             C   s¢   | j dkrd| _ nzt| jƒdkrP| jd	 | jd krP| jd
 | jd krPd| _n<| j rŒd| jkrŒd| jkrŒ| jjd|jƒ | jjd|jƒ | jrž| j	| j
ƒ dS )zButton release event handlerr   r   r„   Tr½  r¼  Nr  r  r  r  r  )rÂ  r‘   r¾  r¿  rÀ  r4  Úinsertr‚   r�   r(  r¶  )r   r#   r   r   r   rE   X
  s    


zPolygonSelector._releasec             C   s&   | j |ƒs"| j|ƒ}| j|ƒ dS dS )z'Cursor move event handler and validatorTF)r$   rB  rH  )r   r#   r   r   r   rý   n
  s
    


zPolygonSelector.onmovec       	      C   s„  | j dkrV| j }|j|j | j|< | j|< |dkrR| jrR|j|j | jd< | jd< �n"d| jkrÄ| jrÄ|j| jj }|j| jj }x>tt	| jƒƒD ],}| j
| | | j|< | j| | | j|< q’W n´| jsÞd| jksÞd| jkrâdS | jjƒ j| jd | jd fƒ\}}tj||j ||j ƒ}t	| jƒdk�r^|| jk �r^| jd | jd  | jd	< | jd
< n|j|j | jd< | jd< | jƒ  dS )zCursor move event handlerr   r   r½  r¼  Nr„   r  r  r  r  r  r  )rÂ  r‚   r�   r¾  r¿  rÀ  r4  r0  Úranger‘   rÄ  rÅ  r!  Zget_transformr;   r}   Úhypotr£   rŸ   rÃ  Ú_draw_polygon)	r   r#   Úidxr¦  rœ   rŠ   r<  r>  Zv0_distr   r   r   rH  z
  s*    
$zPolygonSelector._onmovec             C   sJ   | j  rFd| jksd| jkrF| jdd… | jdd…  | _| _| jƒ  dS )zKey press event handlerr¼  r½  Nr   r  r  )rÀ  r4  r¾  r¿  rÉ  )r   r#   r   r   r   rJ   
  s    "zPolygonSelector._on_key_pressc             C   s˜   | j  rR|j| jjdƒks,|j| jjdƒkrR| jj|jƒ | jj|jƒ | j	ƒ  nB|j| jjdƒkr”| j
|ƒ}|jg|jg | _| _d| _ | jdƒ dS )zKey release event handlerr¼  r½  rþ   FTN)rÀ  r¶   r+  ræ   r¾  r.   r‚   r¿  r�   rÉ  rB  r˜   )r   r#   r   r   r   rL  ©
  s    

zPolygonSelector._on_key_releasec             C   s�   | j j| j| jƒ | jsNt| jƒdkrr| jd | jd krr| jd | jd krr| jj| jdd… | jdd… ƒ n| jj| j| jƒ | jƒ  dS )	z5Redraw the polygon based on the new vertex positions.r„   r   r   Nr  r  r  r  )r!  rs  r¾  r¿  rÀ  r‘   rÁ  r,  )r   r   r   r   rÉ  »
  s    $zPolygonSelector._draw_polygonc             C   s"   t t| jdd… | jdd… ƒƒS )z Get the polygon vertices.

        Returns
        -------
        list
            A list of the vertices of the polygon as ``(xdata, ydata)`` tuples.
        Nr   r  r  )r¹  r—   r¾  r¿  )r   r   r   r   r¶  Ê
  s    	zPolygonSelector.verts)FNNr»  )r   r   r   r   r   rF  rE   rý   rH  rJ  rL  rÉ  r'   r¶  r   r   r   r   rº  
  s   & 
"&	rº  c               @   s*   e Zd ZdZd
dd„Zdd„ Zdd	„ ZdS )ÚLassoa“  Selection curve of an arbitrary shape.

    The selected path can be used in conjunction with
    :func:`~matplotlib.path.Path.contains_point` to select data points
    from an image.

    Unlike :class:`LassoSelector`, this must be initialized with a starting
    point `xy`, and the `Lasso` events are destroyed upon release.

    Parameters
    ----------
    ax : `~matplotlib.axes.Axes`
        The parent axes for the widget.
    xy : (float, float)
        Coordinates of the start of the lasso.
    callback : callable
        Whenever the lasso is released, the `callback` function is called and
        passed the vertices of the selected path.
    NTc             C   s’   t j| |ƒ |o| jj| _| jr2| jj| jjƒ| _|\}}||fg| _	t
|g|gdddd�| _| jj| jƒ || _| jd| jƒ | jd| jƒ d S )Nr~  rŒ   rƒ   )r  rK   r`   r=   r>   )r(   r   r+   r  r  r
  r)   r  r  r¶  r   r!  rš   r/   r1   r¸  rý   )r   r)   r…   r/   r  r£   rŸ   r   r   r   r   ë
  s    zLasso.__init__c             C   sh   | j |ƒrd S | jd k	rV| jj|j|jfƒ t| jƒdkrF| j| jƒ | jjj	| j
ƒ d | _| jƒ  d S )Nrƒ   )r$   r¶  r.   r‚   r�   r‘   r/   r)   r•   rÑ   r!  r4   )r   r#   r   r   r   r¸  ú
  s    

zLasso.onreleasec             C   s¦   | j |ƒrd S | jd krd S |j| jkr,d S |jdkr:d S | jj|j|jfƒ | jj	t
t| jŽ ƒƒ | jr˜| jj| jƒ | jj| jƒ | jj| jjƒ n
| jjƒ  d S )Nr   )r$   r¶  rO   r)   r   r.   r‚   r�   r!  rs  r¹  r—   r  r+   r  r  r  r  r  r†   )r   r#   r   r   r   rý     s    


zLasso.onmove)NT)r   r   r   r   r   r¸  rý   r   r   r   r   rË  Ö
  s   
rË  )%r   rA  Znumbersr   Znumpyr}   r²   r   r   r•   r   Zpatchesr   r   r	   Z
transformsr
   Úobjectr   r   r(   r5   rY   r‰   r±   rÝ   rè   rù   r  r"  rM  rk  ry  r¯  rµ  rº  rË  r   r   r   r   Ú<module>
   sN   &*y     5  ^s e a;   0GW V