o
    icf                     @   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 ur| | d S d S N)nextiterget_childrenupdate_from)tgtsrcfirst_child r   T/var/www/edux/Edux_v2/venv/lib/python3.10/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 r   )_xpad_ypad_update_prop_func)selfxpadypadupdate_funcr   r   r   __init__;   s   
zHandlerBase.__init__c                 C   s*   | j d u r| || d S |  || d S r   )r   _default_update_propr   legend_handleorig_handler   r   r   _update_prop?   s   
zHandlerBase._update_propc                 C   s   | | d S r   )r   r   r   r   r   r   E   s   z HandlerBase._default_update_propc                 C   s.   |  || || |d  |d  d S r   )r    _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 r   )r   r   )r   r%   r   xdescentydescentwidthheightfontsizer   r   r   adjust_drawing_areaP   s
   zHandlerBase.adjust_drawing_areac                 C   s^   |  |||j|j|j|j|\}}}}| |||||||| }	|	D ]}
||
 q#|	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_artistsget_transform
add_artist)r   r%   r   r+   	handleboxr'   r(   r)   r*   artistsar   r   r   legend_artistY   s   zHandlerBase.legend_artistc	           	      C   s   t d)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,   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d	 ZdS )HandlerNpointszM
    A legend handler that shows *numpoints* points in the legend entry.
    333333?Nc                 K   s"   t j| fi | || _|| _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      | j d u r|jS | j S r   )r<   r@   r   r%   r   r   r   get_numpoints      
zHandlerNpoints.get_numpointsc                 C   sn   |  |}|dkr#| j| }t| | | | | |}	|	}
|	|
fS | | | g}	| d|  g}
|	|
fS )N         ?)rD   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)r6   r7   r8   r9   r   rD   rM   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`.
        r@   N)r:   r   	_yoffsets)r   r@   yoffsetsrA   r   r   r   r      s   
zHandlerNpointsYoffsets.__init__c                 C   s,   | j d u r||j }|S |t| j  }|S r   rP   _scatteryoffsetsrH   asarrayr   r%   r'   r(   r)   r*   r+   ydatar   r   r   	get_ydata   s
   

z HandlerNpointsYoffsets.get_ydataNN)r6   r7   r8   r9   r   rW   r   r   r   r   rO      s    
rO   c                   @   "   e Zd ZdZdddZdd ZdS )	HandlerLine2Dz*
    Handler for `.Line2D` instances.
    r;   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      s
   

zHandlerLine2D.__init__c	                 C   s   |  ||||||\}	}
t|	|| d }t|	|}| ||| |d |d t|
|d t|
 }| ||| |d |j	dkrT|
 |j	 }|| ||_|| || ||gS )N   default NonerF   )rM   rH   	full_liker   r&   set_drawstyle
set_markerlenset_linestylemarkerscaleget_markersizeset_markersize
_legmarkerset_transform)r   r%   r   r'   r(   r)   r*   r+   r5   rK   rL   rV   leglinelegline_markernewszr   r   r   r-      s$   








zHandlerLine2D.create_artistsrN   r6   r7   r8   r9   r   r-   r   r   r   r   rZ      s    
rZ   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i | || _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   
patch_funcrA   r   r   r   r     s   
zHandlerPatch.__init__c           	   	   C   s@   | j d u rt| | f||d}|S | j |||||||d}|S )Nxyr)   r*   )r%   r   r'   r(   r)   r*   r+   )ro   r   )	r   r%   r   r'   r(   r)   r*   r+   pr   r   r   _create_patch  s   
zHandlerPatch._create_patchc	           
   	   C   s4   |  |||||||}	| |	|| |	| |	gS r   )rt   r&   ri   
r   r%   r   r'   r(   r)   r*   r+   r5   rs   r   r   r   r-   )  s   

zHandlerPatch.create_artistsr   )r6   r7   r8   r9   r   rt   r-   r   r   r   r   rn     s
    
rn   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   rB   r   r<   scatterpointsrC   r   r   r   rD   6  rE   z#HandlerLineCollection.get_numpointsc                 C   sD   |  d }|jd }| d }|| || || d S Nr   )get_linewidths_us_linestyles
get_colors	set_colorrd   set_linewidth)r   r   r   lwdashescolorr   r   r   r   <  s   


z*HandlerLineCollection._default_update_propc	                 C   sT   |  ||||||\}	}
t|	|| d }t|	|}| ||| || |gS )Nr\   )rM   rH   r`   r   r&   ri   )r   r%   r   r'   r(   r)   r*   r+   r5   rK   rL   rV   rj   r   r   r   r-   D  s   


z$HandlerLineCollection.create_artistsN)r6   r7   r8   r9   rD   r   r-   r   r   r   r   rv   2  s
    rv   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 )NrQ   )rO   r   _sizes)r   rQ   sizesrA   r   r   r   r   U  s   
z%HandlerRegularPolyCollection.__init__c                 C   rB   r   rw   rC   r   r   r   rD   Z  rE   z*HandlerRegularPolyCollection.get_numpointsc                 C   s   | j d u rK| }t|sdg}t||jd  }	t||jd  }
| |}|dk r:d|	|
  |	|
gd | }|S |	|
 }|tdd| |
 }|S | j }|S )NrF   r\      rG   r   )	r   	get_sizesrc   maxre   minrD   rH   rI   )r   r%   r   r'   r(   r)   r*   r+   handle_sizessize_maxsize_minr@   r   rngr   r   r   r   `  s&   

z&HandlerRegularPolyCollection.get_sizesc                 C   s0   |  || ||j |d  |d  d S r   )r    
set_figurefigurer"   r#   r$   r   r   r   r&   u  s   
z(HandlerRegularPolyCollection.update_propc                 C   s"   t || | |||d}|S )N)rotationr   offsetstransOffset)typeget_numsidesget_rotationr   r   r   r   r   rs   r   r   r   create_collection~  s   z.HandlerRegularPolyCollection.create_collectionc	              	   C   sx   |  ||||||\}	}
| ||||||}| |||||||}| j||tt|
||d}| ||| ||_|gS N)r   r   )rM   rW   r   r   listzipr&   _transOffset)r   r%   r   r'   r(   r)   r*   r+   r5   rK   rL   rV   r   rs   r   r   r   r-     s    

z+HandlerRegularPolyCollection.create_artistsrX   )
r6   r7   r8   r9   r   rD   r   r&   r   r-   r   r   r   r   r   R  s    
		r   c                   @      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   )r   r   r   )r   	get_pathsr   r   r   r   r     s   z'HandlerPathCollection.create_collectionNr6   r7   r8   r9   r   r   r   r   r   r         r   c                   @   r   )HandlerCircleCollectionz"Handler for `.CircleCollection`\s.c                 C   s   t ||||d}|S r   )r   r   r   r   r   r     s
   z)HandlerCircleCollection.create_collectionNr   r   r   r   r   r     r   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.rG   Nr;   c                 K   s(   || _ || _tj| f||d| d S )Nr[   )
_xerr_size
_yerr_sizerZ   r   )r   	xerr_size	yerr_sizer?   r@   rA   r   r   r   r     s   

zHandlerErrorbar.__init__c           	      C   s2   | j | }| jd u r|}||fS | j| }||fS r   )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 u rP|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r4fd
dt||D }t|}| ||d | || |
r4t|| }t|| }| ||
d | | ||
d | |
d |
d || || g ||||}|D ]}|| q@|S )Nr\   Fr]   r_   rF   c                    s(   g | ]\}}|  |f|  |ffqS r   r   .0xy)r   r   r   
<listcomp>      z2HandlerErrorbar.create_artists.<locals>.<listcomp>r   |c                    s(   g | ]\}}||  f||  ffqS r   r   r   )r   r   r   r     r   _)rM   rH   r`   r   rT   rc   r   set_visibler&   ra   rb   rd   re   rf   rg   has_xerrr   mcollLineCollectionappendhas_yerrri   )r   r%   r   r'   r(   r)   r*   r+   r5   	plotlinescaplinesbarlinecolsrK   rL   rV   rj   ydata_markerrk   rl   handle_barlinecolshandle_caplinesvertscollcapline_leftcapline_rightr1   artistr   )r   r   r   r-     s   

























zHandlerErrorbar.create_artists)rG   Nr;   N)r6   r7   r8   r9   r   r   r-   r   r   r   r   r     s    
	r   c                   @   s6   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`.
    r;   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`.
        )r?   r@   rQ   N)rO   r   _bottom)r   r?   r@   bottomrQ   rA   r   r   r   r   !  s   

zHandlerStem.__init__c                 C   s4   | j d u r|d|j d  }|S |t| j  }|S )NrG   rR   rU   r   r   r   rW   =  s
   
zHandlerStem.get_ydatac	                    sX  |\}	}
}t |
tj}| ||||||\}}| ||||||}| jd u r)d n| j t||d t| }| ||	|  fddt	||D }|rqt
j| | jd |D ]	}| ||
| qWW d    n1 skw   Y  nt	||
D ]\}}| ||| qvtt|t|g  g}| ||| g |||}|D ]}|| q|S )Nr   c                    s"   g | ]\}}t ||g |gqS r   r   r   r   r   r   r   \  s    z.HandlerStem.create_artists.<locals>.<listcomp>)r   )
isinstancer   r   rM   rW   r   r   rc   r&   r   r   _setattr_cm_copy_collection_propsrH   r   r   ri   )r   r%   r   r'   r(   r)   r*   r+   r5   
markerline	stemlinesbaselineusing_linecollrK   rL   rV   leg_markerlineleg_stemlineslinelmmleg_baseliner1   r   r   r   r   r-   E  sF   




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}   	get_colorrd   get_linestyler   r   r   r   r   t  s   z"HandlerStem._copy_collection_props)r;   NNN)r6   r7   r8   r9   r   rW   r-   r   r   r   r   r   r     s    
/r   c                   @   rY   )	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.
    rF   Nc                 K   s"   || _ || _tj| fi | d S r   )_ndivide_padr   r   )r   ndividerJ   kwargsr   r   r   r     s   zHandlerTuple.__init__c	                 C   s   |  }	| jd u rt|}
n| j}
| jd u r|j| }n| j| }|
dkr/|||
d   |
 }t||| t|
  }g }|D ]}||	|}|	||t
||||||}|| q@|S )NrF   )get_legend_handler_mapr   rc   r   	borderpadr   rH   arangeget_legend_handlerr-   r   extend)r   r%   r   r'   r(   r)   r*   r+   r5   handler_mapr   rJ   	xds_cyclea_listhandle1handler_a_listr   r   r   r-     s&   



zHandlerTuple.create_artists)rF   Nrm   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 u rd S t | } t| r| d S dS )Nr   none)mcolorsto_rgba_arrayrc   )colorsr   r   r   first_color  s   
z7HandlerPolyCollection._update_prop.<locals>.first_colorc                 S   s   t | r| d S d S ry   )rc   )
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_hatchr~   rz   rd   get_linestylesri   get_transformsr   
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}	| |	|| |	| |	gS )Nrq   )r   r&   ri   ru   r   r   r   r-     s   
z$HandlerPolyCollection.create_artistsN)r6   r7   r8   r9   r    r-   r   r   r   r   r     s    r   )r9   	itertoolsr   numpyrH   
matplotlibr   matplotlib.linesr   matplotlib.patchesr   matplotlib.collectionscollectionsr   matplotlib.colorsr   r   r   r   r:   rO   rZ   rn   rv   r   r   r   r   r   r   r   r   r   r   r   <module>   s,    Y, 4. J
l`4