a
    ضak                     @   s   d Z ddlZddlZddlmZ ddlmZ ddl	m
Z ee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: :doc:`/tutorials/intermediate/constrainedlayout_guide`
    N)_apic              
   C   s   t | d}|d s td dS tdD ]x}t|| |||||d t|| |||d t||  ||    t|| rt	|| |||||d n
td t
||  q(|S )a  
    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.

    Returns
    -------
    layoutgrid : private debugging structure
    N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.)make_layoutgridsr   Zwarn_externalrangemake_layout_marginsmake_margin_suptitlesmatch_submerged_marginsZupdate_variablescheck_no_collapsed_axesreposition_axesreset_margins)figrendererr   r   r   r   layoutgrids_ r   n/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/matplotlib/_constrained_layout.pydo_constrained_layoutA   s(    







r   c                 C   s   |du rt  }d|d< t| ds4tjddd|| < nB| j }t||}|| }tj|ddd	d	| jj| jjfd
|| < | j	D ]}t
||}q|| j D ]$}t|dr|  }t||}q|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   _parentZfiglb)parentnameZpanellbT   )r   r   parent_innernrowsncols
parent_posget_subplotspec)dicthasattrmlayoutgrid
LayoutGrid_subplotspecget_gridspecmake_layoutgrids_gsrowspancolspansubfigsr	   
_localaxesas_listr    )r   r   gsZparentlbsfigaxr   r   r   r	      s0    






r	   c              	   C   s   || v s|j du r| S d| d< t|ds\| |j  }tj|dd|j|j| | d| |< n|j}|	 }|| vr|t
| |} | | }| d| vrtj|ddd|j|jfd	| | d< tj| | d d|j|j| | d
| |< | S )zR
    Make the layoutgrid for a gridspec (and anything nested in the gridspec)
    NTr   _subplot_specZgridspec)r   r   r   r   r   width_ratiosheight_ratiostopr   )r   r   r   r   r   )r   r   r   r   r1   r2   )figurer"   r#   r$   Z_ncolsZ_nrowsZget_width_ratiosZget_height_ratiosr0   r&   r'   r(   r)   )r   r-   r   Zsubplot_specZparentgsZ	subspeclbr   r   r   r'      sB    

	


r'   c           
      C   s   |j D ]}t| |}|s dS q|jD ]t}t|dr&|  }|| v r&| | }t|jD ]>}t|jD ].}|	||}	|	j
dks|	jdkrh   dS qhqZq&dS )z9
    Check that no axes have collapsed to zero size.
    Fr    r   T)r*   r   axesr"   r    r&   r
   r   r   get_inner_bboxwidthheight)
r   r   r.   okr/   r-   ZlgijZbbr   r   r   r      s    



r   r   r   r   r   c             	   C   s  | j }| }t|drB|jd ur(|jn|}|jd ur<|jn|}n(|jd urR|jn|}|jd urf|jn|}|d }|d }| \}	}
||||ddddd}||
 |kr|jj	dkr||
 |d< |jj
|
k r||
 |d< ||	 |kr|jj
|	k r||	 |d< |jj	dkr||	 |d< |S )	Nr   r   r   )leftcbrightcbbottomcbtopcbleftrightr3   bottomr=   r>   r?   r@   )r%   r&   r"   r   r   _hspace_wspaceget_geometryr)   startstopr(   )objr   r   r   r   ssr-   rD   rE   r   r   marginr   r   r   get_margin_from_padding   s4    
rL   c             
   C   s
  |j D ]D}|j}t| ||||||d t|dd||d}	| | j|	| q|j D ]}
t|
drV|
	 spqV|

 }| }|| vr 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rz|j|jjkr|d
  |j| 7  < n|dkr|j|jjkr|d  |j| 7  < nX|dkr|j|jjkr|d  |j| 7  < n&|j|jjkr|d  |j| 7  < |dv rz|j|jjkrD|j|jk rD|d  |j|j 7  < |j|jjkrz|j|jkrz|d  |j|j 7  < |dv r|j|jjkr|j|jk r|d  |j|j 7  < |j|jjkr|j|jkr|d  |j|j 7  < q| | || qVd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.
    r<   r   r    NrA   rB   rC   r3   locationr>   r=   r@   r?   )r3   rC   rA   rB   )r*   r%   r   rL   r   Zedit_outer_margin_minsr+   r,   r"   get_in_layoutr    r&   get_pos_and_bboxx0x1y0y1
_colorbarscolorbar_get_padget_cb_parent_spans_colorbar_inforH   r)   r7   rG   r(   r8   )r   r   r   r   r   r   r   r.   rJ   Zmarginsr/   r-   rK   posbboxcbaxpadZ	cbp_rspanZ	cbp_cspanloccbposcbbboxr   r   r   r     sx    	











r   r   r   c                C   s  |j  j}tddg||gg}|j |j |}|j}|j}|jD ]}	t	| |	|||d qF|j
d ur|j
 r|j
 }
t|j
ddr|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 )
Nr   r`   Z_autoposFr   r3   r   rC   rA   )transFigureinvertedtransform_bboxmtransformsZBboxtransSubfigurer8   r7   r*   r   Z	_suptitlerO   get_positiongetattrZset_positionget_tightbboxedit_margin_minZ
_supxlabelZ
_supylabel)r   r   r   r   r   Zinv_trans_figZpadboxZh_pad_localZw_pad_localr.   prZ   r   r   r   r   n  sB    




r   c                 C   sf  |j D ]}t| | qdd | D }|D ]2}| }| | vrR|| q,| |  }t|jdkrt	|j
d |jdd  |j
d |jdd   }t	|j
d |jdd  |j
d	 |jdd   }|D ]}	|	 }
| |
  }|durt|
jdkrt	|j
d |
jdd  |j
d |
jdd   }||krP|}t	|j
d |
jdd  |j
d	 |
jdd   }||kr|}q|jdd D ]}|jd||d
 q|jdd D ]}|jd||d
 qt|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 ]}	|	 }
| |
  }|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
 qHq,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 | ]}t |d r| r|qS )r    )r"   rO   ).0ar   r   r   
<listcomp>  s   z+match_submerged_margins.<locals>.<listcomp>r   rA   Nr=   rB   r>   )cellr3   r@   rC   r?   )r*   r   Zget_axesr    r&   removelenr)   npmaxZmargin_valsri   r(   )r   r   r.   ZaxsZax1Zss1Zlg1ZmaxsublZmaxsubrZax2Zss2Zlg2Zmaxsubl2Zmaxsubr2r:   ZmaxsubtZmaxsubbr   r   r   r     s    





r   c           	      C   s   t j}t j }t j}t j }| jd 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)rr   infrX   r    minr(   rG   rs   rH   r)   r
   )	r[   ZrowstartZrowstopZcolstartZcolstopr   rJ   r(   r)   r   r   r   rW     s    

rW   c                 C   s|   | j }| jdd}||j|j }z| j|dd}W n tyT   | j|d}Y n0 |du rd|}n||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.

    T)original)r   Zfor_layout_only)r   N)r4   rf   transformedre   ra   rh   	TypeErrorrb   )r/   r   r   rY   Z	tightbboxrZ   r   r   r   rP   
  s    rP   c             
   C   s  |j |j }|jD ]8}| |  }	|j|	|d t| ||||||d q|j D ]}
t	|
drV|

 snqV|
 }| }| \}}|| vr dS | | j|j|jd}	||	}|
| ddddd}t|
jddd D ]*\}}|
|jd	 d krt| |||d
 qqVdS )zF
    Reposition all the axes based on the new inner bounding box.
    )rZ   )r   r   r   r   r    Nrowscolsr   )rA   rB   rC   r3   rn   rt   offset)ra   re   r*   Zget_outer_bboxZ_redo_transform_rel_figrx   r   r+   r,   r"   rO   r    r&   rF   r6   r(   r)   rc   _set_position	enumeraterU   rX   reposition_colorbar)r   r   r   r   r   r   r   trans_fig_to_subfigr.   rZ   r/   rJ   r-   r   r   Znewbboxr~   nnr[   r   r   r   r   +  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 rr|
||||}|d
kr|j|j }|
j|j |d
  }||| 7 }|d
  |j| 7  < ||d}nV|j|j }|
j|j }||j | | |d  7 }|d  |j| 7  < ||d}n|
||||}|dkr|j|j }|
j|j |d  }||| 7 }|d  |j| 7  < |d|}nV|j|j }|
j|j }||j | | |d  7 }|d  |j| 7  < |d|}||}||j || || |dv rrd| }|| |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
    rt   r   rz   rM   anchorfractionaspectshrinkrN   rB   rA   r3   rC   )rC   r3   r   auto)rX   r&   r4   ra   re   rW   Zget_bbox_for_cbr6   rP   rV   ZshrunkZanchoredrQ   rR   r7   Z
translatedrS   rT   r8   rc   Zset_transformr   Z
set_anchorZset_box_aspectZ
set_aspect)r   r[   r   r~   rt   r-   r   r   	cb_rspans	cb_cspansZ
bboxparentZpbrM   r   r   r   r   r^   r_   ZcbpadZpbcbZlmarginZdxZbmarginZdyr   r   r   r   V  sd    
















r   c                 C   sh   |j D ]}t| | q|jD ]:}t|dr| 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.
    r    N)r*   r   r5   r"   rO   r    r&   )r   r   r.   r/   rJ   r-   r   r   r   r     s    

r   c                 C   s^   |j d }|d  }t|\}}| | j||d}|j d dv rJ|j}n|j}|j d | S )Nrt   r   rz   rM   )rB   rA   r\   )rX   r&   rW   r6   r7   r8   )r   Zcaxrt   r-   r   r   Z	bboxoutersizer   r   r   rV     s    
rV   )NN)__doc__loggingZnumpyrr   Z
matplotlibr   Zmatplotlib.transformsZ
transformsrd   Zmatplotlib._layoutgridZ_layoutgridr#   	getLogger__name___logr   r	   r'   r   rL   r   r   r   rW   rP   r   r   r   rV   r   r   r   r   <module>   s4   
) 
E,,&T'a"+U