o
    l’×iw  ã                   @   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 ))aB	  
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: :doc:`/tutorials/intermediate/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 doc:`/tutorials/intermediate/constrainedlayout_guide`
for more discussion of the algorithm with examples.
é    N)Ú_apiÚartist©r   r   é   r   Fc              
   C   sü   |   ¡ }t| d|d}|d st d¡ dS tdƒD ]a}	t|| |||||d t|| |||d t|| ƒ ||   ¡  d}
t	|| ƒrqt
|| |||||d |rpt|| ƒ}||   ¡  t	|| ƒrkt
|| |||||d nt |
¡ nt |
¡ t|| ƒ q|S )	aQ  
    Do the constrained_layout.  Called at draw time in
     ``figure.constrained_layout()``

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

    renderer : Renderer
        Renderer to use.

    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.)Ú_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Úreset_margins)Úfigr	   r
   r   r   r   ÚcompressÚrendererÚlayoutgridsÚ_Úwarn_collapsed© r   ú\/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/matplotlib/_constrained_layout.pyÚdo_constrained_layout?   s>   '

ÿ
ÿ


ÿ


ÿ
€
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$   r%   Ú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$   r'   r%   r)   r(   Úwidth_ratiosÚheight_ratiosÚtopr   )r$   r%   r(   r)   r*   )r$   r%   r(   r)   r<   r=   )Úfigurer,   r-   r.   Ú_ncolsÚ_nrowsÚget_width_ratiosÚget_height_ratiosr:   r0   r1   r2   r3   )r   r6   r$   Úsubplot_specÚparentgsÚ	subspeclbÚrepr   r   r    r1   Å   sF   


úë


ü
ûr1   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)
r4   r   Úaxesr0   r   r(   r)   Úget_inner_bboxÚwidthÚheight)
r   r   r8   Úokr9   r6   Ú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Úrightr>   Úbottom)rH   Úget_subplotspecÚapply_aspectr0   ÚnpÚzerosr)   r(   Ú
ValueErrorÚget_positionrJ   Úmaximumr3   rK   r2   ÚsumÚedit_margin_min)r   r   r6   r9   Ú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ÚtopcbrS   rT   r>   rU   rj   rk   rl   rm   )r/   r0   r,   r   r   Ú_hspaceÚ_wspaceÚget_geometryr3   ÚstartÚstopr2   )Úobjr
   r	   r   r   Ússr6   rn   ro   r(   r)   Úmarginr   r   r    Úget_margin_from_padding,  s4   
ýrv   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 )zÊ
    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.
    ri   r   NrS   rT   rU   r>   Úlocationrk   rj   rm   rl   )r>   rU   ©rS   rT   r   ÚlowerÚupper) r4   r/   r0   r   rv   Úedit_outer_margin_minsr5   rV   Úget_in_layoutÚget_pos_and_bboxÚx0Úx1Úy0Úy1Ú
_colorbarsÚcolorbar_get_padÚget_cb_parent_spansÚ_colorbar_inforr   r3   rJ   rq   r2   rK   ÚlegendsÚ_outside_locÚ_bbox_to_anchorÚtransFigureÚinvertedÚtransform_bboxÚget_tightbboxr^   )r   r   r   r
   r	   r   r   r8   rt   r6   Úmarginsr9   ru   ÚposÚbboxÚcbaxÚpadÚ	cbp_rspanÚ	cbp_cspanÚlocÚcbposÚcbbboxÚlegÚinv_trans_figrg   rh   Ú	legendlocr   r   r    r   R  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š   Ú_autoposFr   r>   r   rU   rS   )r‰   rŠ   r‹   ÚmtransformsÚBboxÚtransSubfigurerK   rJ   r4   r   Ú	_suptitler|   r[   ÚgetattrÚset_positionrŒ   r^   Ú
_supxlabelÚ
_supylabel)r   r   r   r
   r	   r˜   ÚpadboxÚh_pad_localÚw_pad_localr8   Ú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)rV   r|   )Ú.0Úar   r   r    Ú
<listcomp>þ  s    ÿz+match_submerged_margins.<locals>.<listcomp>r   rS   Nrj   rT   éÿÿÿÿrk   )Úcellr>   rm   rU   rl   )r4   r   Úget_axesrV   r0   ÚremoveÚlenr3   rX   ÚmaxÚmargin_valsr^   r2   )r   r   r8   ÚaxsÚax1Úss1Úlg1ÚmaxsublÚmaxsubrÚax2Úss2Úlg2Úmaxsubl2Úmaxsubr2rN   Ú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 )zA
    Figure out which subplotspecs this colorbar belongs to:
    Úparents)rX   Úinfr…   rV   Úminr2   rq   r°   rr   r3   r   )	r   ÚrowstartÚrowstopÚcolstartÚcolstopr$   rt   r2   r3   r   r   r    r„   C  s   

r„   c                 C   s\   | j }| jdd}| |j|j ¡}t | |¡}|du r"|}||fS | |j ¡ ¡}||fS )zú
    Get the position and the bbox for the axes.

    Parameters
    ----------
    ax
    renderer

    Returns
    -------
    pos : Bbox
        Position in figure coordinates.
    bbox : Bbox
        Tight bounding box in figure coordinates.
    TrQ   N)r?   r[   Útransformedrž   r‰   ÚmartistÚ_get_tightbbox_for_layout_onlyrŠ   )r9   r   r   rŽ   Ú	tightbboxr   r   r   r    r}   W  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   )rS   rT   rU   r>   r«   r¿   ©Úoffset)r‰   rž   r4   Úget_outer_bboxÚ_redo_transform_rel_figrÆ   r   r5   rV   r|   r0   rI   r2   r3   r‹   Ú_set_positionÚ	enumerater‚   r…   Úreposition_colorbar)r   r   r   r
   r	   r   r   Útrans_fig_to_subfigr8   r   r9   rt   r6   ÚnewbboxrÎ   Únnr   r   r   r    r   s  s>   
ÿþ
ÿ

ÿ€þêr   rÍ   c                C   s„  |j d }|d  ¡ }|j}|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 r6d| }| |¡ | d¡ |S )aß  
    Place the colorbar in its new place.

    Parameters
    ----------
    cbax : Axes
        Axes for the colorbar

    renderer :
    w_pad, h_pad : float
        width and height padding (in fraction of figure)
    hspace, wspace : float
        width and height padding as fraction of figure size divided by
        number of  columns or rows
    margin : array-like
        offset the colorbar needs to be pushed to in order to
        account for multiple colorbars
    r¿   r   rÊ   rw   ÚanchorÚfractionÚaspectÚshrinkrx   rT   rS   r>   rU   )rU   r>   r   Úauto)r…   r0   r?   r‰   rž   r„   Úget_bbox_for_cbrI   r}   rƒ   ÚshrunkÚanchoredr~   r   rJ   Ú
translatedr€   r   rK   r‹   Úset_transformrÑ   Ú
set_anchorÚset_box_aspectÚ
set_aspect)r   r   r   rÎ   r¿   r6   r   rÔ   Ú	cb_rspansÚ	cb_cspansÚ
bboxparentÚpbrw   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)r4   r   rH   r|   r0   )r   r   r8   r9   r6   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Ê   rw   )rT   rS   r‘   )r…   r0   r„   rI   rJ   rK   )r   Úcaxr¿   r6   rä   rå   Ú	bboxouterÚsizer   r   r    rƒ     s   
rƒ   )NNr   F)r   ) Ú__doc__ÚloggingÚnumpyrX   Ú
matplotlibr   r   rÇ   Úmatplotlib.transformsÚ
transformsrœ   Úmatplotlib._layoutgridÚ_layoutgridr-   Ú	getLoggerÚ__name__Ú_logr!   r   r1   r   r   rv   r   r   r   r„   r}   r   rÓ   r   rƒ   r   r   r   r    Ú<module>   s:    1

þ
Y-.%ÿ&ÿi'aÿ*U