B
    Mô`cf  ã               @   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	m
Z
 ddlmZ ddlmZ dd„ ZG d	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dS )#a«  
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)Úcbook)ÚLine2D)Ú	Rectanglec             C   s(   t t| ¡ ƒd ƒ}|d k	r$|  |¡ d S )N)ÚnextÚiterZget_childrenÚupdate_from)ZtgtÚsrcZfirst_child© r
   ú=/tmp/pip-unpacked-wheel-0c1wfn4v/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__;   s    zHandlerBase.__init__c             C   s(   | j d kr|  ||¡ n|   ||¡ d S )N)r   Ú_default_update_prop)r   Úlegend_handleÚorig_handler
   r
   r   Ú_update_prop?   s    
zHandlerBase._update_propc             C   s   |  |¡ d S )N)r   )r   r   r   r
   r
   r   r   E   s    z HandlerBase._default_update_propc             C   s.   |   ||¡ | |¡ | d ¡ | d ¡ d S )N)r   Z_set_artist_propsÚset_clip_boxÚset_clip_path)r   r   r   Úlegendr
   r
   r   Úupdate_propH   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_areaP   s
    zHandlerBase.adjust_drawing_areac             C   sb   |   |||j|j|j|j|¡\}}}}|  |||||||| ¡ ¡}	x|	D ]}
| |
¡ qHW |	d S )a-  
        Return the artist that this HandlerBase generates for the given
        original artist/handle.

        Parameters
        ----------
        legend : `~matplotlib.legend.Legend`
            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 : int
            The fontsize in pixels. The artists being created should
            be scaled according to the given fontsize.
        handlebox : `matplotlib.offsetbox.OffsetBox`
            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_artistY   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.   r1   )r   r   r
   r
   r   Úget_numpoints›   s    
zHandlerNpoints.get_numpointsc             C   sh   |   |¡}|dkr@| j| }t | | | | | |¡}	|	}
n | | | g}	| d|  g}
|	|
fS )Né   g      à?)r3   r/   ÚnpÚlinspace)r   r   r   r   r   r   r    r1   ÚpadÚxdataÚxdata_markerr
   r
   r   Ú	get_xdata¡   s    

zHandlerNpoints.get_xdata)r-   N)r(   r)   r*   r+   r   r3   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 offset 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`.
        r1   N)r,   r   Ú	_yoffsets)r   r1   Úyoffsetsr2   r
   r
   r   r   µ   s    zHandlerNpointsYoffsets.__init__c             C   s*   | j d kr||j }n|t | j ¡ }|S )N)r<   Ú_scatteryoffsetsr5   Úasarray)r   r   r   r   r   r   r    Úydatar
   r
   r   Ú	get_ydataÇ   s    
z HandlerNpointsYoffsets.get_ydata)NN)r(   r)   r*   r+   r   rA   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`.
        )r0   r1   N)r,   r   )r   r0   r1   r2   r
   r
   r   r   Ô   s    
zHandlerLine2D.__init__c	             C   sÊ   |   ||||||¡\}	}
t |	|| d ¡}t|	|ƒ}|  |||¡ | d¡ | d¡ t|
|d t|
ƒ… ƒ}|  |||¡ | d¡ |j	dkr¨| 
¡ |j	 }| |¡ ||_| |¡ | |¡ ||gS )Né   ÚdefaultÚ ÚNoner4   )r:   r5   Ú	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'   r8   r9   r@   ÚleglineÚlegline_markerÚnewszr
   r
   r   r"   å   s"    








zHandlerLine2D.create_artists)rC   N)r(   r)   r*   r+   r   r"   r
   r
   r
   r   rB   Ð   s   
rB   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_funcr2   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    )rU   r   )	r   r   r   r   r   r   r   r    Úpr
   r
   r   Ú_create_patch  s    
zHandlerPatch._create_patchc	       
   	   C   s4   |   |||||||¡}	|  |	||¡ |	 |¡ |	gS )N)rX   r   rP   )
r   r   r   r   r   r   r   r    r'   rW   r
   r
   r   r"   )  s
    
zHandlerPatch.create_artists)N)r(   r)   r*   r+   r   rX   r"   r
   r
   r
   r   rT     s   
rT   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   r3   6  s    
z#HandlerLineCollection.get_numpointsc             C   sD   |  ¡ d }|jd }| ¡ d }| |¡ | |¡ | |¡ d S )Nr   )Úget_linewidthsZ_us_linestylesZ
get_colorsÚ	set_colorrL   Úset_linewidth)r   r   r   ZlwZdashesÚcolorr
   r
   r   r   <  s    


z*HandlerLineCollection._default_update_propc	             C   sT   |   ||||||¡\}	}
t |	|| d ¡}t|	|ƒ}|  |||¡ | |¡ |gS )NrD   )r:   r5   rH   r   r   rP   )r   r   r   r   r   r   r   r    r'   r8   r9   r@   rQ   r
   r
   r   r"   D  s    


z$HandlerLineCollection.create_artistsN)r(   r)   r*   r+   r3   r   r"   r
   r
   r
   r   rY   2  s   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 )ÚHandlerRegularPolyCollectionz'Handler for `.RegularPolyCollection`\s.Nc             K   s    t j| fd|i|—Ž || _d S )Nr=   )r;   r   Ú_sizes)r   r=   Úsizesr2   r
   r
   r   r   U  s    z%HandlerRegularPolyCollection.__init__c             C   s   | j d kr|jS | j S d S )N)r.   rZ   )r   r   r
   r
   r   r3   Z  s    
z*HandlerRegularPolyCollection.get_numpointsc             C   sœ   | j d kr’| ¡ }t|ƒs dg}t|ƒ|jd  }	t|ƒ|jd  }
|  |¡}|dk rrd|	|
  |	|
gd |… }q˜|	|
 }|t dd|¡ |
 }n| j }|S )Nr4   rD   é   g      à?r   )	r`   Ú	get_sizesrK   ÚmaxrM   Úminr3   r5   r6   )r   r   r   r   r   r   r   r    Zhandle_sizesZsize_maxZsize_minr1   ra   Úrngr
   r
   r   rc   `  s    

z&HandlerRegularPolyCollection.get_sizesc             C   s0   |   ||¡ | |j¡ | d ¡ | d ¡ d S )N)r   Ú
set_figureÚfigurer   r   )r   r   r   r   r
   r
   r   r   u  s    
z(HandlerRegularPolyCollection.update_propc             C   s"   t |ƒ| ¡ | ¡ |||d�}|S )N)Zrotationra   ÚoffsetsÚtransOffset)ÚtypeZget_numsidesZget_rotation)r   r   ra   ri   rj   rW   r
   r
   r   Úcreate_collection~  s    z.HandlerRegularPolyCollection.create_collectionc	          	   C   sx   |   ||||||¡\}	}
|  ||||||¡}|  |||||||¡}| j||tt|
|ƒƒ|d�}|  |||¡ ||_|gS )N)ri   rj   )r:   rA   rc   rl   ÚlistÚzipr   Z_transOffset)r   r   r   r   r   r   r   r    r'   r8   r9   r@   ra   rW   r
   r
   r   r"   ‡  s    



z+HandlerRegularPolyCollection.create_artists)NN)
r(   r)   r*   r+   r   r3   rc   r   rl   r"   r
   r
   r
   r   r_   R  s   
		r_   c               @   s   e Zd ZdZdd„ ZdS )ÚHandlerPathCollectionzDHandler for `.PathCollection`\s, which are used by `~.Axes.scatter`.c             C   s"   t |ƒ| ¡ d g|||d�}|S )Nr   )ra   ri   rj   )rk   Ú	get_paths)r   r   ra   ri   rj   rW   r
   r
   r   rl   ž  s
    z'HandlerPathCollection.create_collectionN)r(   r)   r*   r+   rl   r
   r
   r
   r   ro   œ  s   ro   c               @   s   e Zd ZdZdd„ ZdS )ÚHandlerCircleCollectionz"Handler for `.CircleCollection`\s.c             C   s   t |ƒ|||d�}|S )N)ri   rj   )rk   )r   r   ra   ri   rj   rW   r
   r
   r   rl   ©  s    z)HandlerCircleCollection.create_collectionN)r(   r)   r*   r+   rl   r
   r
   r
   r   rq   §  s   rq   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)r0   r1   )Ú
_xerr_sizeÚ
_yerr_sizerB   r   )r   Ú	xerr_sizeÚ	yerr_sizer0   r1   r2   r
   r
   r   r   ´  s    
zHandlerErrorbar.__init__c       	      C   s,   | j | }| jd kr|}n
| j| }||fS )N)ru   rv   )	r   r   r   r   r   r   r    rw   rx   r
   r
   r   Úget_err_size½  s
    


zHandlerErrorbar.get_err_sizec	                s˜  |\}	}
}|   ||||||¡\}}t ||| d ¡}t||ƒ}t |¡}t |d t|ƒ… ¡}|  ||||||¡\‰ ‰t||ƒ}|	d kr | d¡ | d¡ n\|  ||	|¡ | 	d¡ | 
d¡ |  ||	|¡ | d¡ |jdkrü| ¡ |j }| |¡ g }g }|j�r¸‡ fdd„t||ƒD ƒ}t |¡}|  ||d |¡ | |¡ |
�r¸t|ˆ  |ƒ}t|ˆ  |ƒ}|  ||
d |¡ |  ||
d |¡ | 
d	¡ | 
d	¡ | |¡ | |¡ |j�rl‡fd
d„t||ƒD ƒ}t |¡}|  ||d |¡ | |¡ |
�rlt||ˆ ƒ}t||ˆ ƒ}|  ||
d |¡ |  ||
d |¡ | 
d¡ | 
d¡ | |¡ | |¡ ||||f•}x|D ]}| |¡ �q€W |S )NrD   FrE   rG   r4   c                s(   g | ] \}}|ˆ  |f|ˆ  |ff‘qS r
   r
   )Ú.0ÚxÚy)rw   r
   r   ú
<listcomp>ò  s   z2HandlerErrorbar.create_artists.<locals>.<listcomp>r   ú|c                s(   g | ] \}}||ˆ  f||ˆ  ff‘qS r
   r
   )rz   r{   r|   )rx   r
   r   r}     s   Ú_)r:   r5   rH   r   r?   rK   ry   Zset_visibler   rI   rJ   rL   rM   rN   rO   Zhas_xerrrn   ÚmcollÚLineCollectionÚappendZhas_yerrrP   )r   r   r   r   r   r   r   r    r'   Z	plotlinesZcaplinesZbarlinecolsr8   r9   r@   rQ   Zydata_markerrR   rS   Zhandle_barlinecolsZhandle_caplinesZvertsZcollZcapline_leftZcapline_rightr#   Úartistr
   )rw   rx   r   r"   È  sr    


























zHandlerErrorbar.create_artists)rs   Nrt   N)r(   r)   r*   r+   r   ry   r"   r
   r
   r
   r   rr   ±  s
    
rr   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, default: 0.3
            Padding between points in legend entry.

        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`.
        )r0   r1   r=   N)r;   r   Ú_bottom)r   r0   r1   Úbottomr=   r2   r
   r
   r   r   !  s
    
zHandlerStem.__init__c             C   s2   | j d kr|d|j d  }n|t | j ¡ }|S )Ng      à?)r<   r>   r5   r?   )r   r   r   r   r   r   r    r@   r
   r
   r   rA   =  s    
zHandlerStem.get_ydatac	          	      sN  |\}	}
}t |
tjƒ}|  ||||||¡\}}|  ||||||¡}| jd krRd‰ n| j‰ t||d t|ƒ… ƒ}|  ||	|¡ ‡ fdd„t	||ƒD ƒ}|rÒt
j| | jd��" x|D ]}|  ||
|¡ q°W W d Q R X n&x$t	||
ƒD ]\}}|  |||¡ qÞW tt |¡t |¡gˆ ˆ gƒ}|  |||¡ |||f•}x|D ]}| |¡ �q6W |S )Ng        c                s"   g | ]\}}t ||gˆ |gƒ‘qS r
   )r   )rz   r{   r|   )r‡   r
   r   r}   \  s   z.HandlerStem.create_artists.<locals>.<listcomp>)r   )Ú
isinstancer€   r�   r:   rA   r†   r   rK   r   rn   r   Z_setattr_cmÚ_copy_collection_propsr5   re   rd   rP   )r   r   r   r   r   r   r   r    r'   Z
markerlineZ	stemlinesZbaselineZusing_linecollr8   r9   r@   Zleg_markerlineZleg_stemlinesÚlineZlmÚmZleg_baseliner#   rƒ   r
   )r‡   r   r"   E  s6    








zHandlerStem.create_artistsc             C   s(   |  | ¡ d ¡ | | ¡ d ¡ dS )zt
        Copy properties from the `.LineCollection` *orig_handle* to the
        `.Line2D` *legend_handle*.
        r   N)r\   Z	get_colorrL   Zget_linestyle)r   r   r   r
   r
   r   r‰   t  s    z"HandlerStem._copy_collection_props)r…   NNN)r(   r)   r*   r+   r   rA   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S )	ÚHandlerTupleal  
    Handler for Tuple.

    Additional kwargs are passed through to `HandlerBase`.

    Parameters
    ----------
    ndivide : int, default: 1
        The number of sections to divide the legend area into. If None,
        use the length of the input tuple.
    pad : float, default: :rc:`legend.borderpad`
        Padding in units of fraction of font size.
    r4   Nc             K   s   || _ || _tj| f|Ž d S )N)Ú_ndivideÚ_padr   r   )r   Úndivider7   Úkwargsr
   r
   r   r   Œ  s    zHandlerTuple.__init__c	             C   sÀ   |  ¡ }	| jd krt|ƒ}
n| j}
| jd kr8|j| }n
| j| }|
dkr^|||
d   |
 }t||| t |
¡  ƒ}g }x>|D ]6}| |	|¡}| 	||t
|ƒ|||||¡}| |¡ q‚W |S )Nr4   )Zget_legend_handler_mapr�   rK   rŽ   Z	borderpadr   r5   ZarangeZget_legend_handlerr"   r   Úextend)r   r   r   r   r   r   r   r    r'   Zhandler_mapr�   r7   Z	xds_cycleÚa_listZhandle1ÚhandlerZ_a_listr
   r
   r   r"   ‘  s$    




zHandlerTuple.create_artists)r4   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| ¡ ƒ}| ||ƒ¡ t |d| ¡ ƒ}| ||ƒ¡ | | ¡ ¡ | | ¡ ¡ | 	|| 
¡ ƒ¡ | || ¡ ƒ¡ | || ¡ ƒ¡ | | ¡ ¡ | | ¡ ¡ d S )Nc             S   s.   | d krd S t  | ¡} t| ƒr&| d S dS d S )Nr   Únone)ÚmcolorsZto_rgba_arrayrK   )Úcolorsr
   r
   r   Úfirst_color·  s    
z7HandlerPolyCollection._update_prop.<locals>.first_colorc             S   s   t | ƒr| d S d S d S )Nr   )rK   )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_hatchr]   r[   rL   Zget_linestylesrP   Zget_transformsrg   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�}	|  |	||¡ |	 |¡ |	gS )N)rV   r   r   )r   r   rP   )
r   r   r   r   r   r   r   r    r'   rW   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numpyr5   Z
matplotlibr   Zmatplotlib.linesr   Zmatplotlib.patchesr   Zmatplotlib.collectionsÚcollectionsr€   Zmatplotlib.colorsr—   r–   r   r   r,   r;   rB   rT   rY   r_   ro   rq   rr   r„   rŒ   r”   r
   r
   r
   r   Ú<module>   s*   Y, 4. J
l`4