B
    cdw                 @   sb  d Z ddlm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 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G d$d% d%eZG d&d' d'eZ G d(d) d)eZ!dS )*a  
Default legend handlers.

.. important::

    This is a low-level legend API, which most end users do not need.

    We recommend that you are familiar with the :doc:`legend guide
    </tutorials/intermediate/legend_guide>` before reading 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)
    )Sequence)cycleN)_api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   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__?   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_propC   s    
zHandlerBase._update_propc             C   s   | | d S )N)r
   )r   r   r   r   r   r   r   I   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_propL   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_areaT   s
    zHandlerBase.adjust_drawing_areac             C   sv   |  |||j|j|j|j|\}}}}| |||||||| }	t|	trV|	d g}	x|	D ]}
|	|
 q\W |	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_transform
isinstance_Line2DHandleListZ
add_artist)r   r   r   r"   Z	handleboxr   r   r    r!   artistsar   r   r   legend_artist]   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                   s2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )HandlerNpointszM
    A legend handler that shows *numpoints* points in the legend entry.
    333333?Nc                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.
        **kwargs
            Keyword arguments forwarded to `.HandlerBase`.
        N)superr   
_numpoints_marker_pad)r   
marker_pad	numpointskwargs)	__class__r   r   r      s    zHandlerNpoints.__init__c             C   s   | j d kr|jS | j S d S )N)r3   r6   )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      ?)r9   r4   nplinspace)r   r   r   r   r    r!   r"   r6   padxdataxdata_markerr   r   r   	get_xdata   s    

zHandlerNpoints.get_xdata)r1   N)r,   r-   r.   r/   r   r9   r@   __classcell__r   r   )r8   r   r0      s   r0   c                   s*   e Zd ZdZd fdd	Zdd Z  ZS )HandlerNpointsYoffsetsz
    A legend handler that shows *numpoints* in the legend, and allows them to
    be individually offset in the y-direction.
    Nc                s    t  jf d|i| || _dS )aL  
        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.
        **kwargs
            Keyword arguments forwarded to `.HandlerNpoints`.
        r6   N)r2   r   	_yoffsets)r   r6   yoffsetsr7   )r8   r   r   r      s    zHandlerNpointsYoffsets.__init__c             C   s*   | j d kr||j }n|t| j  }|S )N)rC   _scatteryoffsetsr;   asarray)r   r   r   r   r    r!   r"   ydatar   r   r   	get_ydata   s    
z HandlerNpointsYoffsets.get_ydata)NN)r,   r-   r.   r/   r   rH   rA   r   r   )r8   r   rB      s   rB   c                   s*   e Zd ZdZd fdd	Zdd Z  ZS )	HandlerLine2DCompoundz
    Original handler for `.Line2D` instances, that relies on combining
    a line-only with a marker-only artist.  May be deprecated in the future.
    333333?Nc                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.
        **kwargs
            Keyword arguments forwarded to `.HandlerNpoints`.
        )r5   r6   N)r2   r   )r   r5   r6   r7   )r8   r   r   r      s    zHandlerLine2DCompound.__init__c	             C   s   |  ||||||\}	}
t|	|| d }t|	|}| ||| |d |d t|
|d t|
 }| ||| |d |j	dkr|
 |j	 }|| ||_|| || ||gS )N   default Noner:   )r@   r;   	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?   rG   leglinelegline_markernewszr   r   r   r$      s"    








z$HandlerLine2DCompound.create_artists)rJ   N)r,   r-   r.   r/   r   r$   rA   r   r   )r8   r   rI      s   rI   c               @   s$   e Zd Zdd Zdd Zdd ZdS )r&   c             C   s
   || _ d S )N)_legline)r   rX   r   r   r   r     s    z_Line2DHandleList.__init__c             C   s   dS )NrK   r   )r   r   r   r   __len__	  s    z_Line2DHandleList.__len__c             C   s&   |dkrt jddd | j| jg| S )Nr   z3.5zuAccess to the second element returned by HandlerLine2D is deprecated since %(since)s; it will be removed %(removal)s.)message)r   Zwarn_deprecatedr[   )r   indexr   r   r   __getitem__  s    
z_Line2DHandleList.__getitem__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S )	HandlerLine2Dz
    Handler for `.Line2D` instances.

    See Also
    --------
    HandlerLine2DCompound : An earlier handler implementation, which used one
                            artist for the line and another for the marker(s).
    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.
        **kwargs
            Keyword arguments forwarded to `.HandlerNpoints`.
        )r5   r6   N)r0   r   )r   r5   r6   kwr   r   r   r   !  s    
zHandlerLine2D.__init__c	             C   s   |  ||||||\}	}
d }| |dkrFt|	d |	d d}	dg}t|	|| d }t|	||d}| ||| |jdkr| |j }|	| |
| t|S )Nr:   r      rK   )	markevery)r@   r9   r;   r<   rO   r   r   rT   rU   rV   rW   r&   )r   r   r   r   r   r    r!   r"   r+   r>   r?   re   rG   rX   rZ   r   r   r   r$   /  s    



zHandlerLine2D.create_artists)ra   N)r,   r-   r.   r/   r   r$   r   r   r   r   r`     s   
r`   c                   s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
HandlerPatchz)
    Handler for `.Patch` instances.
    Nc                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.

        **kwargs
            Keyword arguments forwarded to `.HandlerBase`.
        N)r2   r   _patch_func)r   Z
patch_funcr7   )r8   r   r   r   Q  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"   )rg   r   )	r   r   r   r   r   r    r!   r"   pr   r   r   _create_patchf  s    
zHandlerPatch._create_patchc	       
   	   C   s4   |  |||||||}	| |	|| |	| |	gS )N)rj   r   rW   )
r   r   r   r   r   r    r!   r"   r+   ri   r   r   r   r$   q  s
    
zHandlerPatch.create_artists)N)r,   r-   r.   r/   r   rj   r$   rA   r   r   )r8   r   rf   L  s   rf   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	HandlerStepPatchzA
    Handler for `~.matplotlib.patches.StepPatch` instances.
    c       	      C   s    t | | f| ||d}|S )N)rh   colorr    r!   )r   get_facecolor)	r   r   r   r   r   r    r!   r"   ri   r   r   r   rj     s    
zHandlerStepPatch._create_patchc       	      C   sF   t d|g|d |d g| | | d}|d |d |S )Nr   rK   )rl   Z	linestyleZ	linewidthrL   rM   )r   get_edgecolorget_linestyleZget_linewidthrP   rQ   )	r   r   r   r   r   r    r!   r"   rX   r   r   r   _create_line  s    

zHandlerStepPatch._create_linec	       
   	   C   s`   |  s| d k	r:| |||||||}	| |	|| n| |||||||}	|	| |	gS )N)get_fill	get_hatchrj   r   rp   rW   )
r   r   r   r   r   r    r!   r"   r+   ri   r   r   r   r$     s    
zHandlerStepPatch.create_artistsN)r,   r-   r.   r/   rj   rp   r$   r   r   r   r   rk   z  s   rk   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)r3   scatterpoints)r   r   r   r   r   r9     s    
z#HandlerLineCollection.get_numpointsc             C   sD   |  d }|jd }| d }|| || || d S )Nr   )get_linewidthsZ_us_linestylesZ
get_colors	set_colorrS   set_linewidth)r   r   r   ZlwZdashesrl   r   r   r   r     s    


z*HandlerLineCollection._default_update_propc	             C   sT   |  ||||||\}	}
t|	|| d }t|	|}| ||| || |gS )NrK   )r@   r;   rO   r   r   rW   )r   r   r   r   r   r    r!   r"   r+   r>   r?   rG   rX   r   r   r   r$     s    


z$HandlerLineCollection.create_artistsN)r,   r-   r.   r/   r9   r   r$   r   r   r   r   rs     s   rs   c                   sJ   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Zdd Z	  Z
S )HandlerRegularPolyCollectionz'Handler for `.RegularPolyCollection`\s.Nc                s    t  jf d|i| || _d S )NrD   )r2   r   _sizes)r   rD   sizesr7   )r8   r   r   r     s    z%HandlerRegularPolyCollection.__init__c             C   s   | j d kr|jS | j S d S )N)r3   rt   )r   r   r   r   r   r9     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 )Nr:   rK      g      ?r   )	ry   	get_sizesrR   maxrT   minr9   r;   r<   )r   r   r   r   r   r    r!   r"   Zhandle_sizesZsize_maxZsize_minr6   rz   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     s    
z(HandlerRegularPolyCollection.update_propc             C   s"   t || | |||d}|S )N)Zrotationrz   offsetstransOffset)typeZget_numsidesZget_rotation)r   r   rz   r   r   ri   r   r   r   create_collection  s    z.HandlerRegularPolyCollection.create_collectionc	          	   C   sx   |  ||||||\}	}
| ||||||}| |||||||}| j||tt|
||d}| ||| ||_|gS )N)r   r   )r@   rH   r|   r   listzipr   Z_transOffset)r   r   r   r   r   r    r!   r"   r+   r>   r?   rG   rz   ri   r   r   r   r$     s    



z+HandlerRegularPolyCollection.create_artists)NN)r,   r-   r.   r/   r   r9   r|   r   r   r$   rA   r   r   )r8   r   rx     s   		rx   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   )rz   r   r   )r   Z	get_paths)r   r   rz   r   r   ri   r   r   r   r     s
    z'HandlerPathCollection.create_collectionN)r,   r-   r.   r/   r   r   r   r   r   r     s   r   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)r   r   )r   )r   r   rz   r   r   ri   r   r   r   r     s    z)HandlerCircleCollection.create_collectionN)r,   r-   r.   r/   r   r   r   r   r   r     s   r   c                   s2   e Zd ZdZd fdd	Zdd Zd	d
 Z  ZS )HandlerErrorbarzHandler for Errorbars.      ?N333333?c                s(   || _ || _t jf ||d| d S )N)r5   r6   )
_xerr_size
_yerr_sizer2   r   )r   	xerr_size	yerr_sizer5   r6   r7   )r8   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  |\}	}
}|  ||||||\}}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 | || |
rt|  |}t|  |}| ||
d | | ||
d | |
d	 |
d	 || || |jrlfd
dt||D }t|}| ||d | || |
rlt|| }t|| }| ||
d | | ||
d | |
d |
d || || ||||f}x|D ]}|| qW |S )NrK   FrL   rN   r:   c                s(   g | ] \}}|  |f|  |ffqS r   r   ).0xy)r   r   r   
<listcomp>a  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  s   _)r@   r;   rO   r   rF   rR   r   Zset_visibler   rP   rQ   rS   rT   rU   rV   Zhas_xerrr   mcollLineCollectionappendZhas_yerrrW   )r   r   r   r   r   r    r!   r"   r+   Z	plotlinesZcaplinesZbarlinecolsr>   r?   rG   rX   Zydata_markerrY   rZ   Zhandle_barlinecolsZhandle_caplinesZvertsZcollZcapline_leftZcapline_rightr'   artistr   )r   r   r   r$   7  sr    


























zHandlerErrorbar.create_artists)r   Nr   N)r,   r-   r.   r/   r   r   r$   rA   r   r   )r8   r   r   !  s
    r   c                   s:   e Zd ZdZd fdd	Zdd Zdd	 Zd
d Z  ZS )HandlerStemz6
    Handler for plots produced by `~.Axes.stem`.
    333333?Nc                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.
        **kwargs
            Keyword arguments forwarded to `.HandlerNpointsYoffsets`.
        )r5   r6   rD   N)r2   r   _bottom)r   r5   r6   bottomrD   r7   )r8   r   r   r     s    zHandlerStem.__init__c             C   s2   | j d kr|d|j d  }n|t| j  }|S )Ng      ?)rC   rE   r;   rF   )r   r   r   r   r    r!   r"   rG   r   r   r   rH     s    
zHandlerStem.get_ydatac	          	      sN  |\}	}
}t |
tj}| ||||||\}}| ||||||}| jd krRd n| j t||d t| }| ||	|  fddt	||D }|rt
j| | jd" x|D ]}| ||
| qW W d Q R X n&x$t	||
D ]\}}| ||| qW tt|t|g  g}| ||| |||f}x|D ]}|| q6W |S )Ng        c                s"   g | ]\}}t ||g |gqS r   )r   )r   r   r   )r   r   r   r     s   z.HandlerStem.create_artists.<locals>.<listcomp>)r   )r%   r   r   r@   rH   r   r   rR   r   r   r   Z_setattr_cm_copy_collection_propsr;   r~   r}   rW   )r   r   r   r   r   r    r!   r"   r+   Z
markerlineZ	stemlinesZbaselineZusing_linecollr>   r?   rG   Zleg_markerlineZleg_stemlineslineZlmmZleg_baseliner'   r   r   )r   r   r$     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)rv   Z	get_colorrS   ro   )r   r   r   r   r   r   r     s    z"HandlerStem._copy_collection_props)r   NNN)	r,   r-   r.   r/   r   rH   r$   r   rA   r   r   )r8   r   r     s    /r   c                   s*   e Zd ZdZd fdd	Zdd Z  ZS )	HandlerTuplez
    Handler for Tuple.
    r:   Nc                s   || _ || _t jf | dS )a  
        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.
        **kwargs
            Keyword arguments forwarded to `.HandlerBase`.
        N)_ndivide_padr2   r   )r   ndivider=   r7   )r8   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 }xR|D ]J}||	|}|	||t
||||||}t|tr|d g}|| qW |S )Nr:   r   )Zget_legend_handler_mapr   rR   r   Z	borderpadr   r;   ZarangeZget_legend_handlerr$   r   r%   r&   extend)r   r   r   r   r   r    r!   r"   r+   Zhandler_mapr   r=   Z	xds_cyclea_listZhandle1ZhandlerZ_a_listr   r   r   r$     s(    






zHandlerTuple.create_artists)r:   N)r,   r-   r.   r/   r   r$   rA   r   r   )r8   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 }||  |_|| |_|j|_|j|_| |_| |_	|j
|_
|||  |||  |||  ||  d S )Nc             S   s   | j dkrdS t| d S )Nr   )r   r   r   r   )sizetuple)Zcolorsr   r   r   first_color#  s    
z7HandlerPolyCollection._update_prop.<locals>.first_colorc             S   s   t | r| d S d S d S )Nr   )rR   )Z
prop_arrayr   r   r   	get_first(  s    z5HandlerPolyCollection._update_prop.<locals>.get_first)rm   Z
_facecolorrn   Z
_edgecolorZ_original_facecolorZ_original_edgecolorrq   Z_fillrr   Z_hatchZ_hatch_colorrw   ru   rS   Zget_linestylesrW   Zget_transformsr   Z
get_figure)r   r   r   r   r   r   r   r   r   "  s    

z"HandlerPolyCollection._update_propc	       
      C   s4   t | | f||d}	| |	|| |	| |	gS )N)rh   r    r!   )r   r   rW   )
r   r   r   r   r   r    r!   r"   r+   ri   r   r   r   r$   ?  s
    

z$HandlerPolyCollection.create_artistsN)r,   r-   r.   r/   r   r$   r   r   r   r   r     s   r   )"r/   Zcollections.abcr   	itertoolsr   Znumpyr;   Z
matplotlibr   r   Zmatplotlib.linesr   Zmatplotlib.patchesr   Zmatplotlib.collectionscollectionsr   r   r   r0   rB   rI   r&   r`   rf   rk   rs   rx   r   r   r   r   r   r   r   r   r   r   <module>   s0   \*25.( J
kZ7