o
    h                     @   s  d Z ddlZddl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lmZ ddlmZ ddlmZ ddlmZmZmZ ddlmZmZmZ dZ d	d
 Z!dd Z"d3ddZ#d4ddZ$G dd de
j%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%d& Z/G d'd( d(e.Z0G d)d* d*e&Z1G d+d, d,e
j%ej2Z3G d-d. d.Z4G d/d0 d0e4Z5G d1d2 d2e4Z6dS )5ac  
Container classes for `.Artist`\s.

`OffsetBox`
    The base of all container artists defined in this module.

`AnchoredOffsetbox`, `AnchoredText`
    Anchor and align an arbitrary `.Artist` or a text relative to the parent
    axes or a specific anchor point.

`DrawingArea`
    A container with fixed width and height. Children have a fixed position
    inside the container and may be clipped.

`HPacker`, `VPacker`
    Containers for layouting their children vertically or horizontally.

`PaddedBox`
    A container to add a padding around an `.Artist`.

`TextArea`
    Contains a single `.Text` instance.
    N)_api
_docstring)FontProperties)	BboxImage)FancyBboxPatchFancyArrowPatchbbox_artist)BboxBboxBaseTransformedBboxFc                    s,   dd dd gt   fdd}|S )z
    Decorator for the get_offset method of OffsetBox and subclasses, that
    allows supporting both the new signature (self, bbox, renderer) and the old
    signature (self, width, height, xdescent, ydescent, renderer).
    c                 S      t  S Nlocals)selfwidthheightxdescentydescentrenderer r   h/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/offsetbox.py<lambda>3       z$_compat_get_offset.<locals>.<lambda>c                 S   r   r   r   r   bboxr   r   r   r   r   4   r   c                    sb   t j| g|R i |}d|v r|d nt|d  |d  |d |d } |d ||d S )Nr   r   r   r   r   r   r   )r   select_matching_signaturer	   from_bounds)r   argskwargsparamsr   methsigsr   r   
get_offset6   s   z&_compat_get_offset.<locals>.get_offset)	functoolswraps)r"   r$   r   r!   r   _compat_get_offset-   s   r'   c                  O   s   t rt| i | d S d S r   )DEBUGmbbox_artist)r   r   r   r   r   _bbox_artistA   s   r*   fixedc                    s<  t jg d|d |dkr0tdg fdd| D  }|dd }|du r,|d   }||fS |d	krh|du r:d
}t| d
krM|t|  t| d
   nd tdg fdd| D  }|dd }||fS |dkrt| }|du r du r|td|  t|  }n|t|  |  |  tt|  }||fS dS )a[  
    Pack boxes specified by their *widths*.

    For simplicity of the description, the terminology used here assumes a
    horizontal layout, but the function works equally for a vertical layout.

    There are three packing *mode*\s:

    - 'fixed': The elements are packed tight to the left with a spacing of
      *sep* in between. If *total* is *None* the returned total will be the
      right edge of the last box. A non-*None* total will be passed unchecked
      to the output. In particular this means that right edge of the last
      box may be further to the right than the returned total.

    - 'expand': Distribute the boxes with equal spacing so that the left edge
      of the first box is at 0, and the right edge of the last box is at
      *total*. The parameter *sep* is ignored in this mode. A total of *None*
      is accepted and considered equal to 1. The total is returned unchanged
      (except for the conversion *None* to 1). If the total is smaller than
      the sum of the widths, the laid out boxes will overlap.

    - 'equal': If *total* is given, the total space is divided in N equal
      ranges and each box is left-aligned within its subspace.
      Otherwise (*total* is *None*), *sep* must be provided and each box is
      left-aligned in its subspace of width ``(max(widths) + sep)``. The
      total width is then calculated to be ``N * (max(widths) + sep)``.

    Parameters
    ----------
    widths : list of float
        Widths of boxes to be packed.
    total : float or None
        Intended total length. *None* if not used.
    sep : float or None
        Spacing between boxes.
    mode : {'fixed', 'expand', 'equal'}
        The packing mode.

    Returns
    -------
    total : float
        The total width needed to accommodate the laid out boxes.
    offsets : array of float
        The left offsets of the boxes.
    )r+   expandequal)moder+   r   c                       g | ]}|  qS r   r   .0wsepr   r   
<listcomp>w       z'_get_packed_offsets.<locals>.<listcomp>Nr,      c                    r/   r   r   r0   r3   r   r   r5      r6   r-   z@total and sep cannot both be None when using layout mode 'equal')	r   check_in_listnpcumsumlensummax
ValueErrorarange)widthstotalr4   r.   offsets_offsetsmaxhr   r3   r   _get_packed_offsetsF   s4   .rF   baselinec                    s   t jg d|d  du rtdd | D  |dkr7tdd | D tdd | D f}d	gt|  }||fS |d
v rJd	 f}dd | D }||fS |dv r_d	 f} fdd| D }||fS |dkrpd	 f} fdd| D }||fS )a  
    Align boxes each specified by their ``(y0, y1)`` spans.

    For simplicity of the description, the terminology used here assumes a
    horizontal layout (i.e., vertical alignment), but the function works
    equally for a vertical layout.

    Parameters
    ----------
    yspans
        List of (y0, y1) spans of boxes to be aligned.
    height : float or None
        Intended total height. If None, the maximum of the heights
        (``y1 - y0``) in *yspans* is used.
    align : {'baseline', 'left', 'top', 'right', 'bottom', 'center'}
        The alignment anchor of the boxes.

    Returns
    -------
    (y0, y1)
        y range spanned by the packing.  If a *height* was originally passed
        in, then for all alignments other than "baseline", a span of ``(0,
        height)`` is used without checking that it is actually large enough).
    descent
        The descent of the packing.
    offsets
        The bottom offsets of the boxes.
    )rG   lefttoprightbottomcenter)alignNc                 s   s    | ]	\}}|| V  qd S r   r   r1   y0y1r   r   r   	<genexpr>   s    z'_get_aligned_offsets.<locals>.<genexpr>rG   c                 s   s    | ]\}}|V  qd S r   r   rN   r   r   r   rQ          c                 s   s    | ]\}}|V  qd S r   r   rN   r   r   r   rQ      rR   r   )rH   rK   c                 S   s   g | ]\}}| qS r   r   rN   r   r   r   r5          z(_get_aligned_offsets.<locals>.<listcomp>)rJ   rI   c                    s   g | ]\}} | qS r   r   rN   r   r   r   r5          rL   c                    s$   g | ]\}} ||  d  | qS )      ?r   rN   rT   r   r   r5      s   $ )r   r9   r>   minr<   )yspansr   rM   yspanrD   r   rT   r   _get_aligned_offsets   s*   $rZ   c                       s   e Zd ZdZ fddZ fddZejjj	dd Zdd	 Z
d
d Zedd Zdd Zdd Zdd Zdd Zdd Zdd ZdddZdd Z  ZS ) 	OffsetBoxz
    The OffsetBox is a simple container artist.

    The child artists are meant to be drawn at a relative position to its
    parent.

    Being an artist itself, all parameters are passed on to `.Artist`.
    c                    s0   t  j|  | | | d g | _d| _d S )NFr   r   )super__init___internal_updateset_clip_on	_children_offset)r   r   r   	__class__r   r   r^      s
   


zOffsetBox.__init__c                    s(   t  | |  D ]}|| q
dS )z
        Set the `.Figure` for the `.OffsetBox` and all its children.

        Parameters
        ----------
        fig : `~matplotlib.figure.Figure`
        N)r]   
set_figureget_children)r   figcrc   r   r   re      s   zOffsetBox.set_figurec                 C   s0   t jj| | |  D ]	}|d ur||_qd S r   )martistArtistaxesfsetrf   )r   axrh   r   r   r   rk      s   zOffsetBox.axesc                 C   sF   |  |r	di fS |  D ]}||\}}|r||f  S qdi fS )a  
        Delegate the mouse event contains-check to the children.

        As a container, the `.OffsetBox` does not respond itself to
        mouseevents.

        Parameters
        ----------
        mouseevent : `~matplotlib.backend_bases.MouseEvent`

        Returns
        -------
        contains : bool
            Whether any values are within the radius.
        details : dict
            An artist-specific dictionary of details of the event context,
            such as which points are contained in the pick radius. See the
            individual Artist subclasses for details.

        See Also
        --------
        .Artist.contains
        F)_different_canvasrf   contains)r   
mouseeventrh   abr   r   r   ro      s   
zOffsetBox.containsc                 C      || _ d| _dS )a  
        Set the offset.

        Parameters
        ----------
        xy : (float, float) or callable
            The (x, y) coordinates of the offset in display units. These can
            either be given explicitly as a tuple (x, y), or by providing a
            function that converts the extent into the offset. This function
            must have the signature::

                def offset(width, height, xdescent, ydescent, renderer) -> (float, float)
        TN)rb   staler   xyr   r   r   
set_offset  s   
zOffsetBox.set_offsetc                 C   s.   t | jr| |j|j|j |j |S | jS )aO  
        Return the offset as a tuple (x, y).

        The extent parameters have to be provided to handle the case where the
        offset is dynamically determined by a callable (see
        `~.OffsetBox.set_offset`).

        Parameters
        ----------
        bbox : `.Bbox`
        renderer : `.RendererBase` subclass
        )callablerb   r   r   x0rO   r   r   r   r   r$   #  s
   zOffsetBox.get_offsetc                 C   rs   )zh
        Set the width of the box.

        Parameters
        ----------
        width : float
        TN)r   rt   )r   r   r   r   r   	set_width6     
zOffsetBox.set_widthc                 C   rs   )zj
        Set the height of the box.

        Parameters
        ----------
        height : float
        TN)r   rt   )r   r   r   r   r   
set_heightA  r{   zOffsetBox.set_heightc                 C   s   dd | j D S )z/Return a list of the visible child `.Artist`\s.c                 S   s   g | ]}|  r|qS r   )get_visibler1   rh   r   r   r   r5   N  rU   z2OffsetBox.get_visible_children.<locals>.<listcomp>ra   r   r   r   r   get_visible_childrenL  s   zOffsetBox.get_visible_childrenc                 C      | j S )z'Return a list of the child `.Artist`\s.r   r   r   r   r   rf   P     zOffsetBox.get_childrenc                 C   s   t d)a/  
        Return the bbox of the offsetbox and the child offsets.

        The bbox should satisfy ``x0 <= x1 and y0 <= y1``.

        Parameters
        ----------
        renderer : `.RendererBase` subclass

        Returns
        -------
        bbox
        list of (xoffset, yoffset) pairs
        z:get_bbox_and_offsets must be overridden in derived classes)NotImplementedErrorr   r   r   r   r   _get_bbox_and_child_offsetsT  s   z%OffsetBox._get_bbox_and_child_offsetsc                 C   s   |  |\}}|S )z:Return the bbox of the offsetbox, ignoring parent offsets.)r   )r   r   r   rD   r   r   r   get_bboxf  s   zOffsetBox.get_bboxNc                 C   sb   |d u r| j dd }| |}z
| ||\}}W n ty*   |  \}}Y nw |||S NTroot)
get_figure_get_rendererr   r$   	TypeError
translated)r   r   r   pxpyr   r   r   get_window_extentk  s   
zOffsetBox.get_window_extentc           	      C   sz   |  |\}}| ||\}}t|  |D ]\}\}}||| || f || qt| |dtddd d| _dS )zm
        Update the location of children if necessary and draw them
        to the given *renderer*.
        F        padfillpropsN)	r   r$   zipr   rw   drawr*   dictrt   	r   r   r   rD   r   r   rh   oxoyr   r   r   r   v  s   
zOffsetBox.drawr   )__name__
__module____qualname____doc__r^   re   ri   rj   rk   setterro   rw   r'   r$   rz   r|   r   rf   r   r   r   r   __classcell__r   r   rc   r   r[      s$    

 

r[   c                       s"   e Zd Z		d fdd	Z  ZS )
PackerBaser   NrG   r+   c                    s8   t    || _|| _|| _|| _|| _|| _|| _dS )ao  
        Parameters
        ----------
        pad : float, default: 0.0
            The boundary padding in points.

        sep : float, default: 0.0
            The spacing between items in points.

        width, height : float, optional
            Width and height of the container box in pixels, calculated if
            *None*.

        align : {'top', 'bottom', 'left', 'right', 'center', 'baseline'}, default: 'baseline'
            Alignment of boxes.

        mode : {'fixed', 'expand', 'equal'}, default: 'fixed'
            The packing mode.

            - 'fixed' packs the given `.Artist`\s tight with *sep* spacing.
            - 'expand' uses the maximal available space to distribute the
              artists with equal spacing in between.
            - 'equal': Each artist an equal fraction of the available space
              and is left-aligned (or top-aligned) therein.

        children : list of `.Artist`
            The artists to pack.

        Notes
        -----
        *pad* and *sep* are in points and will be scaled with the renderer
        dpi, while *width* and *height* are in pixels.
        N)	r]   r^   r   r   r4   r   r.   rM   ra   )r   r   r4   r   r   rM   r.   childrenrc   r   r   r^     s   
$
zPackerBase.__init__)r   r   NNrG   r+   N)r   r   r   r^   r   r   r   rc   r   r     s    r   c                   @      e Zd ZdZdd ZdS )VPackerz
    VPacker packs its children vertically, automatically adjusting their
    relative positions at draw time.

    .. code-block:: none

       +---------+
       | Child 1 |
       | Child 2 |
       | Child 3 |
       +---------+
    c                    s     d}| j| }| j| }| jd ur+|  D ]}t|tr*|jdkr*|| j q fdd|  D }t	dd |D | j| j
\\}}}	tdd |D | j|| j\}
}|
|dd |D   }|d }|| }t|| || |
|g t|	|fS )	N      ?r,   c                       g | ]}|  qS r   r   r~   r   r   r   r5     rS   z7VPacker._get_bbox_and_child_offsets.<locals>.<listcomp>c                 S      g | ]}|j qS r   )	intervalxr1   r   r   r   r   r5         c                 S   r   r   rT   r   r   r   r   r5     r   c                 S   r   r   )rP   r   r   r   r   r5     r   r   )points_to_pixelsr   r4   r   r   
isinstancer   r.   rz   rZ   rM   rF   r   r	   r   paddedr   )r   r   dpicorr   r4   rh   bboxesry   x1xoffsetsr   yoffsetsr   r   r   r   r     s*   



z#VPacker._get_bbox_and_child_offsetsNr   r   r   r   r   r   r   r   r   r     s    r   c                   @   r   )HPackera  
    HPacker packs its children horizontally, automatically adjusting their
    relative positions at draw time.

    .. code-block:: none

       +-------------------------------+
       | Child 1    Child 2    Child 3 |
       +-------------------------------+
    c                    s     d}| j| }| j| } fdd|  D }|s)tdddd|g fS tdd |D | j| j	\\}}}t
dd |D | j|| j\}	}
|d j}|
dd |D | 8 }
t|||	|| |g t|
|fS )Nr   c                    r   r   r   r~   r   r   r   r5     rS   z7HPacker._get_bbox_and_child_offsets.<locals>.<listcomp>r   c                 S   r   r   )	intervalyr   r   r   r   r5     r   c                 S   r   r   )r   r   r   r   r   r5     r   c                 S   r   r   )ry   r   r   r   r   r5     r   )r   r   r4   r   r	   r   r   rZ   r   rM   rF   r   r.   ry   r   )r   r   r   r   r4   r   rO   rP   r   r   r   ry   r   r   r   r     s"   



z#HPacker._get_bbox_and_child_offsetsNr   r   r   r   r   r     s    r   c                       sL   e Zd ZdZdddd fddZdd	 Zd
d ZdddZdd Z  Z	S )	PaddedBoxa  
    A container to add a padding around an `.Artist`.

    The `.PaddedBox` contains a `.FancyBboxPatch` that is used to visualize
    it when rendering.

    .. code-block:: none

       +----------------------------+
       |                            |
       |                            |
       |                            |
       | <--pad--> Artist           |
       |             ^              |
       |            pad             |
       |             v              |
       +----------------------------+

    Attributes
    ----------
    pad : float
        The padding in points.
    patch : `.FancyBboxPatch`
        When *draw_frame* is True, this `.FancyBboxPatch` is made visible and
        creates a border around the box.
    r   FN)
draw_framepatch_attrsc                   sP   t    || _|g| _tddddddd|dd	| _|d	ur&| j| d	S d	S )
a  
        Parameters
        ----------
        child : `~matplotlib.artist.Artist`
            The contained `.Artist`.
        pad : float, default: 0.0
            The padding in points. This will be scaled with the renderer dpi.
            In contrast, *width* and *height* are in *pixels* and thus not
            scaled.
        draw_frame : bool
            Whether to draw the contained `.FancyBboxPatch`.
        patch_attrs : dict or None
            Additional parameters passed to the contained `.FancyBboxPatch`.
        r   r   r   r2   kr8   Tsquare,pad=0	rv   r   r   	facecolor	edgecolormutation_scalesnapvisibleboxstyleN)r]   r^   r   ra   r   patchupdate)r   childr   r   r   rc   r   r   r^     s   
zPaddedBox.__init__c                 C   s,   | j |d }| jd ||dgfS )Nr   r   r\   )r   r   ra   r   r   )r   r   r   r   r   r   r   6  s   z%PaddedBox._get_bbox_and_child_offsetsc           	      C   s|   |  |\}}| ||\}}t|  |D ]\}\}}||| || f q| | |  D ]}|| q1d| _d S NF)r   r$   r   r   rw   r   r   rt   r   r   r   r   r   ;  s   

zPaddedBox.drawc                 C   s(   | j |j |r| j | d| _d S NT)r   
set_boundsboundsset_mutation_scalert   r   r   fontsizer   r   r   update_frameI  s   
zPaddedBox.update_framec                 C   s    |  | | | j| d S r   )r   r   r   r   r   r   r   r   r   O  s   zPaddedBox.draw_frame)r   r   )
r   r   r   r   r^   r   r   r   r   r   r   r   rc   r   r     s    
r   c                       st   e Zd ZdZd fdd	Zedd Zejdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Z  ZS )DrawingAreaz
    The DrawingArea can contain any Artist as a child. The DrawingArea
    has a fixed width and height. The position of children relative to
    the parent is fixed. The children can be clipped at the
    boundaries of the parent.
    r   Fc                    s@   t    || _|| _|| _|| _|| _t | _	t | _
dS )a"  
        Parameters
        ----------
        width, height : float
            Width and height of the container box.
        xdescent, ydescent : float
            Descent of the box in x- and y-direction.
        clip : bool
            Whether to clip the children to the box.
        N)r]   r^   r   r   r   r   _clip_childrenmtransformsAffine2Doffset_transformdpi_transform)r   r   r   r   r   cliprc   r   r   r^   ]  s   

zDrawingArea.__init__c                 C   r   )zl
        If the children of this DrawingArea should be clipped
        by DrawingArea bounding box.
        )r   r   r   r   r   clip_childrenq  s   zDrawingArea.clip_childrenc                 C   s   t || _d| _d S r   )boolr   rt   )r   valr   r   r   r   y  s   

c                 C   s   | j | j S )zX
        Return the `~matplotlib.transforms.Transform` applied to the children.
        )r   r   r   r   r   r   get_transform~  s   zDrawingArea.get_transformc                 C      dS z+
        set_transform is ignored.
        Nr   r   tr   r   r   set_transform      zDrawingArea.set_transformc                 C   0   || _ | j  | j|d |d  d| _dS z
        Set the offset of the container.

        Parameters
        ----------
        xy : (float, float)
            The (x, y) coordinates of the offset in display units.
        r   r8   TNrb   r   clear	translatert   ru   r   r   r   rw        	

zDrawingArea.set_offsetc                 C   r   zReturn offset of the container.rb   r   r   r   r   r$     r   zDrawingArea.get_offsetc                 C   s6   | d}t| j | | j | | j| | j| S )Nr   )r   r	   r   r   r   r   r   )r   r   dpi_corr   r   r   r     s
   
zDrawingArea.get_bboxc                 C   sZ   | j | | s||   | jdur| j|_| jdd}|dur+|| dS dS )&Add an `.Artist` to the container box.NFr   )ra   appendis_transform_setr   r   rk   r   re   )r   rq   rg   r   r   r   
add_artist  s   
zDrawingArea.add_artistc              	   C   s   | d}| j  | j| ttddgd| jg| j	| jg| j	dgg| 
 }| jD ]}| jr>|js>|js>|| || q.t| |dtddd d| _d S )Nr   r   Fr   r   r   )r   r   r   scaler   TransformedPathmpathPathr   r   r   ra   r   clipbox	_clippathset_clip_pathr   r*   r   rt   )r   r   r   tpathrh   r   r   r   r     s    





zDrawingArea.draw)r   r   F)r   r   r   r   r^   propertyr   r   r   r   rw   r$   r   r   r   r   r   r   rc   r   r   U  s    

r   c                       sp   e Zd ZdZddd fdd
Zdd Zd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Z  ZS )TextAreaz
    The TextArea is a container artist for a single Text instance.

    The text is placed at (0, 0) with baseline+left alignment, by default. The
    width and height of the TextArea instance is the width and height of its
    child text.
    NF)	textpropsmultilinebaselinec                   sj   |du ri }t jdd|fi || _t   | jg| _t | _t | _	| j
| j| j	  || _dS )a  
        Parameters
        ----------
        s : str
            The text to be displayed.
        textprops : dict, default: {}
            Dictionary of keyword parameters to be passed to the `.Text`
            instance in the TextArea.
        multilinebaseline : bool, default: False
            Whether the baseline for multiline text is adjusted so that it
            is (approximately) center-aligned with single-line text.
        Nr   )mtextText_textr]   r^   ra   r   r   r   _baseline_transformr   _multilinebaseline)r   sr   r   rc   r   r   r^     s   





zTextArea.__init__c                 C   s   | j | d| _dS )z&Set the text of this area as a string.TN)r   set_textrt   r   r  r   r   r   r    s   
zTextArea.set_textc                 C   
   | j  S )z5Return the string representation of this area's text.)r   get_textr   r   r   r   r       
zTextArea.get_textc                 C   rs   )aX  
        Set multilinebaseline.

        If True, the baseline for multiline text is adjusted so that it is
        (approximately) center-aligned with single-line text.  This is used
        e.g. by the legend implementation so that single-line labels are
        baseline-aligned, but multiline labels are "center"-aligned with them.
        TN)r  rt   r   r   r   r   set_multilinebaseline  s   	
zTextArea.set_multilinebaselinec                 C   r   )z(
        Get multilinebaseline.
        )r  r   r   r   r   get_multilinebaseline  s   zTextArea.get_multilinebaselinec                 C   r   r   r   r   r   r   r   r     r   zTextArea.set_transformc                 C   r   r   r   ru   r   r   r   rw   
  r   zTextArea.set_offsetc                 C   r   r   r   r   r   r   r   r$     r   zTextArea.get_offsetc                 C   s   |j d| jj| j rdndd\}}}| j|\}}}|j\}}	| j  t|dkrF| j	rFd|	 d||   }
| j
d||
  |
}nt|| |	| }|| }	| j }d| d | d	| }t|| ||	S )
NlpTeXF)ismathr8   rV   r      )rH   rL   rJ   )get_text_width_height_descentr   _fontproperties
get_usetex_get_layoutsizer   r   r<   r  r   r>   get_horizontalalignmentr	   r   )r   r   _h_d_r   infoydr2   hyd_newh_dhary   r   r   r   r     s    


zTextArea.get_bboxc                 C   s,   | j | t| |dtddd d| _d S NFr   r   r   )r   r   r*   r   rt   r   r   r   r   r   3  s   
zTextArea.draw)r   r   r   r   r^   r  r  r  r	  r   rw   r$   r   r   r   r   r   rc   r   r     s    
r   c                       sX   e Zd ZdZ fddZdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Z  ZS )AuxTransformBoxaZ  
    Offset Box with the aux_transform. Its children will be
    transformed with the aux_transform first then will be
    offsetted. The absolute coordinate of the aux_transform is meaning
    as it will be automatically adjust so that the left-lower corner
    of the bounding box of children will be set to (0, 0) before the
    offset transform.

    It is similar to drawing area, except that the extent of the box
    is not predetermined but calculated from the window extent of its
    children. Furthermore, the extent of the children will be
    calculated in the transformed coordinate.
    c                    s(   || _ t   t | _t | _d S r   )aux_transformr]   r^   r   r   r   ref_offset_transform)r   r  rc   r   r   r^   H  s   

zAuxTransformBox.__init__c                 C   s$   | j | ||   d| _dS )r   TN)ra   r   r   r   rt   )r   rq   r   r   r   r   P  s   
zAuxTransformBox.add_artistc                 C   s   | j | j | j S )zf
        Return the :class:`~matplotlib.transforms.Transform` applied
        to the children
        )r  r   r   r   r   r   r   r   V  s
   zAuxTransformBox.get_transformc                 C   r   r   r   r   r   r   r   r   _  r   zAuxTransformBox.set_transformc                 C   r   r   r   ru   r   r   r   rw   d  r   zAuxTransformBox.set_offsetc                 C   r   r   r   r   r   r   r   r$   r  r   zAuxTransformBox.get_offsetc                    sr   | j  }| j  | j    fdd| jD }t|}| j|j |j	  | j 
| tdd|j|jS )Nc                    r   r   r   r~   r   r   r   r5   |  rS   z,AuxTransformBox.get_bbox.<locals>.<listcomp>r   )r   
get_matrixr   r   ra   r	   unionr   ry   rO   
set_matrixr   r   r   )r   r   _offr   ubr   r   r   r   v  s   



zAuxTransformBox.get_bboxc                 C   s6   | j D ]}|| qt| |dtddd d| _d S r  )ra   r   r*   r   rt   )r   r   rh   r   r   r   r     s   

zAuxTransformBox.draw)r   r   r   r   r^   r   r   r   rw   r$   r   r   r   r   r   rc   r   r  :  s    	r  c                       s   e Zd ZdZdZdddddddd	d
dd
Zdddddddd fdd
Zdd Zdd Zdd Z	dd Z
dd Zd&ddZed d! Zd&d"d#Zd$d% Z  ZS )'AnchoredOffsetboxa0  
    An offset box placed according to location *loc*.

    AnchoredOffsetbox has a single child.  When multiple children are needed,
    use an extra OffsetBox to enclose them.  By default, the offset box is
    anchored against its parent Axes. You may explicitly specify the
    *bbox_to_anchor*.
       r8   r                 	   
   )
zupper rightz
upper leftz
lower leftzlower rightrJ   zcenter leftzcenter rightzlower centerzupper centerrL   皙?rV   NT)r   	borderpadr   propframeonbbox_to_anchorbbox_transformc          
         s   t  jdi |	 | || | | t|tr!tj| j|d}|| _	|| _
|| _|du r8ttjd d| _nt|| _t|trPd|vrP| jtjd  tddddd	| j d
|dd	| _dS )a  
        Parameters
        ----------
        loc : str
            The box location.  Valid locations are
            'upper left', 'upper center', 'upper right',
            'center left', 'center', 'center right',
            'lower left', 'lower center', 'lower right'.
            For backward compatibility, numeric values are accepted as well.
            See the parameter *loc* of `.Legend` for details.
        pad : float, default: 0.4
            Padding around the child as fraction of the fontsize.
        borderpad : float, default: 0.5
            Padding between the offsetbox frame and the *bbox_to_anchor*.
        child : `.OffsetBox`
            The box that will be anchored.
        prop : `.FontProperties`
            This is only used as a reference for paddings. If not given,
            :rc:`legend.fontsize` is used.
        frameon : bool
            Whether to draw a frame around the box.
        bbox_to_anchor : `.BboxBase`, 2-tuple, or 4-tuple of floats
            Box that is used to position the legend in conjunction with *loc*.
        bbox_transform : None or :class:`matplotlib.transforms.Transform`
            The transform for the bounding box (*bbox_to_anchor*).
        **kwargs
            All other parameters are passed on to `.OffsetBox`.

        Notes
        -----
        See `.Legend` for a detailed description of the anchoring mechanism.
        )locNlegend.fontsizer  r  r   r   r2   r   Tr   r   r   )r]   r^   set_bbox_to_anchor	set_childr   strr   check_getitemcodesr6  r1  r   r   mplrcParamsr2  	_from_anyr   set_sizer   get_size_in_pointsr   )
r   r6  r   r1  r   r2  r3  r4  r5  r   rc   r   r   r^     s*   &

zAnchoredOffsetbox.__init__c                 C   s    || _ |dur| j|_d| _dS )zSet the child to be anchored.NT)_childrk   rt   )r   r   r   r   r   r:    s   
zAnchoredOffsetbox.set_childc                 C   r   )zReturn the child.rC  r   r   r   r   	get_child  r   zAnchoredOffsetbox.get_childc                 C      | j gS )zReturn the list of children.rD  r   r   r   r   rf     s   zAnchoredOffsetbox.get_childrenc                 C   s.   | | j }| j| }|  ||S r   )r   r2  rB  r   rE  r   r   )r   r   r   r   r   r   r   r     s   
zAnchoredOffsetbox.get_bboxc                 C   s2   | j du r	| jjS | j}|du r| j S t| j |S )z,Return the bbox that the box is anchored to.N)_bbox_to_anchorrk   r   _bbox_to_anchor_transformr   )r   	transformr   r   r   get_bbox_to_anchor  s   
z$AnchoredOffsetbox.get_bbox_to_anchorc              
   C   s   |du s	t |tr|| _n/zt|}W n ty' } ztd| |d}~ww |dkr6|d |d ddg}tj| | _|| _d| _	dS )aB  
        Set the bbox that the box is anchored to.

        *bbox* can be a Bbox instance, a list of [left, bottom, width,
        height], or a list of [left, bottom] where the width and
        height will be assumed to be zero. The bbox will be
        transformed to display coordinate by the given transform.
        NzInvalid bbox: r  r   r8   T)
r   r
   rG  r<   r   r?   r	   r   rH  rt   )r   r   rI  lerrr   r   r   r9    s   	
z$AnchoredOffsetbox.set_bbox_to_anchorc                 C   sV   | j || j  }|  }t| jtdd|j	|j
||\}}||j ||j fS )Nr   )r1  r   r2  rB  rJ  _get_anchored_bboxr6  r	   r   r   r   ry   rO   )r   r   r   r   r4  ry   rO   r   r   r   r$     s   zAnchoredOffsetbox.get_offsetc                 C   s&   | j |j |r| j | d S d S r   )r   r   r   r   r   r   r   r   r   *  s   zAnchoredOffsetbox.update_framec                 C   s~   |   sd S | |}|| j }| || | j| | | 	||\}}| 
 ||f | 
 | d| _d S r   )r}   r   r   r2  rB  r   r   r   r$   r   rE  rw   rt   )r   r   r   r   r   r   r   r   r   r   /  s   

zAnchoredOffsetbox.drawr   )r   r   r   r   zorderr=  r^   r:  rE  rf   r   rJ  r9  r'   r$   r   r   r   r   r   rc   r   r'    s:    B



r'  c                 C   s(   g d|  }| | }|j||djS )z}
    Return the (x, y) position of the *bbox* anchored at the *parentbbox* with
    the *loc* code with the *borderpad*.
    )NNENWSWSEEWrS  SNC)	container)r   anchoredp0)r6  r   
parentbboxr1  rh   rX  r   r   r   rM  @  s   rM  c                       s*   e Zd ZdZdddd fdd
Z  ZS )AnchoredTextz&
    AnchoredOffsetbox with Text.
    r0  rV   N)r   r1  r2  c          	         sf   |du ri }ddh}|t |@ rtdt||d| _| jj }t j|f||| j|d| dS )aq  
        Parameters
        ----------
        s : str
            Text.

        loc : str
            Location code. See `AnchoredOffsetbox`.

        pad : float, default: 0.4
            Padding around the text as fraction of the fontsize.

        borderpad : float, default: 0.5
            Spacing between the offsetbox frame and the *bbox_to_anchor*.

        prop : dict, optional
            Dictionary of keyword parameters to be passed to the
            `~matplotlib.text.Text` instance contained inside AnchoredText.

        **kwargs
            All other parameters are passed to `AnchoredOffsetbox`.
        Nvaverticalalignmentz<Mixing verticalalignment with AnchoredText is not supported.)r   )r   r1  r   r2  )setr?   r   txtr   get_fontpropertiesr]   r^   )	r   r  r6  r   r1  r2  r   	badkwargsfprc   r   r   r^   Q  s    

zAnchoredText.__init__)r   r   r   r   r^   r   r   r   rc   r   r\  L  s    r\  c                
       sr   e Zd Zdddddddddd	 fdd
Zd	d
 Zdd Zdd Zdd Zdd Zdd Z	dd Z
dd Z  ZS )OffsetImager8   NTg      @F)	zoomcmapnorminterpolationorigin
filternorm	filterradresampler   c       	            sV   t    |
| _td| j|||||||	d|| _| jg| _| | | | d S )N)r   rf  rg  rh  ri  rj  rk  rl  r   )	r]   r^   _dpi_corr   r   imagera   set_zoomset_data)r   arrre  rf  rg  rh  ri  rj  rk  rl  r   r   rc   r   r   r^   y  s    


zOffsetImage.__init__c                 C   s$   t || _| j| j d| _d S r   )r:   asarray_datarn  rp  rt   )r   rq  r   r   r   rp    s   
zOffsetImage.set_datac                 C   r   r   )rs  r   r   r   r   get_data     zOffsetImage.get_datac                 C      || _ d| _d S r   )_zoomrt   )r   re  r   r   r   ro    s   
zOffsetImage.set_zoomc                 C   r   r   )rw  r   r   r   r   get_zoom  ru  zOffsetImage.get_zoomc                 C   r   r   r   r   r   r   r   r$     r   zOffsetImage.get_offsetc                 C   rF  r   )rn  r   r   r   r   rf     s   zOffsetImage.get_childrenc           	      C   s`   | j r|dnd}|  }|  }|jd d \}}|| | || | }}tdd||S )Nr   r  r   )rm  r   rx  rt  shaper	   r   )	r   r   r   re  datanynxr2   r  r   r   r   r     s   zOffsetImage.get_bboxc                 C   s   | j | d| _d S r   )rn  r   rt   r   r   r   r   r     s   
zOffsetImage.draw)r   r   r   r^   rp  rt  ro  rx  r$   rf   r   r   r   r   r   rc   r   rd  w  s&     rd  c                
   @   s   e Zd ZdZdZdd Zejd%dddd	dddd
ddZe	dd Z
e
jdd Z
e	dd Zejdd Zdd Zdd Zdd Zd&ddZdd Zd&ddZd&dd Zd!d" Zd#d$ ZdS )'AnnotationBboxz
    Container for an `OffsetBox` referring to a specific position *xy*.

    Optionally an arrow pointing from the offsetbox to *xy* can be drawn.

    This is like `.Annotation`, but with `OffsetBox` instead of `.Text`.
    r)  c                 C   s"   d| j d dd| j d ddS )NzAnnotationBbox(r   g,r8   ))rv   r   r   r   r   __str__  s   "zAnnotationBbox.__str__Nrz  Tr0  rV   rV   )r3  r   annotation_clipbox_alignment	bboxprops
arrowpropsr   c             
   K   s   t j|  tjj| |||d || _|dur| nd| _| | |dur)|n|| _	|dur2|n|| _
|	| _|durN| jdd| _tdi | j| _nd| _d| _tdddd	d
| j d|d| _| jjd|d |
rw| jjdi |
 | | dS )a
  
        Parameters
        ----------
        offsetbox : `OffsetBox`

        xy : (float, float)
            The point *(x, y)* to annotate. The coordinate system is determined
            by *xycoords*.

        xybox : (float, float), default: *xy*
            The position *(x, y)* to place the text at. The coordinate system
            is determined by *boxcoords*.

        xycoords : single or two-tuple of str or `.Artist` or `.Transform` or callable, default: 'data'
            The coordinate system that *xy* is given in. See the parameter
            *xycoords* in `.Annotation` for a detailed description.

        boxcoords : single or two-tuple of str or `.Artist` or `.Transform` or callable, default: value of *xycoords*
            The coordinate system that *xybox* is given in. See the parameter
            *textcoords* in `.Annotation` for a detailed description.

        frameon : bool, default: True
            By default, the text is surrounded by a white `.FancyBboxPatch`
            (accessible as the ``patch`` attribute of the `.AnnotationBbox`).
            If *frameon* is set to False, this patch is made invisible.

        annotation_clip: bool or None, default: None
            Whether to clip (i.e. not draw) the annotation when the annotation
            point *xy* is outside the Axes area.

            - If *True*, the annotation will be clipped when *xy* is outside
              the Axes.
            - If *False*, the annotation will always be drawn.
            - If *None*, the annotation will be clipped when *xy* is outside
              the Axes and *xycoords* is 'data'.

        pad : float, default: 0.4
            Padding around the offsetbox.

        box_alignment : (float, float)
            A tuple of two floats for a vertical and horizontal alignment of
            the offset box w.r.t. the *boxcoords*.
            The lower-left corner is (0, 0) and upper-right corner is (1, 1).

        bboxprops : dict, optional
            A dictionary of properties to set for the annotation bounding box,
            for example *boxstyle* and *alpha*.  See `.FancyBboxPatch` for
            details.

        arrowprops: dict, optional
            Arrow properties, see `.Annotation` for description.

        fontsize: float or str, optional
            Translated to points and passed as *mutation_scale* into
            `.FancyBboxPatch` to scale attributes of the box style (e.g. pad
            or rounding_size).  The name is chosen in analogy to `.Text` where
            *fontsize* defines the mutation scale as well.  If not given,
            :rc:`legend.fontsize` is used.  See `.Text.set_fontsize` for valid
            values.

        **kwargs
            Other `AnnotationBbox` properties.  See `.AnnotationBbox.set` for
            a list.
        )xycoordsr  Nrelposr  r\   r8   r8   r   r   r2   r   T)rv   r   r   r   r   r   r   r   squarer   )r\   r  r   )ri   rj   r^   r   _AnnotationBase	offsetboxcopyr  set_fontsizexybox	boxcoords_box_alignmentpop_arrow_relposr   arrow_patchr   r2  rB  r   set_boxstyler_  r_   )r   r  rv   r  r  r  r3  r   r  r  r  r  r   r   r   r   r   r^     s8   L

zAnnotationBbox.__init__c                 C   r   r   )r  r   r   r   r   xyann8  r   zAnnotationBbox.xyannc                 C   rv  r   )r  rt   )r   r  r   r   r   r  <     
c                 C   r   r   )r  r   r   r   r   	anncoordsA  r   zAnnotationBbox.anncoordsc                 C   rv  r   )r  rt   )r   coordsr   r   r   r  E  r  c                 C   s0   |  |r	di fS | d sdi fS | j|S r   )rn   	_check_xyr  ro   )r   rp   r   r   r   ro   J  s
   

zAnnotationBbox.containsc                 C   s"   | j | jg}| jr|| j |S r   )r  r   r  r   )r   r   r   r   r   rf   R  s   zAnnotationBbox.get_childrenc                 C   s4   | j d ur| j | | j| tj| | d S r   )r  re   r  ri   rj   )r   rg   r   r   r   re   X  s   
zAnnotationBbox.set_figurec                 C   s(   |du r	t jd }t|d| _d| _dS )zk
        Set the fontsize in points.

        If *s* is not given, reset to :rc:`legend.fontsize`.
        Nr7  r8  T)r>  r?  r   r2  rt   r  r   r   r   r  ^  s   

zAnnotationBbox.set_fontsizec                 C   r  )zReturn the fontsize in points.)r2  rB  r   r   r   r   get_fontsizej  r  zAnnotationBbox.get_fontsizec                    >    d u r| j dd  |   t fdd|  D S )NTr   c                    r   r   r!  r1   r   r   r   r   r5   s      z4AnnotationBbox.get_window_extent.<locals>.<listcomp>r   r   update_positionsr	   r#  rf   r   r   r   r   r   n     
z AnnotationBbox.get_window_extentc                    r  )NTr   c                    r   r   )get_tightbboxr  r   r   r   r5   {  r  z0AnnotationBbox.get_tightbbox.<locals>.<listcomp>r  r   r   r   r   r  v  r  zAnnotationBbox.get_tightbboxc                 C   s
  |  || j| j\}}| j|}| j\}}| j|||j  |j |||j	  |j
 f | j|}| j|j ||  }| j| | jr|j|j| j  }| |}	| j||	 d| jv rm|| jd }| j| | jd| j}
| j|
 dS dS )zHUpdate pixel positions for the annotated point, the text, and the arrow.r   patchAN)_get_xyr  r  r  r   r  rw   r   ry   r   rO   r   r   r   r   r   r  r   r  rZ  r  r  _get_position_xyr  set_positionsget
set_patchA)r   r   ox0oy0r   fwfhr   arrow_begin	arrow_endr  r   r   r   r  ~  s,   
&

zAnnotationBbox.update_positionsc                 C   s   |   r	| |sd S |j| jj|  d | | | jd ur?| jjddd u r9| jdd }d ur9| j	| | j
| | j
| | j
| || jj d| _d S )N)gidFr   )r}   r  
open_grouprd   r   get_gidr  r  r   re   r   r   r  close_grouprt   )r   r   rg   r   r   r   r     s   


zAnnotationBbox.draw)Nrz  Nr   )r   r   r   r   rN  r  r   interpdr^   r   r  r   r  ro   rf   re   r  r  r   r  r  r   r   r   r   r   r}    s<    k






$r}  c                   @   sr   e Zd ZdZdddZedd Zedd Zdd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd ZdS )DraggableBasea  
    Helper base class for a draggable artist (legend, offsetbox).

    Derived classes must override the following methods::

        def save_offset(self):
            '''
            Called when the object is picked for dragging; should save the
            reference position of the artist.
            '''

        def update_offset(self, dx, dy):
            '''
            Called during the dragging; (*dx*, *dy*) is the pixel offset from
            the point where the mouse drag started.
            '''

    Optionally, you may override the following method::

        def finalize_offset(self):
            '''Called when the mouse is released.'''

    In the current implementation of `.DraggableLegend` and
    `DraggableAnnotation`, `update_offset` places the artists in display
    coordinates, and `finalize_offset` recalculates their position in axes
    coordinate and set a relevant attribute.
    Fc                    sd   || _ | s|d d| _|o| jj| _| jj  fddd| jfd| j	fd| j
ffD | _d S )NTFc              	      s&   g | ]\}}t  j ||qS r   )r%   partial
disconnect_connect_picklable)r1   namefunc	callbacksr   r   r5     s    z*DraggableBase.__init__.<locals>.<listcomp>
pick_eventbutton_release_eventmotion_notify_event)
ref_artistpickable
set_picker
got_artistcanvassupports_blit	_use_blitr  on_pick
on_release	on_motion_disconnectors)r   r  use_blitr   r  r   r^     s   

zDraggableBase.__init__c                 C   s   | j jddjS r   )r  r   r  r   r   r   r   r     s    zDraggableBase.<lambda>c                 C   s   dd | j d d D S )Nc                 S   s   g | ]}|j d  qS )r   )r   )r1   r  r   r   r   r5     s    
z*DraggableBase.<lambda>.<locals>.<listcomp>r  r  r   r   r   r   r     s    c                 C   s   |   r>| jr@|j| j }|j| j }| || | jr7| j	| j
 | j| jjdd  | j  d S | j  d S d S d S r   )_check_still_parentedr  xmouse_xymouse_yupdate_offsetr  r  restore_region
backgroundr  r   r   r   blit)r   evtdxdyr   r   r   r    s   zDraggableBase.on_motionc                 C   s   |   rJ|j| jkr|jj| _|jj| _|   d| _	| j	rL| j
rN| jd | j  | jjdd}| j|j| _| j|  | j  d S d S d S d S )NTFr   )r  artistr  rp   r  r  r  r  save_offsetr  r  set_animatedr  r   r   copy_from_bboxr   r  r   r  )r   r  rg   r   r   r   r    s   


zDraggableBase.on_pickc                 C   sj   |   r/| jr1|   d| _| jr3| j| j | j| jj	
  | j  | jd d S d S d S d S r   )r  r  finalize_offsetr  r  r  r  r  r   figurer   r  r  )r   eventr   r   r   r    s   
zDraggableBase.on_releasec                 C   s"   | j jddd u r|   dS dS )NFr   T)r  r   r  r   r   r   r   r    s   z#DraggableBase._check_still_parentedc                 C   s   | j D ]}|  qdS )zDisconnect the callbacks.Nr  )r   disconnectorr   r   r   r    s   
zDraggableBase.disconnectc                 C      d S r   r   r   r   r   r   r       zDraggableBase.save_offsetc                 C   r  r   r   )r   r  r  r   r   r   r    r  zDraggableBase.update_offsetc                 C   r  r   r   r   r   r   r   r    r  zDraggableBase.finalize_offsetNF)r   r   r   r   r^   r   r  cidsr  r  r  r  r  r  r  r  r   r   r   r   r    s    

r  c                       s6   e Zd Zd
 fdd	Zdd Zdd Zdd	 Z  ZS )DraggableOffsetBoxFc                    s   t  j||d || _d S N)r  )r]   r^   r  )r   r  r  r  rc   r   r   r^   #     
zDraggableOffsetBox.__init__c                 C   sD   | j }|jdd }||||}|\| _| _| j | d S r   )r  r   r   r$   r   offsetbox_xoffsetbox_yrw   )r   r  r   offsetr   r   r   r  '  s
   zDraggableOffsetBox.save_offsetc                 C   s$   | j | | j| f}| j| d S r   )r  r  r  rw   )r   r  r  loc_in_canvasr   r   r   r  .  s   z DraggableOffsetBox.update_offsetc                 C   sB   | j }|jdd }||}|j\}}||j ||j f}|S r   )r  r   r   r   rb   ry   rO   )r   r  r   r   r   r   r  r   r   r   get_loc_in_canvas2  s   

z$DraggableOffsetBox.get_loc_in_canvasr  )r   r   r   r^   r  r  r  r   r   r   rc   r   r  "  s
    r  c                       s.   e Zd Zd fdd	Zdd Zdd Z  ZS )	DraggableAnnotationFc                    s   t  j||d || _d S r  )r]   r^   
annotation)r   r  r  rc   r   r   r^   <  r  zDraggableAnnotation.__init__c                 C   s"   | j }| |j\| _| _d S r   )r  r   rI  r  r   r   )r   annr   r   r   r  @  s   zDraggableAnnotation.save_offsetc                 C   s.   | j }|  | j| | j| f|_d S r   )r  r   invertedrI  r   r   r  )r   r  r  r  r   r   r   r  D  s   
z!DraggableAnnotation.update_offsetr  )r   r   r   r^   r  r  r   r   r   rc   r   r  ;  s    r  )r+   )rG   )7r   r%   numpyr:   
matplotlibr>  r   r   matplotlib.artistr  ri   matplotlib.pathpathr   matplotlib.texttextr   matplotlib.transforms
transformsr   matplotlib.font_managerr   matplotlib.imager   matplotlib.patchesr   r   r   r)   r	   r
   r   r(   r'   r*   rF   rZ   rj   r[   r   r   r   r   r   r   r  r'  rM  r\  rd  r  r}  r  r  r  r   r   r   r   <module>   sJ    

Q3 ;/("XpuR 5+G vo