o
    ik                     @   s   d Z ddlZddlZddlmZ ddlmZ e	e
Zdd Zdd Z	dddZd	d
 Zdd Zdd ZdddZdddZdd Zd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,
legend).

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`
    Nc                 C   s   | j |v p	|j | v S )N)start)span0span1 r   Y/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/matplotlib/_constrained_layout.py_spans_overlap8   s   r   c                 C   sB   | j D ]}|jdur|j }|d dks|d dkr dS qdS )zEReturn whether all axes in the figure have a finite width and height.N   r      FT)axes
_layoutbox_poslayoutboxget_rect)figaxnewposr   r   r   _axes_all_finite_sized<   s   


r   c                 C   s  | j  j}t }| jD ]}t|dr#|  }	|	jdur#|	|	 qt
|dkr/td | jjdk r?|D ]}	t| |	 q7tdD ]}
| jD ]}t|j |jdur\t|||| qH| j}|duol|jduol| }|r||j|d}|j}t|r|j||d   | jjdk r| j}|jD ]}| rt|||d q|D ]}	t| |	 q| j jd7  _| j  t| r| jD ]}|jdur|j ! }|j"|d	d
 q|r|j! }|#d|  qC|dur|jdur|jd qCtd qCdS )a  
    Do the constrained_layout.  Called at draw time in
     ``figure.constrained_layout()``

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

    renderer : Renderer
      the renderer to use.

     h_pad, w_pad : float
       are in figure-normalized units, and are a padding around the axes
       elements.

     hspace, wspace : float
        are in fractions of the subplot sizes.

    get_subplotspecNr   zgThere are no gridspecs with layoutboxes. Possibly did not call parent GridSpec with the figure= keyword   r   rendererhspacewspaceoriginal)whichg      ?zUconstrained_layout not applied.  At least one axes collapsed to zero width or height.)$transFigureinvertedtransform_bboxsetr
   hasattrr   get_gridspecr   addlencbook_warn_externalconstrained_layout_called_make_ghost_gridspec_slotsrange_logdebug_make_layout_margins	_suptitleget_in_layoutget_window_extentheightnpisfiniteedit_heightchildren_is_gridspec_layoutbox_arrange_subplotspecs_align_spinesupdate_variablesr   r   r   _set_positionset_y)r   r   h_padw_padr   r   invTransFiggssr   gs_suptitledo_suptitlebboxr.   figlbchildr   r   r   r   do_constrained_layoutG   sp   <















rD   c           
      C   s   |  \}}tj|| td}g }| jD ]}t|dr-|jdur-|  |kr-||g7 }q|D ]}| }d||j	|j
d < q0t|D ]\}}	|	sW| || }|d qEdS )a+  
    Check for unoccupied gridspec slots and make ghost axes for these
    slots...  Do for each gs separately.  This is a pretty big kludge
    but shouldn't have too much ill effect.  The worst is that
    someone querying the figure will wonder why there are more
    axes than they thought.
    )dtyper   NTr   F)get_geometryr/   zerosboolr
   r   r   r   r    num1num2	enumerateadd_subplotset_visible)
r   r=   nrowsncolshassubplotspecaxsr   ss0nnhssr   r   r   r&      s$   




r&   c                 C   s  | j }|j j}| jdd}z	| j|dd}W n ty'   | j|d}Y nw |du r/|}n||}t|j	r?t|j
sAdS | jj}	|	du rK|}	| jj}
|
du rU|}
| j|j |j |
  | j|j|j |
  | j|j |j |	  | j|j|j |	  td|j |j |  td|j|j |  td|j |j |	  td	|j td
|j |jjdk r| jjddd | jjddd | jjddd | jjddd | jjddd | jjddd | jjddd | jjddd dS 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.
    T)r   )r   for_layout_onlyr   Nzleft %fzright %fz	bottom %fz
bbox.y0 %fz	pos.y0 %fr      weakstrengthr   ) figurer   r   r   get_positionget_tightbbox	TypeErrorr/   r0   widthr.   r   r9   r:   edit_left_margin_minx0edit_right_margin_minx1edit_bottom_margin_miny0edit_top_margin_miny1r(   r)   r   r%   constrain_height_minconstrain_width_minconstrain_top_marginconstrain_bottom_marginconstrain_right_marginconstrain_left_margin)r   r   r9   r:   r   r;   pos	tightbboxrA   h_padtw_padtr   r   r   r*      sL   r*   c           !         s8     \}}  }  }|du rt|}|du r t|} fdd| jD }g }g }g }	g }
|D ]0}| }|j}|j}|	| |	| |		t
||j|j  |
	t
||j|j  q4t|D ]/\}}|| }|| }|	| }|
| }d}d}d}d}d}d}t|d t|D ]}|| }|| }|| }|
| }|	| } |s|j|jkrtd t|j|jgd d}|s|j|jkrtd	 t|j|jgd
 d}|s|j|jkrtd t|j|jgd d}|s|j|jkrtd t|j|jgd d}|s+t|t|kr+|j|jj| |   d}n)t||rT|| krC|j|jj| |   n|| k rT|j|jj|  |  |sot|t|kro|j|jj| |  d}qt||r||kr|j|jj| |  q||k r|j|jj| |  qqidS )z
    - Align right/left and bottom/top spines of appropriate subplots.
    - Compare size of subplotspec including height and width ratios
       and make sure that the axes spines are at least as large
       as they should be.
    Nc                    s4   g | ]}t |d r|jdur|   kr|qS )r   N)r   r   r   r    ).0r   r=   r   r   
<listcomp>D  s    
z!_align_spines.<locals>.<listcomp>Fr   z+same start columns; line up layoutbox leftsleftTz+same stop columns; line up layoutbox rightsrightz'same start rows; line up layoutbox topstopz)same stop rows; line up layoutbox bottomsbottom)rF   get_width_ratiosget_height_ratiosr/   onesr
   r   rowspancolspanappendsumr   stoprK   r'   r"   r(   r)   	layoutboxalignr   constrain_heightr.   r   rg   constrain_widthr^   rh   )!r   r=   rN   rO   width_ratiosheight_ratiosrQ   rowspanscolspansheightswidthsr   rR   r{   r|   idx0ax0rowspan0colspan0height0width0	alignleft
alignrightalignbotaligntopalignheight
alignwidthidx1ax1rowspan1colspan1width1height1r   rr   r   r5   3  s   











r5   c                 C   s4  g }| j D ]}| r!|j D ]}| rt|||d q||g7 }q|D ]s}|j}|  \}}	|j}
|j}|dd }|D ]W}|j}|j}|j}||	 }|j	|j
kr_tj|j|jg|d |j	|j
krptj|j|jg|d || }|
j	|j
krtj|j|jg|d |j	|
j
krtj|j|jg|d q?q$dS )zCRecursively arrange the subplotspec children of the given gridspec.r   r   Npadding)r2   _is_subplotspec_layoutboxr3   r4   artistr    rF   r{   r|   r   r   r   hstackr   vstack)r=   r   r   
sschildrenrC   child2child0rR   rN   rO   r   r   child1ss1r   r   padr   r   r   r4     s@   


r4   皙?c                 C   sz  | j }| j}|  j }tj||jd |d}	|dv rjtj|	|	jd ddd|d}
|dkr:tj||	g||j d	d
 ntj|	|g||j d t||
gd|g t	||
gd |
j
||j d|  d	d |	|
fS |dv rtj|	|	jd ddd|d}
|dkrtj||	g||j d ntj|	|g||j d tj||
gd|gd	d t	||
gd |
j|j| | dd |	|
fS )~
    Do the layout for a colorbar, to not overly pollute colorbar.py

    *pad* is in fraction of the original axis size.
    .cbarparentnamer   rt   ru   .posFTr   r   
tightwidthrm   subplotr   ru   strongr   rY   r   r   v_centerrX   rw   rv   r   r   tightheightrm   r   r   rw   h_centermedium)r   r   r   r   	LayoutBoxr   r   r^   match_heightsr   r   r.   r   match_widthsr   )r   caxshrinkaspectlocationr   axlbaxposaxsslblblbposr   r   r   layoutcolorbarsingle  s`   

r   c                 C   s   t j }}t j  }}d } } }}| D ]6}	|	 }
|
jj|k r'|
jj}|	}|
jj|kr3|
jj}|	}|
jj|k r?|
jj}|	}|
jj|krK|
jj}|	}q||d ||||d ||fS )zS
    Find axes covering the first and last rows and columns of a list of axes.
    Nr   )r/   infr   r{   r   r   r|   )rQ   startrowstartcolstoprowstopcolstartax_rowstartax_col
stopax_row
stopax_colr   subspecr   r   r   _getmaxminrowcolumn  s*   
r   c                 C   s,  | d    }|j}tj|j|jjd |d}t| \}	}
}}}}}}|dv rtj||jd ddd|d}| D ]}|d	krD|j|g}n||jg}tj|||j	 d
d q8|j
D ]K}|j}|d	kr{|jjd |kro|j|g}n)|jj|krz||jg}n|dkr|jj|kr||jg}n|jjd |k r|j|g}tj|||j	 d
d qX|j}|j}|j|j|j | d
d |j|j|j d|  d |j d
d |j|j||  d
d ||fS |dv rtj||jd ddd|d}| D ]}|dkr|j|g}n||jg}tj|||j	 d
d q|j
D ]R}|j}|dkr6|jjd |	kr)|j|g}n-|jj|
kr5||jg}n |dkrV|jjd |	k rJ|j|g}n|jj|
krV||jg}tj|||j	 d
d q|j}|j}||j|j |  ||j|j d|  d |j  |j|j	| | dd ||fS )r   r   r   r   r   r   FTr   ru   r   r   r   rt   rX   r   r   r   rw   rv   r   )r   r    r   r   r   r   r   r   r   r^   r2   r   r|   r   r   r   r   rv   rw   constrain_bottomr   r.   r   r{   ru   rt   constrain_left)parentsr   r   r   r   r   r=   gslbr   minrowmaxrow	minax_row	maxax_rowmincolmaxcol	minax_col	maxax_colr   r   orderchr   maxposlbminposlbr   r   r   layoutcolorbargridspec  s   







/








r   )NN)r   r   )r   )__doc__loggingnumpyr/   matplotlib.cbookr#   matplotlib._layoutboxr   r   	getLogger__name__r(   r   r   rD   r&   r*   r5   r4   r   r   r   r   r   r   r   <module>   s$    -

 7
t
#;