3
6Ad]�g  ã               @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddl	j
ZddljZdd„ 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À  
This module defines default legend handlers.

It is strongly encouraged to have read the :doc:`legend guide
</tutorials/intermediate/legend_guide>` before this documentation.

Legend handlers are expected to be a callable object with a following
signature. ::

    legend_handler(legend, orig_handle, fontsize, handlebox)

Where *legend* is the legend itself, *orig_handle* is the original
plot, *fontsize* is the fontsize in pixels, and *handlebox* is a
OffsetBox instance. Within the call, you should create relevant
artists (using relevant properties from the *legend* and/or
*orig_handle*) and add them into the handlebox. The artists needs to
be scaled according to the fontsize (note that the size is in pixel,
i.e., this is dpi-scaled value).

This module includes definition of several legend handler classes
derived from the base class (HandlerBase) with the following method::

    def legend_artist(self, legend, orig_handle, fontsize, handlebox)

é    )ÚcycleN)ÚLine2D)Ú	Rectanglec             C   s   | j |jƒ d ƒ d S )Nr   )Úupdate_fromÚget_children)ZtgtÚsrc© r   ú„/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/matplotlib-3.1.1-py3.6-linux-x86_64.egg/matplotlib/legend_handler.pyÚupdate_from_first_child%   s    r
   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 )ÚHandlerBasea   
    A Base class for default legend handlers.

    The derived classes are meant to override *create_artists* method, which
    has a following signature.::

      def create_artists(self, legend, orig_handle,
                         xdescent, ydescent, width, height, fontsize,
                         trans):

    The overridden method needs to create artists of the given
    transform that fits in the given dimension (xdescent, ydescent,
    width, height) that are scaled by fontsize if necessary.

    ç        Nc             C   s   || | _ | _|| _d S )N)Ú_xpadÚ_ypadÚ_update_prop_func)ÚselfZxpadZypadÚupdate_funcr   r   r	   Ú__init__9   s    zHandlerBase.__init__c             C   s(   | j d kr| j||ƒ n| j ||ƒ d S )N)r   Ú_default_update_prop)r   Úlegend_handleÚorig_handler   r   r	   Ú_update_prop=   s    
zHandlerBase._update_propc             C   s   |j |ƒ d S )N)r   )r   r   r   r   r   r	   r   C   s    z HandlerBase._default_update_propc             C   s.   | j ||ƒ |j|ƒ |jd ƒ |jd ƒ d S )N)r   Ú_set_artist_propsÚset_clip_boxÚset_clip_path)r   r   r   Úlegendr   r   r	   Úupdate_propF   s    

zHandlerBase.update_propc             C   sD   || j |  }|| j|  }|| j |  }|| j|  }||||fS )N)r   r   )r   r   r   ÚxdescentÚydescentÚwidthÚheightÚfontsizer   r   r	   Úadjust_drawing_areaN   s
    zHandlerBase.adjust_drawing_areac          	   C   sb   | j |||j|j|j|j|ƒ\}}}}| j||||||||jƒ ƒ}	x|	D ]}
|j|
ƒ qHW |	d S )aU  
        Return the artist that this HandlerBase generates for the given
        original artist/handle.

        Parameters
        ----------
        legend : :class:`matplotlib.legend.Legend` instance
            The legend for which these legend artists are being created.
        orig_handle : :class:`matplotlib.artist.Artist` or similar
            The object for which these legend artists are being created.
        fontsize : float or int
            The fontsize in pixels. The artists being created should
            be scaled according to the given fontsize.
        handlebox : :class:`matplotlib.offsetbox.OffsetBox` instance
            The box which has been created to hold this legend entry's
            artists. Artists created in the `legend_artist` method must
            be added to this handlebox inside this method.

        r   )r!   r   r   r   r   Úcreate_artistsÚget_transformÚ
add_artist)r   r   r   r    Ú	handleboxr   r   r   r   ÚartistsÚar   r   r	   Úlegend_artistW   s    
zHandlerBase.legend_artistc	       	      C   s   t dƒ‚d S )NzDerived must override)ÚNotImplementedError)	r   r   r   r   r   r   r   r    Útransr   r   r	   r"   |   s    zHandlerBase.create_artists)r   r   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   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 )ÚHandlerNpointszM
    A legend handler that shows *numpoints* points in the legend entry.
    ç333333Ó?Nc             K   s   t j| f|Ž || _|| _dS )a*  
        Parameters
        ----------
        marker_pad : float
            Padding between points in legend entry.

        numpoints : int
            Number of points to show in legend entry.

        Notes
        -----
        Any other keyword arguments are given to `HandlerBase`.
        N)r   r   Ú
_numpointsÚ_marker_pad)r   Ú
marker_padÚ	numpointsÚkwr   r   r	   r   †   s    zHandlerNpoints.__init__c             C   s   | j d kr|jS | j S d S )N)r1   r4   )r   r   r   r   r	   Úget_numpoints™   s    
zHandlerNpoints.get_numpointsc             C   sn   | j |ƒ}|dkr@| j| }tj| | | | | |ƒ}	|	}
n&tj| | | dƒ}	| d|  g}
|	|
fS )Né   é   g      à?)r6   r2   ÚnpÚlinspace)r   r   r   r   r   r   r    r4   ÚpadÚxdataÚxdata_markerr   r   r	   Ú	get_xdataŸ   s    

zHandlerNpoints.get_xdata)r0   N)r+   r,   r-   r.   r   r6   r>   r   r   r   r	   r/   ‚   s   
r/   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )ÚHandlerNpointsYoffsetsz‚
    A legend handler that shows *numpoints* in the legend, and allows them to
    be individually offest in the y-direction.
    Nc             K   s    t j| fd|i|—Ž || _dS )a^  
        Parameters
        ----------
        numpoints : int
            Number of points to show in legend entry.

        yoffsets : array of floats
            Length *numpoints* list of y offsets for each point in
            legend entry.

        Notes
        -----
        Any other keyword arguments are given to `HandlerNpoints`.
        r4   N)r/   r   Ú	_yoffsets)r   r4   Úyoffsetsr5   r   r   r	   r   ³   s    zHandlerNpointsYoffsets.__init__c             C   s*   | j d kr||j }n|tj| j ƒ }|S )N)r@   Ú_scatteryoffsetsr9   Úasarray)r   r   r   r   r   r   r    Úydatar   r   r	   Ú	get_ydataÅ   s    
z HandlerNpointsYoffsets.get_ydata)NN)r+   r,   r-   r.   r   rE   r   r   r   r	   r?   ®   s   
r?   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚHandlerLine2Dz*
    Handler for `.Line2D` instances.
    ç333333Ó?Nc             K   s   t j| f||dœ|—Ž dS )a-  
        Parameters
        ----------
        marker_pad : float
            Padding between points in legend entry.

        numpoints : int
            Number of points to show in legend entry.

        Notes
        -----
        Any other keyword arguments are given to `HandlerNpoints`.
        )r3   r4   N)r/   r   )r   r3   r4   r5   r   r   r	   r   Ò   s    
zHandlerLine2D.__init__c	             C   sÊ   | j ||||||ƒ\}	}
tj|	|| d ƒ}t|	|ƒ}| j|||ƒ |jdƒ |jdƒ t|
|d t|
ƒ… ƒ}| j|||ƒ |jdƒ |j	dkr¨|j
ƒ |j	 }|j|ƒ ||_|j|ƒ |j|ƒ ||gS )Nr8   ÚdefaultÚ ÚNoner7   )r>   r9   Ú	full_liker   r   Úset_drawstyleÚ
set_markerÚlenÚset_linestyleÚmarkerscaleÚget_markersizeÚset_markersizeZ
_legmarkerÚset_transform)r   r   r   r   r   r   r   r    r*   r<   r=   rD   ÚleglineÚlegline_markerÚnewszr   r   r	   r"   ã   s"    








zHandlerLine2D.create_artists)rG   N)r+   r,   r-   r.   r   r"   r   r   r   r	   rF   Î   s   
rF   c               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚHandlerPatchz)
    Handler for `.Patch` instances.
    Nc             K   s   t j| f|Ž || _dS )a•  
        Parameters
        ----------
        patch_func : callable, optional
            The function that creates the legend key artist.
            *patch_func* should have the signature::

                def patch_func(legend=legend, orig_handle=orig_handle,
                               xdescent=xdescent, ydescent=ydescent,
                               width=width, height=height, fontsize=fontsize)

            Subsequently the created artist will have its ``update_prop``
            method called and the appropriate transform will be applied.

        Notes
        -----
        Any other keyword arguments are given to `HandlerBase`.
        N)r   r   Ú_patch_func)r   Z
patch_funcr5   r   r   r	   r     s    zHandlerPatch.__init__c       	   	   C   s>   | j d kr"t| | f||d�}n| j |||||||d�}|S )N)Úxyr   r   )r   r   r   r   r   r   r    )rX   r   )	r   r   r   r   r   r   r   r    Úpr   r   r	   Ú_create_patch  s    
zHandlerPatch._create_patchc	       
      C   s4   | j |||||||ƒ}	| j|	||ƒ |	j|ƒ |	gS )N)r[   r   rS   )
r   r   r   r   r   r   r   r    r*   rZ   r   r   r	   r"   '  s
    
zHandlerPatch.create_artists)N)r+   r,   r-   r.   r   r[   r"   r   r   r   r	   rW     s   
rW   c               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚHandlerLineCollectionz2
    Handler for `.LineCollection` instances.
    c             C   s   | j d kr|jS | j S d S )N)r1   Úscatterpoints)r   r   r   r   r	   r6   4  s    
z#HandlerLineCollection.get_numpointsc             C   sD   |j ƒ d }|jd }|jƒ d }|j|ƒ |j|ƒ |j|ƒ d S )Nr   )Úget_linewidthsÚ_us_linestylesÚ
get_colorsÚ	set_colorrO   Úset_linewidth)r   r   r   ÚlwÚdashesÚcolorr   r   r	   r   :  s    


z*HandlerLineCollection._default_update_propc	             C   sZ   | j ||||||ƒ\}	}
|| d tj|	jtƒ }t|	|ƒ}| j|||ƒ |j|ƒ |gS )Ng       @)r>   r9   ÚonesÚshapeÚfloatr   r   rS   )r   r   r   r   r   r   r   r    r*   r<   r=   rD   rT   r   r   r	   r"   B  s    


z$HandlerLineCollection.create_artistsN)r+   r,   r-   r.   r6   r   r"   r   r   r   r	   r\   0  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 )ÚHandlerRegularPolyCollectionz0
    Handler for `.RegularPolyCollections`.
    Nc             K   s    t j| fd|i|—Ž || _d S )NrA   )r?   r   Ú_sizes)r   rA   Úsizesr5   r   r   r	   r   T  s    z%HandlerRegularPolyCollection.__init__c             C   s   | j d kr|jS | j S d S )N)r1   r]   )r   r   r   r   r	   r6   Y  s    
z*HandlerRegularPolyCollection.get_numpointsc             C   sœ   | j d kr’|jƒ }t|ƒs dg}t|ƒ|jd  }	t|ƒ|jd  }
| j|ƒ}|dk rrd|	|
  |	|
gd |… }q˜|	|
 }|tjdd|ƒ |
 }n| j }|S )Nr7   r8   é   g      à?r   )	rj   Ú	get_sizesrN   ÚmaxrP   Úminr6   r9   r:   )r   r   r   r   r   r   r   r    Zhandle_sizesZsize_maxZsize_minr4   rk   Úrngr   r   r	   rm   _  s    

z&HandlerRegularPolyCollection.get_sizesc             C   s0   | j ||ƒ |j|jƒ |jd ƒ |jd ƒ d S )N)r   Ú
set_figureÚfigurer   r   )r   r   r   r   r   r   r	   r   t  s    
z(HandlerRegularPolyCollection.update_propc             C   s"   t |ƒ|jƒ |jƒ |||d�}|S )N)Úrotationrk   ÚoffsetsÚtransOffset)ÚtypeÚget_numsidesÚget_rotation)r   r   rk   rt   ru   rZ   r   r   r	   Úcreate_collection}  s    z.HandlerRegularPolyCollection.create_collectionc	             C   sx   | j ||||||ƒ\}	}
| j||||||ƒ}| j|||||||ƒ}| j||tt|
|ƒƒ|d�}| j|||ƒ ||_|gS )N)rt   ru   )r>   rE   rm   ry   ÚlistÚzipr   Ú_transOffset)r   r   r   r   r   r   r   r    r*   r<   r=   rD   rk   rZ   r   r   r	   r"   †  s    



z+HandlerRegularPolyCollection.create_artists)NN)
r+   r,   r-   r.   r   r6   rm   r   ry   r"   r   r   r   r	   ri   P  s   
		ri   c               @   s   e Zd ZdZdd„ ZdS )ÚHandlerPathCollectionzM
    Handler for `.PathCollections`, which are used by `~.Axes.scatter`.
    c             C   s"   t |ƒ|jƒ d g|||d�}|S )Nr   )rk   rt   ru   )rv   Ú	get_paths)r   r   rk   rt   ru   rZ   r   r   r	   ry   Ÿ  s
    z'HandlerPathCollection.create_collectionN)r+   r,   r-   r.   ry   r   r   r   r	   r}   ›  s   r}   c               @   s   e Zd ZdZdd„ ZdS )ÚHandlerCircleCollectionz+
    Handler for `.CircleCollections`.
    c             C   s   t |ƒ|||d�}|S )N)rt   ru   )rv   )r   r   rk   rt   ru   rZ   r   r   r	   ry   ¬  s    z)HandlerCircleCollection.create_collectionN)r+   r,   r-   r.   ry   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 )ÚHandlerErrorbarz 
    Handler for Errorbars.
    ç      à?Nç333333Ó?c             K   s(   || _ || _tj| f||dœ|—Ž d S )N)r3   r4   )Ú
_xerr_sizeÚ
_yerr_sizerF   r   )r   Ú	xerr_sizeÚ	yerr_sizer3   r4   r5   r   r   r	   r   ¸  s    
zHandlerErrorbar.__init__c       	      C   s,   | j | }| jd kr|}n
| j| }||fS )N)rƒ   r„   )	r   r   r   r   r   r   r    r…   r†   r   r   r	   Úget_err_sizeÁ  s
    


zHandlerErrorbar.get_err_sizec	                s¾  |\}	}
}| j ||||||ƒ\}}|| d tj|jtƒ }t||ƒ}tj|ƒ}tj|d t|ƒ… ƒ}| j||||||ƒ\‰ ‰t||ƒ}|	d kr¦|j	dƒ |j	dƒ n^| j
||	|ƒ |jdƒ |jdƒ | j
||	|ƒ |jdƒ |jdk�r|jƒ |j }|j|ƒ g }g }|j�rÀ‡ fdd„t||ƒD ƒ}tj|ƒ}| j
||d |ƒ |j|ƒ |
�rÀt|ˆ  |ƒ}t|ˆ  |ƒ}| j
||
d |ƒ | j
||
d |ƒ |jd	ƒ |jd	ƒ |j|ƒ |j|ƒ |j�rt‡fd
d„t||ƒD ƒ}tj|ƒ}| j
||d |ƒ |j|ƒ |
�rtt||ˆ ƒ}t||ˆ ƒ}| j
||
d |ƒ | j
||
d |ƒ |jdƒ |jdƒ |j|ƒ |j|ƒ g }|j|ƒ |j|ƒ |j|ƒ |j|ƒ x|D ]}|j|ƒ �q¦W |S )Ng       @FrH   rJ   r7   c                s(   g | ] \}}|ˆ  |f|ˆ  |ff‘qS r   r   )Ú.0ÚxÚy)r…   r   r	   ú
<listcomp>ö  s   z2HandlerErrorbar.create_artists.<locals>.<listcomp>r   ú|c                s(   g | ] \}}||ˆ  f||ˆ  ff‘qS r   r   )rˆ   r‰   rŠ   )r†   r   r	   r‹     s   Ú_)r>   r9   rf   rg   rh   r   rC   rN   r‡   Úset_visibler   rL   rM   rO   rP   rQ   rR   Úhas_xerrr{   ÚmcollÚLineCollectionÚappendÚhas_yerrÚextendrS   )r   r   r   r   r   r   r   r    r*   Z	plotlinesÚcaplinesZbarlinecolsr<   r=   rD   rT   Zydata_markerrU   rV   Zhandle_barlinecolsZhandle_caplinesÚvertsÚcollZcapline_leftZcapline_rightr&   Úartistr   )r…   r†   r	   r"   Ì  sz    





























zHandlerErrorbar.create_artists)r�   Nr‚   N)r+   r,   r-   r.   r   r‡   r"   r   r   r   r	   r€   ´  s
    
r€   c               @   s2   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ ZdS )ÚHandlerStemz6
    Handler for plots produced by `~.Axes.stem`.
    ç333333Ó?Nc             K   s$   t j| f|||dœ|—Ž || _dS )aü  
        Parameters
        ----------
        marker_pad : float
            Padding between points in legend entry. Default is 0.3.

        numpoints : int, optional
            Number of points to show in legend entry.

        bottom : float, optional

        yoffsets : array of floats, optional
            Length *numpoints* list of y offsets for each point in
            legend entry.

        Notes
        -----
        Any other keyword arguments are given to `HandlerNpointsYoffsets`.
        )r3   r4   rA   N)r?   r   Ú_bottom)r   r3   r4   ÚbottomrA   r5   r   r   r	   r   )  s
    
zHandlerStem.__init__c             C   s2   | j d kr|d|j d  }n|tj| j ƒ }|S )Ng      à?)r@   rB   r9   rC   )r   r   r   r   r   r   r    rD   r   r   r	   rE   E  s    
zHandlerStem.get_ydatac	                sV  |\}	}
}t |
tjƒ}| j||||||ƒ\}}| j||||||ƒ}| jd krRd‰ n| j‰ t||d t|ƒ… ƒ}| j||	|ƒ ‡ fdd„t	||ƒD ƒ}|rÄ| j
}| j| _
xB|D ]}| j||
|ƒ q¬W n&x$t	||
ƒD ]\}}| j|||ƒ qÐW |rô|| _
ttj|ƒtj|ƒgˆ ˆ gƒ}| j|||ƒ |}|j|ƒ |j|ƒ x|D ]}|j|ƒ �q>W |S )Ng        c                s"   g | ]\}}t ||gˆ |gƒ‘qS r   )r   )rˆ   r‰   rŠ   )rœ   r   r	   r‹   d  s   z.HandlerStem.create_artists.<locals>.<listcomp>)Ú
isinstancer�   r‘   r>   rE   r›   r   rN   r   r{   r   Ú_copy_collection_propsr9   ro   rn   r’   rS   )r   r   r   r   r   r   r   r    r*   Ú
markerlineÚ	stemlinesÚbaselineZusing_linecollr<   r=   rD   Zleg_markerlineZleg_stemlinesZorig_update_funcÚlineZlmÚmZleg_baseliner&   r˜   r   )rœ   r	   r"   M  s>    










zHandlerStem.create_artistsc             C   s(   |j |jƒ d ƒ |j|jƒ d ƒ dS )zt
        Method to copy properties from a LineCollection (orig_handle) to a
        Line2D (legend_handle).
        r   N)ra   Ú	get_colorrO   Úget_linestyle)r   r   r   r   r   r	   rž   „  s    z"HandlerStem._copy_collection_props)rš   NNN)r+   r,   r-   r.   r   rE   r"   rž   r   r   r   r	   r™   %  s    
7r™   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚHandlerTuplea¯  
    Handler for Tuple.

    Additional kwargs are passed through to `HandlerBase`.

    Parameters
    ----------
    ndivide : int, optional
        The number of sections to divide the legend area into. If None,
        use the length of the input tuple. Default is 1.


    pad : float, optional
        If None, fall back to ``legend.borderpad`` as the default.
        In units of fraction of font size. Default is None.
    r7   Nc             K   s   || _ || _tj| f|Ž d S )N)Ú_ndivideÚ_padr   r   )r   Úndivider;   Úkwargsr   r   r	   r   ž  s    zHandlerTuple.__init__c	          
   C   sÀ   |j ƒ }	| jd krt|ƒ}
n| j}
| jd kr8|j| }n
| j| }|
dkr^|||
d   |
 }t||| tj|
ƒ  ƒ}g }x>|D ]6}|j|	|ƒ}|j	||t
|ƒ|||||ƒ}|j|ƒ q‚W |S )Nr7   )Úget_legend_handler_mapr§   rN   r¨   Ú	borderpadr   r9   ÚarangeÚget_legend_handlerr"   Únextr”   )r   r   r   r   r   r   r   r    r*   Úhandler_mapr©   r;   Z	xds_cycleÚa_listZhandle1ÚhandlerZ_a_listr   r   r	   r"   ¤  s$    




zHandlerTuple.create_artists)r7   N)r+   r,   r-   r.   r   r"   r   r   r   r	   r¦   �  s   
r¦   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚHandlerPolyCollectionza
    Handler for `.PolyCollection` used in `~.Axes.fill_between` and
    `~.Axes.stackplot`.
    c             C   s¾   dd„ }dd„ }t |d|jƒ ƒ}|j||ƒƒ t |d|jƒ ƒ}|j||ƒƒ |j|jƒ ƒ |j|jƒ ƒ |j	||j
ƒ ƒƒ |j||jƒ ƒƒ |j||jƒ ƒƒ |j|jƒ ƒ |j|jƒ ƒ d S )Nc             S   s.   | d krd S t j| ƒ} t| ƒr&| d S dS d S )Nr   Únone)ÚmcolorsÚto_rgba_arrayrN   )Úcolorsr   r   r	   Úfirst_colorÊ  s    
z7HandlerPolyCollection._update_prop.<locals>.first_colorc             S   s   t | ƒr| d S d S d S )Nr   )rN   )Z
prop_arrayr   r   r	   Ú	get_firstÓ  s    z5HandlerPolyCollection._update_prop.<locals>.get_firstÚ_original_edgecolorÚ_original_facecolor)ÚgetattrÚget_edgecolorÚset_edgecolorÚget_facecolorÚset_facecolorÚset_fillÚget_fillÚ	set_hatchÚ	get_hatchrb   r^   rO   Úget_linestylesrS   Úget_transformsrq   Ú
get_figureÚ	set_alphaÚ	get_alpha)r   r   r   r¸   r¹   Ú	edgecolorÚ	facecolorr   r   r	   r   É  s    	

z"HandlerPolyCollection._update_propc	       
      C   s4   t | | f||d�}	| j|	||ƒ |	j|ƒ |	gS )N)rY   r   r   )r   r   rS   )
r   r   r   r   r   r   r   r    r*   rZ   r   r   r	   r"   æ  s
    

z$HandlerPolyCollection.create_artistsN)r+   r,   r-   r.   r   r"   r   r   r   r	   r³   Ä  s   r³   )r.   Ú	itertoolsr   Únumpyr9   Úmatplotlib.linesr   Úmatplotlib.patchesr   Úmatplotlib.collectionsÚcollectionsr�   Úmatplotlib.colorsr·   rµ   r
   Úobjectr   r/   r?   rF   rW   r\   ri   r}   r   r€   r™   r¦   r³   r   r   r   r	   Ú<module>   s(   

Y, 4. Kqh7