3
$ßL]�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_fromZget_children)ZtgtÚsrc© r   úA/tmp/pip-install-edjban26/matplotlib/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ypadZ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   Z_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_artistsZget_transformZ
add_artist)r   r   r   r   Z	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)r+   r.   )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      à?)r0   r,   ÚnpÚlinspace)r   r   r   r   r   r   r   r.   ÚpadÚxdataÚxdata_markerr   r   r   Ú	get_xdataŸ   s    

zHandlerNpoints.get_xdata)r*   N)r%   r&   r'   r(   r   r0   r8   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`.
        r.   N)r)   r   Ú	_yoffsets)r   r.   Úyoffsetsr/   r   r   r   r   ³   s    zHandlerNpointsYoffsets.__init__c             C   s*   | j d kr||j }n|tj| j ƒ }|S )N)r:   Ú_scatteryoffsetsr3   Úasarray)r   r   r   r   r   r   r   Úydatar   r   r   Ú	get_ydataÅ   s    
z HandlerNpointsYoffsets.get_ydata)NN)r%   r&   r'   r(   r   r?   r   r   r   r   r9   ®   s   
r9   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`.
        )r-   r.   N)r)   r   )r   r-   r.   r/   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 )Nr2   ÚdefaultÚ ÚNoner1   )r8   r3   Z	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$   r6   r7   r>   ÚleglineÚlegline_markerÚnewszr   r   r   r   ã   s"    








zHandlerLine2D.create_artists)rA   N)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 )
Ú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_funcr/   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   )rQ   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)rT   r   rL   )
r   r   r   r   r   r   r   r   r$   rS   r   r   r   r   '  s
    
zHandlerPatch.create_artists)N)r%   r&   r'   r(   r   rT   r   r   r   r   r   rP     s   
rP   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)r+   Úscatterpoints)r   r   r   r   r   r0   4  s    
z#HandlerLineCollection.get_numpointsc             C   sD   |j ƒ d }|jd }|jƒ d }|j|ƒ |j|ƒ |j|ƒ d S )Nr   )Úget_linewidthsZ_us_linestylesZ
get_colorsÚ	set_colorrH   Úset_linewidth)r   r   r   ZlwZ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       @)r8   r3   ÚonesÚshapeÚfloatr   r   rL   )r   r   r   r   r   r   r   r   r$   r6   r7   r>   rM   r   r   r   r   B  s    


z$HandlerLineCollection.create_artistsN)r%   r&   r'   r(   r0   r   r   r   r   r   r   rU   0  s   rU   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 )Nr;   )r9   r   Ú_sizes)r   r;   Úsizesr/   r   r   r   r   T  s    z%HandlerRegularPolyCollection.__init__c             C   s   | j d kr|jS | j S d S )N)r+   rV   )r   r   r   r   r   r0   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 )Nr1   r2   é   g      à?r   )	r_   Ú	get_sizesrG   ÚmaxrI   Úminr0   r3   r4   )r   r   r   r   r   r   r   r   Zhandle_sizesZsize_maxZsize_minr.   r`   Úrngr   r   r   rb   _  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)Zrotationr`   ÚoffsetsÚtransOffset)ÚtypeZget_numsidesZget_rotation)r   r   r`   rh   ri   rS   r   r   r   Úcreate_collection}  s    z.HandlerRegularPolyCollection.create_collectionc	             C   sx   | j ||||||ƒ\}	}
| j||||||ƒ}| j|||||||ƒ}| j||tt|
|ƒƒ|d�}| j|||ƒ ||_|gS )N)rh   ri   )r8   r?   rb   rk   ÚlistÚzipr   Z_transOffset)r   r   r   r   r   r   r   r   r$   r6   r7   r>   r`   rS   r   r   r   r   †  s    



z+HandlerRegularPolyCollection.create_artists)NN)
r%   r&   r'   r(   r   r0   rb   r   rk   r   r   r   r   r   r^   P  s   
		r^   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   )r`   rh   ri   )rj   Ú	get_paths)r   r   r`   rh   ri   rS   r   r   r   rk   Ÿ  s
    z'HandlerPathCollection.create_collectionN)r%   r&   r'   r(   rk   r   r   r   r   rn   ›  s   rn   c               @   s   e Zd ZdZdd„ ZdS )ÚHandlerCircleCollectionz+
    Handler for `.CircleCollections`.
    c             C   s   t |ƒ|||d�}|S )N)rh   ri   )rj   )r   r   r`   rh   ri   rS   r   r   r   rk   ¬  s    z)HandlerCircleCollection.create_collectionN)r%   r&   r'   r(   rk   r   r   r   r   rp   ¨  s   rp   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)r-   r.   )Ú
_xerr_sizeÚ
_yerr_sizer@   r   )r   Ú	xerr_sizeÚ	yerr_sizer-   r.   r/   r   r   r   r   ¸  s    
zHandlerErrorbar.__init__c       	      C   s,   | j | }| jd kr|}n
| j| }||fS )N)rt   ru   )	r   r   r   r   r   r   r   rv   rw   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       @FrB   rD   r1   c                s(   g | ] \}}|ˆ  |f|ˆ  |ff‘qS r   r   )Ú.0ÚxÚy)rv   r   r   ú
<listcomp>ö  s   z2HandlerErrorbar.create_artists.<locals>.<listcomp>r   ú|c                s(   g | ] \}}||ˆ  f||ˆ  ff‘qS r   r   )ry   rz   r{   )rw   r   r   r|     s   Ú_)r8   r3   r[   r\   r]   r   r=   rG   rx   Zset_visibler   rE   rF   rH   rI   rJ   rK   Zhas_xerrrm   ÚmcollÚLineCollectionÚappendZhas_yerrÚextendrL   )r   r   r   r   r   r   r   r   r$   Z	plotlinesZcaplinesZbarlinecolsr6   r7   r>   rM   Zydata_markerrN   rO   Zhandle_barlinecolsZhandle_caplinesZvertsZcollZcapline_leftZcapline_rightr    Úartistr   )rv   rw   r   r   Ì  sz    





























zHandlerErrorbar.create_artists)rr   Nrs   N)r%   r&   r'   r(   r   rx   r   r   r   r   r   rq   ´  s
    
rq   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`.
        )r-   r.   r;   N)r9   r   Ú_bottom)r   r-   r.   Úbottomr;   r/   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:   r<   r3   r=   )r   r   r   r   r   r   r   r>   r   r   r   r?   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   )ry   rz   r{   )r‡   r   r   r|   d  s   z.HandlerStem.create_artists.<locals>.<listcomp>)Ú
isinstancer   r€   r8   r?   r†   r   rG   r   rm   r   Ú_copy_collection_propsr3   rd   rc   r�   rL   )r   r   r   r   r   r   r   r   r$   Z
markerlineZ	stemlinesZbaselineZusing_linecollr6   r7   r>   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)rX   Z	get_colorrH   Zget_linestyle)r   r   r   r   r   r   r‰   „  s    z"HandlerStem._copy_collection_props)r…   NNN)r%   r&   r'   r(   r   r?   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.
    r1   Nc             K   s   || _ || _tj| f|Ž d S )N)Ú_ndivideÚ_padr
   r   )r   Úndivider5   Ú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 )Nr1   )Zget_legend_handler_mapr�   rG   rŽ   Z	borderpadr   r3   ZarangeZget_legend_handlerr   Únextr‚   )r   r   r   r   r   r   r   r   r$   Zhandler_mapr�   r5   Z	xds_cycleÚa_listZhandle1ÚhandlerZ_a_listr   r   r   r   ¤  s$    




zHandlerTuple.create_artists)r1   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)ÚmcolorsZto_rgba_arrayrG   )Úcolorsr   r   r   Úfirst_colorÊ  s    
z7HandlerPolyCollection._update_prop.<locals>.first_colorc             S   s   t | ƒr| d S d S d S )Nr   )rG   )Z
prop_arrayr   r   r   Ú	get_firstÓ  s    z5HandlerPolyCollection._update_prop.<locals>.get_firstZ_original_edgecolorZ_original_facecolor)ÚgetattrZget_edgecolorZset_edgecolorZget_facecolorZset_facecolorZset_fillZget_fillZ	set_hatchZ	get_hatchrY   rW   rH   Zget_linestylesrL   Zget_transformsrf   Z
get_figureZ	set_alphaZ	get_alpha)r   r   r   r˜   r™   Z	edgecolorZ	facecolorr   r   r   r   É  s    	

z"HandlerPolyCollection._update_propc	       
      C   s4   t | | f||d�}	| j|	||ƒ |	j|ƒ |	gS )N)rR   r   r   )r   r   rL   )
r   r   r   r   r   r   r   r   r$   rS   r   r   r   r   æ  s
    

z$HandlerPolyCollection.create_artistsN)r%   r&   r'   r(   r   r   r   r   r   r   r”   Ä  s   r”   )r(   Ú	itertoolsr   Znumpyr3   Zmatplotlib.linesr   Zmatplotlib.patchesr   Zmatplotlib.collectionsÚcollectionsr   Zmatplotlib.colorsr—   r–   r	   Úobjectr
   r)   r9   r@   rP   rU   r^   rn   rp   rq   r„   rŒ   r”   r   r   r   r   Ú<module>   s(   

Y, 4. Kqh7