o
    îÇh«z  ã                   @   sü   d 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 e e¡Z		d'dd„Zd(dd	„Zd
d„ Zdd„ Zdd„ Zdddddœdd„Zdddddœdd„Zdddœdd„Zdd„ Zdd„ Zdd„ Zdddddœdd„Zdd œd!d"„Zd#d$„ Zd%d&„ ZdS ))a	  
Adjust subplot layouts so that there are no overlapping Axes or Axes
decorations.  All Axes decorations are dealt with (labels, ticks, titles,
ticklabels) and some dependent artists are also dealt with (colorbar,
suptitle).

Layout is done via `~matplotlib.gridspec`, with one constraint per gridspec,
so it is possible to have overlapping Axes if the gridspecs overlap (i.e.
using `~matplotlib.gridspec.GridSpecFromSubplotSpec`).  Axes placed using
``figure.subplots()`` or ``figure.add_subplots()`` will participate in the
layout.  Axes manually placed via ``figure.add_axes()`` will not.

See Tutorial: :ref:`constrainedlayout_guide`

General idea:
-------------

First, a figure has a gridspec that divides the figure into nrows and ncols,
with heights and widths set by ``height_ratios`` and ``width_ratios``,
often just set to 1 for an equal grid.

Subplotspecs that are derived from this gridspec can contain either a
``SubPanel``, a ``GridSpecFromSubplotSpec``, or an ``Axes``.  The ``SubPanel``
and ``GridSpecFromSubplotSpec`` are dealt with recursively and each contain an
analogous layout.

Each ``GridSpec`` has a ``_layoutgrid`` attached to it.  The ``_layoutgrid``
has the same logical layout as the ``GridSpec``.   Each row of the grid spec
has a top and bottom "margin" and each column has a left and right "margin".
The "inner" height of each row is constrained to be the same (or as modified
by ``height_ratio``), and the "inner" width of each column is
constrained to be the same (as modified by ``width_ratio``), where "inner"
is the width or height of each column/row minus the size of the margins.

Then the size of the margins for each row and column are determined as the
max width of the decorators on each Axes that has decorators in that margin.
For instance, a normal Axes would have a left margin that includes the
left ticklabels, and the ylabel if it exists.  The right margin may include a
colorbar, the bottom margin the xaxis decorations, and the top margin the
title.

With these constraints, the solver then finds appropriate bounds for the
columns and rows.  It's possible that the margins take up the whole figure,
in which case the algorithm is not applied and a warning is raised.

See the tutorial :ref:`constrainedlayout_guide`
for more discussion of the algorithm with examples.
é    N)Ú_apiÚartist©r   r   é   r   Fc              
   C   sJ  |   ¡ }t| d|d}|d st d¡ dS tdƒD ]ˆ}	t|| |||||d t|| |||d t|| ƒ ||   ¡  d}
t	|| ƒr˜t
|| |||||d |r—t|| ƒ}||   ¡  t	|| ƒrkt
|| |||||d nt |
¡ | j }dur—| ¡ r—|jr—| ¡ \}}	| |||   ¡ j| f¡ | d	¡ nt |
¡ t|| ƒ q|S )
a3  
    Do the constrained_layout.  Called at draw time in
     ``figure.constrained_layout()``

    Parameters
    ----------
    fig : `~matplotlib.figure.Figure`
        `.Figure` instance to do the layout in.

    h_pad, w_pad : float
      Padding around the Axes elements in figure-normalized units.

    hspace, wspace : float
       Fraction of the figure to dedicate to space between the
       Axes.  These are evenly spread between the gaps between the Axes.
       A value of 0.2 for a three-column layout would have a space
       of 0.1 of the figure width between each column.
       If h/wspace < h/w_pad, then the pads are used instead.

    rect : tuple of 4 floats
        Rectangle in figure coordinates to perform constrained layout in
        [left, bottom, width, height], each from 0-1.

    compress : bool
        Whether to shift Axes so that white space in between them is
        removed. This is useful for simple grids of fixed-aspect Axes (e.g.
        a grid of images).

    Returns
    -------
    layoutgrid : private debugging structure
    N)ÚrectÚhasgridszhThere are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keywordé   )Úh_padÚw_padÚhspaceÚwspace)r	   r
   z{constrained_layout not applied because axes sizes collapsed to zero.  Try making figure larger or Axes decorations smaller.Úbottom)Ú_get_rendererÚmake_layoutgridsr   Úwarn_externalÚrangeÚmake_layout_marginsÚmake_margin_suptitlesÚmatch_submerged_marginsÚupdate_variablesÚcheck_no_collapsed_axesÚreposition_axesÚcompress_fixed_aspectÚ	_suptitleÚget_in_layoutÚ_autoposÚget_positionÚset_positionÚget_inner_bboxÚy1Úset_verticalalignmentÚreset_margins)Úfigr	   r
   r   r   r   ÚcompressÚrendererÚlayoutgridsÚ_Úwarn_collapsedÚsuptitleÚx© r*   úr/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/matplotlib/_constrained_layout.pyÚdo_constrained_layout?   sR   $

ÿ
ÿ


ÿ


ÿ
ÿÿÿ
€
r,   c                 C   s¶   |du rt ƒ }d|d< t| dƒstj|dd|| < n!| j ¡ }t||ƒ}|| }tj|ddd	d	| jj| jjfd
|| < | j	D ]}t
||ƒ}q>| jD ]}| ¡ }|durXt||ƒ}qI|S )z½
    Make the layoutgrid tree.

    (Sub)Figures get a layoutgrid so we can have figure margins.

    Gridspecs that are attached to Axes get a layoutgrid so Axes
    can have margins.
    NFr   Ú_parentÚfiglb)ÚparentÚnameÚpanellbTr   )r/   r0   Úparent_innerÚnrowsÚncolsÚ
parent_pos)ÚdictÚhasattrÚmlayoutgridÚ
LayoutGridÚ_subplotspecÚget_gridspecÚmake_layoutgrids_gsÚrowspanÚcolspanÚsubfigsr   Ú
_localaxes)r"   r%   r   ÚgsÚparentlbÚsfigÚaxr*   r*   r+   r   œ   s2   



ÿ
û


€r   c              	   C   sä   || v s	|j du r| S d| d< t|dƒs/| |j  }tj|dd|j|j| ¡ | ¡ d| |< | S |j}| 	¡ }|| vr?t
| |ƒ} | | }|df}|| vr[tj|ddd|j|jfd	| |< tj| | d|j|j| ¡ | ¡ d
| |< | S )zR
    Make the layoutgrid for a gridspec (and anything nested in the gridspec)
    NTr   Ú_subplot_specÚgridspec)r/   r2   r0   r4   r3   Úwidth_ratiosÚheight_ratiosÚtopr   )r/   r0   r3   r4   r5   )r/   r0   r3   r4   rG   rH   )Úfigurer7   r8   r9   Ú_ncolsÚ_nrowsÚget_width_ratiosÚget_height_ratiosrE   r;   r<   r=   r>   )r%   rA   r/   Úsubplot_specÚparentgsÚ	subspeclbÚrepr*   r*   r+   r<   É   sF   


úë


ü
ûr<   c           
      C   s’   |j D ]}t| |ƒ}|s dS q|jD ]3}| ¡ }|| v rF| | }t|jƒD ]}t|jƒD ]}| ||¡}	|	jdks?|	j	dkrD   dS q-q&qdS )z9
    Check that no Axes have collapsed to zero size.
    Fr   T)
r?   r   Úaxesr;   r   r3   r4   r   ÚwidthÚheight)
r%   r"   rC   ÚokrD   rA   ÚlgÚiÚjÚbbr*   r*   r+   r   ÷   s$   

ÿ

ÿþ€r   c                 C   sL  d }|j D ]h}| ¡ d u rq| ¡  | ¡ }| ¡ }|d u r-|}t |j¡}t |j¡}n||kr5tdƒ‚|j	dd}|j	dd}	|j
|	j
 }
|
dkrWt ||j |
¡||j< |j|	j }|dkrmt ||j |¡||j< q|d u rvtdƒ‚t |¡d }| |  d|¡ | |  d	|¡ t |¡d }| |  d
|¡ | |  d|¡ | S )NzECannot do compressed layout if Axes are notall from the same gridspecT©ÚoriginalFr   z>Cannot do compressed layout if no Axes are part of a gridspec.r   ÚleftÚrightrI   r   )rS   Úget_subplotspecÚapply_aspectr;   ÚnpÚzerosr4   r3   Ú
ValueErrorr   rT   Úmaximumr>   rU   r=   ÚsumÚedit_margin_min)r%   r"   rA   rD   ÚsubÚ_gsÚextrawÚextrahÚorigÚactualÚdwÚdhÚwÚhr*   r*   r+   r     s>   
€r   ©r
   r	   r   r   c             	   C   s  | j }| ¡ }t|dƒr!|jd ur|jn|}|jd ur|jn|}n|jd ur)|jn|}|jd ur3|jn|}|d }|d }| ¡ \}	}
||||dddddœ}||
 |krl|jj	dkr`||
 |d< |jj
|
k rl||
 |d< ||	 |krŠ|jj
|	k r~||	 |d< |jj	dkrŠ||	 |d< |S )	Nr   r   r   )ÚleftcbÚrightcbÚbottomcbÚtopcbr]   r^   rI   r   rr   rs   rt   ru   )r:   r;   r7   r   r   Ú_hspaceÚ_wspaceÚget_geometryr>   ÚstartÚstopr=   )Úobjr
   r	   r   r   ÚssrA   rv   rw   r3   r4   Úmarginr*   r*   r+   Úget_margin_from_padding0  s4   
ýr~   c             
   C   sÒ  |j D ]%}|j}| ¡ }	t| ||||||d t|dd||d}
| |	  |
|¡ q|jD ]L}| ¡ r7| ¡ s8q,| ¡ }| ¡ }	|	| vrG dS t|||||d}t	||ƒ\}}|d  |j
|j
 7  < |d  |j|j 7  < |d  |j|j 7  < |d  |j|j 7  < |jD ]æ}t| |ƒ}t|ƒ\}}|jd }t	||ƒ\}}|dkrº|j|jjkr¹|d	  |j| 7  < n@|dkrÑ|j|jjkrÐ|d
  |j| 7  < n)|dkrè|j|jjkrç|d  |j| 7  < n|j|jjkrú|d  |j| 7  < |dv r5|j|jjkr|j
|j
k r|d  |j
|j
 7  < |j|jjkr5|j|jkr5|d  |j|j 7  < |dv rp|j|jjkrU|j|jk rU|d  |j|j 7  < |j|jjkrp|j|jkrp|d  |j|j 7  < qŠ| |	  ||¡ q,|jD ]i}d}|jrå|jdu rå|du r–|j ¡ j}|| |¡ƒ}|jd|  }|jd|  }|j}|dkr¼| |  d|¡ n|dkrÉ| |  d|¡ |dkrØ| |  d|¡ q}|dkrå| |  d|¡ q}dS )a¥  
    For each Axes, make a margin between the *pos* layoutbox and the
    *axes* layoutbox be a minimum size that can accommodate the
    decorations on the axis.

    Then make room for colorbars.

    Parameters
    ----------
    layoutgrids : dict
    fig : `~matplotlib.figure.Figure`
        `.Figure` instance to do the layout in.
    renderer : `~matplotlib.backend_bases.RendererBase` subclass.
        The renderer to use.
    w_pad, h_pad : float, default: 0
        Width and height padding (in fraction of figure).
    hspace, wspace : float, default: 0
        Width and height padding as fraction of figure size divided by
        number of columns or rows.
    rq   r   Nr]   r^   r   rI   Úlocationrs   rr   ru   rt   )rI   r   ©r]   r^   r   ÚlowerÚupper) r?   r:   r;   r   r~   Úedit_outer_margin_minsr@   r_   r   Úget_pos_and_bboxÚx0Úx1Úy0r   Ú
_colorbarsÚcolorbar_get_padÚget_cb_parent_spansÚ_colorbar_inforz   r>   rT   ry   r=   rU   ÚlegendsÚ_outside_locÚ_bbox_to_anchorÚtransFigureÚinvertedÚtransform_bboxÚget_tightbboxrf   )r%   r"   r$   r
   r	   r   r   rC   r|   rA   ÚmarginsrD   r}   ÚposÚbboxÚcbaxÚpadÚ	cbp_rspanÚ	cbp_cspanÚlocÚcbposÚcbbboxÚlegÚinv_trans_figro   rp   Ú	legendlocr*   r*   r+   r   V  s    
þÿÿ


€€€

€





€ðr   ©r
   r	   c                C   s°  |j  ¡ j}t ddg||gg¡}|j |j  |¡}|j}|j}|jD ]}	t	| |	|||d q#|j
d urf|j
 ¡ rf|j
 ¡ }
t|j
ddƒrf|j
 |
d d| f¡ ||j
 |¡ƒ}| |  d|jd|  ¡ |jd ur›|j ¡ r›|j ¡ }
t|jddƒr›|j |
d |f¡ ||j |¡ƒ}| |  d|jd|  ¡ |jd urÒ|j ¡ rÔ|j ¡ }
t|jddƒrÖ|j ||
d f¡ ||j |¡ƒ}| |  d	|jd|  ¡ d S d S d S d S )
Nr   r    r   Fr   rI   r   r   r]   )r   r   r‘   ÚmtransformsÚBboxÚtransSubfigurerU   rT   r?   r   r   r   r   Úgetattrr   r’   rf   Ú
_supxlabelÚ
_supylabel)r%   r"   r$   r
   r	   rž   ÚpadboxÚh_pad_localÚw_pad_localrC   Úpr•   r*   r*   r+   r   Ì  sF   ÿÿ
ÿ


ÿ
ûr   c                 C   s`  |j D ]}t| |ƒ qdd„ | ¡ D ƒ}|D ]–}| ¡ }| ¡ | vr)| |¡ q| | ¡  }t|jƒdkrìt 	|j
d |jdd…  |j
d |jdd…   ¡}t 	|j
d |jdd…  |j
d	 |jdd…   ¡}|D ]Y}	|	 ¡ }
| |
 ¡  }|durÇt|
jƒdkrÇt 	|j
d |
jdd…  |j
d |
jdd…   ¡}||kr¦|}t 	|j
d |
jdd…  |j
d	 |
jdd…   ¡}||krÇ|}qn|jdd… D ]
}|jd||d
 qÏ|jdd… D ]
}|jd||d
 qát|jƒdkr­t 	|j
d |jdd…  |j
d |jdd…   ¡}t 	|j
d |jdd…  |j
d |jdd…   ¡}|D ]Z}	|	 ¡ }
| |
 ¡  }|dur…t|
jƒdkr…t 	t 	|j
d |
jdd…  |j
d |
jdd…   ¡|g¡}t 	t 	|j
d |
jdd…  |j
d |
jdd…   ¡|g¡}q,|jdd… D ]}|jd||d
 qŽ|jdd… D ]}|jd||d
 q¡qdS )aç  
    Make the margins that are submerged inside an Axes the same size.

    This allows Axes that span two columns (or rows) that are offset
    from one another to have the same size.

    This gives the proper layout for something like::
        fig = plt.figure(constrained_layout=True)
        axs = fig.subplot_mosaic("AAAB
CCDD")

    Without this routine, the Axes D will be wider than C, because the
    margin width between the two columns in C has no width by default,
    whereas the margins between the two columns of D are set by the
    width of the margin between A and B. However, obviously the user would
    like C and D to be the same size, so we need to add constraints to these
    "submerged" margins.

    This routine makes all the interior margins the same, and the spacing
    between the three columns in A and the two column in C are all set to the
    margins between the two columns of D.

    See test_constrained_layout::test_constrained_layout12 for an example.
    c                 S   s$   g | ]}|  ¡ d ur| ¡ r|‘qS )N)r_   r   )Ú.0Úar*   r*   r+   Ú
<listcomp>  s    ÿz+match_submerged_margins.<locals>.<listcomp>r   r]   Nrr   r^   éÿÿÿÿrs   )ÚcellrI   ru   r   rt   )r?   r   Úget_axesr_   r;   ÚremoveÚlenr>   ra   ÚmaxÚmargin_valsrf   r=   )r%   r"   rC   ÚaxsÚax1Úss1Úlg1ÚmaxsublÚmaxsubrÚax2Úss2Úlg2Úmaxsubl2Úmaxsubr2rX   ÚmaxsubtÚmaxsubbr*   r*   r+   r   ó  s    


ÿÿÿÿÿÿÿÿ€ÿÿÿÿ
ÿÿýÿÿý€€Ár   c           	      C   sˆ   t j}t j }t j}t j }| jd D ]"}| ¡ }t|jj|ƒ}t|jj|ƒ}t|j	j|ƒ}t|j	j|ƒ}qt
||ƒ}t
||ƒ}||fS )z¢
    Figure out which subplotspecs this colorbar belongs to.

    Parameters
    ----------
    cbax : `~matplotlib.axes.Axes`
        Axes for the colorbar.
    Úparents)ra   Úinfr‹   r_   Úminr=   ry   r³   rz   r>   r   )	r–   ÚrowstartÚrowstopÚcolstartÚcolstopr/   r|   r=   r>   r*   r*   r+   rŠ   T  s   	

rŠ   c                 C   sb   | j dd}| jdd}| |j|j ¡}t | |¡}|du r%|}||fS | |j ¡ ¡}||fS )a{  
    Get the position and the bbox for the Axes.

    Parameters
    ----------
    ax : `~matplotlib.axes.Axes`
    renderer : `~matplotlib.backend_bases.RendererBase` subclass.

    Returns
    -------
    pos : `~matplotlib.transforms.Bbox`
        Position in figure coordinates.
    bbox : `~matplotlib.transforms.Bbox`
        Tight bounding box in figure coordinates.
    F©ÚrootTr[   N)Ú
get_figurer   Útransformedr£   r   ÚmartistÚ_get_tightbbox_for_layout_onlyr   )rD   r$   r"   r”   Ú	tightbboxr•   r*   r*   r+   r„   m  s   ÿr„   c             
   C   s  |j |j }|jD ]}| |  ¡ }	|j|	 |¡d t| ||||||d q	|jD ]X}
|
 ¡ du s5|
 	¡ s6q)|
 ¡ }| 
¡ }|| vrE dS | | j|j|jd}	| |	¡}|
 |¡ dddddœ}t|
jddd… ƒD ]\}}|
|jd d kr€t| |||d	 qkq)dS )
zF
    Reposition all the Axes based on the new inner bounding box.
    )r•   )r
   r	   r   r   N©ÚrowsÚcolsr   )r]   r^   r   rI   r®   rÂ   ©Úoffset)r   r£   r?   Úget_outer_bboxÚ_redo_transform_rel_figrÌ   r   r@   r_   r   r;   r   r=   r>   r‘   Ú_set_positionÚ	enumeraterˆ   r‹   Úreposition_colorbar)r%   r"   r$   r
   r	   r   r   Útrans_fig_to_subfigrC   r•   rD   r|   rA   ÚnewbboxrÔ   Únnr–   r*   r*   r+   r   ‰  s>   
ÿþ
ÿ

ÿ€þêr   rÓ   c                C   sŠ  |j d }|d  ¡ }|jdd}|j|j }t|ƒ\}}	| | j||	d}
| | j||	d}|j d }|j d }|j d }|j d	 }|j d
 }t||ƒ\}}t	| |ƒ}|dv rº| 
||¡ ||¡}|dkrŽ|j|j }|
j|j |d  }||| 7 }|d  |j| 7  < | |d¡}n|j|j }|
j|j }||j | | |d  7 }|d  |j| 7  < | |d¡}na| 
||¡ ||¡}|dkrð|j|j }|
j|j |d  }||| 7 }|d  |j| 7  < | d|¡}n+|j|j }|
j|j }||j | | |d  7 }|d  |j| 7  < | d|¡}| |¡}| |j¡ | |¡ | |¡ |dv r9d| }| |¡ | d¡ |S )aƒ  
    Place the colorbar in its new place.

    Parameters
    ----------
    layoutgrids : dict
    cbax : `~matplotlib.axes.Axes`
        Axes for the colorbar.
    renderer : `~matplotlib.backend_bases.RendererBase` subclass.
        The renderer to use.
    offset : array-like
        Offset the colorbar needs to be pushed to in order to
        account for multiple colorbars.
    rÂ   r   FrÉ   rÐ   r   ÚanchorÚfractionÚaspectÚshrinkr€   r^   r]   rI   r   )r   rI   r   Úauto)r‹   r;   rË   r   r£   rŠ   Úget_bbox_for_cbr   r„   r‰   ÚshrunkÚanchoredr…   r†   rT   Ú
translatedr‡   r   rU   r‘   Úset_transformr×   Ú
set_anchorÚset_box_aspectÚ
set_aspect)r%   r–   r$   rÔ   rÂ   rA   r"   rÚ   Ú	cb_rspansÚ	cb_cspansÚ
bboxparentÚpbr   rÝ   rÞ   rß   rà   r›   rœ   ÚcbpadÚpbcbÚlmarginÚdxÚbmarginÚdyr*   r*   r+   rÙ   ³  sd   

ÿ











rÙ   c                 C   sV   |j D ]}t| |ƒ q|jD ]}| ¡ r"| ¡ }|| v r"| |  ¡  q| |  ¡  dS )zÁ
    Reset the margins in the layoutboxes of *fig*.

    Margins are usually set as a minimum, so if the figure gets smaller
    the minimum needs to be zero in order for it to grow again.
    N)r?   r!   rS   r   r;   )r%   r"   rC   rD   rA   r*   r*   r+   r!     s   

€r!   c                 C   s^   |j d }|d  ¡ }t|ƒ\}}| | j||d}|j d dv r%|j}n|j}|j d | S )NrÂ   r   rÐ   r   )r^   r]   r—   )r‹   r;   rŠ   r   rT   rU   )r%   ÚcaxrÂ   rA   rê   rë   Ú	bboxouterÚsizer*   r*   r+   r‰     s   
r‰   )NNr   F)r   ) Ú__doc__ÚloggingÚnumpyra   Ú
matplotlibr   r   rÍ   Úmatplotlib.transformsÚ
transformsr¡   Úmatplotlib._layoutgridÚ_layoutgridr8   Ú	getLoggerÚ__name__Ú_logr,   r   r<   r   r   r~   r   r   r   rŠ   r„   r   rÙ   r!   r‰   r*   r*   r*   r+   Ú<module>   s:    1

þ
]-.%ÿ&ÿv'aÿ*Q