B
    ]”t\´c  ã               @   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„ 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   |   | ¡ d ¡ d S )Nr   )Úupdate_fromZget_children)ZtgtÚsrc© r   ú8lib/python3.7/site-packages/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|  ||¡ 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   C   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_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|¡\}}}}|  |||||||| ¡ ¡}	x|	D ]}
| |
¡ 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   |   |¡}|dkr@| j| }t | | | | | |¡}	|	}
n&t | | | 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 ¡ }|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Ð   |   ||||||¡\}	}
|| d t |	jt¡ }t|	|ƒ}|  |||¡ | d¡ | d¡ t|
|d t	|
ƒ… ƒ}|  |||¡ | 
d¡ |jdkr®| ¡ |j }| |¡ ||_| |¡ | |¡ ||gS )Ng       @ÚdefaultÚ ÚNoner1   )r8   r3   ÚonesÚshapeÚfloatr   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   )rT   r   )	r   r   r   r   r   r   r   r   Úpr   r   r   Ú_create_patch  s    
zHandlerPatch._create_patchc	       
   	   C   s4   |   |||||||¡}	|  |	||¡ |	 |¡ |	gS )N)rW   r   rO   )
r   r   r   r   r   r   r   r   r$   rV   r   r   r   r   '  s
    
zHandlerPatch.create_artists)N)r%   r&   r'   r(   r   rW   r   r   r   r   r   rS     s   
rS   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   |  ¡ d }|jd }| ¡ d }| |¡ | |¡ | |¡ d S )Nr   )Úget_linewidthsZ_us_linestylesZ
get_colorsZ	set_colorrK   Úset_linewidth)r   r   r   ZlwZdashesZcolorr   r   r   r   :  s    


z*HandlerLineCollection._default_update_propc	             C   sZ   |   ||||||¡\}	}
|| d t |	jt¡ }t|	|ƒ}|  |||¡ | |¡ |gS )Ng       @)r8   r3   rE   rF   rG   r   r   rO   )r   r   r   r   r   r   r   r   r$   r6   r7   r>   rP   r   r   r   r   B  s    


z$HandlerLineCollection.create_artistsN)r%   r&   r'   r(   r0   r   r   r   r   r   r   rX   0  s   rX   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+   rY   )r   r   r   r   r   r0   Y  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 )Nr1   r2   é   g      à?r   )	r]   Ú	get_sizesrJ   ÚmaxrL   Úminr0   r3   r4   )r   r   r   r   r   r   r   r   Zhandle_sizesZsize_maxZsize_minr.   r^   Úrngr   r   r   r`   _  s    

z&HandlerRegularPolyCollection.get_sizesc             C   s0   |   ||¡ | |j¡ | d ¡ | d ¡ d S )N)r   Ú
set_figureZfigurer   r   )r   r   r   r   r   r   r   r   t  s    
z(HandlerRegularPolyCollection.update_propc             C   s"   t |ƒ| ¡ | ¡ |||d}|S )N)Zrotationr^   ÚoffsetsÚtransOffset)ÚtypeZget_numsidesZget_rotation)r   r   r^   re   rf   rV   r   r   r   Úcreate_collection}  s    z.HandlerRegularPolyCollection.create_collectionc	          	   C   sx   |   ||||||¡\}	}
|  ||||||¡}|  |||||||¡}| j||tt|
|ƒƒ|d}|  |||¡ ||_|gS )N)re   rf   )r8   r?   r`   rh   ÚlistÚzipr   Z_transOffset)r   r   r   r   r   r   r   r   r$   r6   r7   r>   r^   rV   r   r   r   r   †  s    



z+HandlerRegularPolyCollection.create_artists)NN)
r%   r&   r'   r(   r   r0   r`   r   rh   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 |ƒ| ¡ d g|||d}|S )Nr   )r^   re   rf   )rg   Z	get_paths)r   r   r^   re   rf   rV   r   r   r   rh   Ÿ  s
    z'HandlerPathCollection.create_collectionN)r%   r&   r'   r(   rh   r   r   r   r   rk   ›  s   rk   c               @   s   e Zd ZdZdd„ ZdS )ÚHandlerCircleCollectionz+
    Handler for `.CircleCollections`.
    c             C   s   t |ƒ|||d}|S )N)re   rf   )rg   )r   r   r^   re   rf   rV   r   r   r   rh   ¬  s    z)HandlerCircleCollection.create_collectionN)r%   r&   r'   r(   rh   r   r   r   r   rl   ¨  s   rl   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)rp   rq   )	r   r   r   r   r   r   r   rr   rs   r   r   r   Úget_err_sizeÁ  s
    


zHandlerErrorbar.get_err_sizec	                s¾  |\}	}
}|   ||||||¡\}}|| d t |jt¡ }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rt‡fd
d„t||ƒD ƒ}t |¡}|  
||d |¡ | |¡ |
rtt||ˆ ƒ}t||ˆ ƒ}|  
||
d |¡ |  
||
d |¡ | d¡ | d¡ | |¡ | |¡ g }| |¡ | |¡ | |¡ | |¡ x|D ]}| |¡ q¦W |S )Ng       @FrB   rD   r1   c                s(   g | ] \}}|ˆ  |f|ˆ  |ff‘qS r   r   )Ú.0ÚxÚy)rr   r   r   ú
<listcomp>ö  s   z2HandlerErrorbar.create_artists.<locals>.<listcomp>r   ú|c                s(   g | ] \}}||ˆ  f||ˆ  ff‘qS r   r   )ru   rv   rw   )rs   r   r   rx     s   Ú_)r8   r3   rE   rF   rG   r   r=   rJ   rt   Zset_visibler   rH   rI   rK   rL   rM   rN   Zhas_xerrrj   ÚmcollZLineCollectionÚappendZhas_yerrÚextendrO   )r   r   r   r   r   r   r   r   r$   Z	plotlinesZcaplinesZbarlinecolsr6   r7   r>   rP   Zydata_markerrQ   rR   Zhandle_barlinecolsZhandle_caplinesZvertsZcollZcapline_leftZcapline_rightr    Úartistr   )rr   rs   r   r   Ì  sz    





























zHandlerErrorbar.create_artists)rn   Nro   N)r%   r&   r'   r(   r   rt   r   r   r   r   r   rm   ´  s
    
rm   c               @   s*   e Zd ZdZd
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 ¡ }|S )Ng      à?)r:   r<   r3   r=   )r   r   r   r   r   r   r   r>   r   r   r   r?   E  s    
zHandlerStem.get_ydatac	             C   s2  |\}	}
}|   ||||||¡\}}|  ||||||¡}| jd krFd}n| j}t||d t|ƒ… ƒ}|  ||	|¡ g }x2t||ƒD ]$\}}t||g||gƒ}| |¡ q€W x$t||
ƒD ]\}}|  |||¡ q´W tt 	|¡t 
|¡g||gƒ}|  |||¡ |g}| |¡ | |¡ x|D ]}| |¡ qW |S )Ng        )r8   r?   r   r   rJ   r   rj   r|   r3   rb   ra   r}   rO   )r   r   r   r   r   r   r   r   r$   Z
markerlineZ	stemlinesZbaseliner6   r7   r>   r‚   Zleg_markerlineZleg_stemlinesZthisxZthisyÚlZlmÚmZleg_baseliner    r~   r   r   r   r   M  s2    








zHandlerStem.create_artists)r€   NNN)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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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 )Nr1   )Zget_legend_handler_mapr†   rJ   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handle1ZhandlerZ_a_listr   r   r   r     s$    




zHandlerTuple.create_artists)r1   N)r%   r&   r'   r(   r   r   r   r   r   r   r…   x  s   
r…   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚHandlerPolyCollectionz]
    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   Znone)ÚmcolorsZto_rgba_arrayrJ   )Úcolorsr   r   r   Úfirst_color´  s    
z7HandlerPolyCollection._update_prop.<locals>.first_colorc             S   s   t | ƒr| d S d S d S )Nr   )rJ   )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[   rZ   rK   Zget_linestylesrO   Zget_transformsrd   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)rU   r   r   )r   r   rO   )
r   r   r   r   r   r   r   r   r$   rV   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@   rS   rX   r\   rk   rl   rm   r   r…   rŒ   r   r   r   r   Ú<module>   s(   Y, 4. KqS7