B
    ]”t\xr  ã               @   s®   d Z ddl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„ 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Î  
This module provides the routine to 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 :meth:`~matplotlib.gridspec`, with one constraint per
gridspec, so it is possible to have overlapping axes if the gridspecs
overlap (i.e. using :meth:`~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)ÚLegendc             C   s0   ||   ko|kn  p.||  ko*|kS   S )N© )Ú
colnum0minÚ
colnum0maxÚ
colnumCminÚ
colnumCmaxr   r   ú=lib/python3.7/site-packages/matplotlib/_constrained_layout.pyÚ_in_same_column;   s    r	   c             C   s0   ||   ko|kn  p.||  ko*|kS   S )Nr   )Ú
rownum0minÚ
rownum0maxÚ
rownumCminÚ
rownumCmaxr   r   r   Ú_in_same_row@   s    r   c             C   sD   x>| j D ]4}|jdk	r|j ¡ }|d dks8|d dkrdS qW dS )zw
    helper function to make sure all axes in the
    figure have a finite width and height.  If not, return False
    Né   r   é   FT)ÚaxesÚ
_layoutboxÚ_poslayoutboxÚget_rect)ÚfigÚaxÚnewposr   r   r   Ú_axes_all_finite_sizedE   s    

r   c             C   sÞ  | j  ¡ j}tƒ }x8| jD ].}t|dƒr| ¡  ¡ }	|	jdk	r| 	|	¡ qW t
|ƒdkrbt d¡ | jjdk r†x|D ]}	t| |	ƒ qtW xPtdƒD ]B}
x2| jD ](}t |j¡ |jdk	r t||||ƒ q W | jdk	r| jjdk	r| j}||j|dƒ}|j|j }|j || ¡ | jjdk rp| j}x(|jD ]}| ¡ r4t|||d q4W x|D ]}	t| |	ƒ q\W | j jd7  _| j ¡  t| ƒrÌx@| jD ]*}|jdk	rœ|j ¡ }|j |d	d
 qœW q’t d¡ q’W d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)ZwhichzUconstrained_layout not applied.  At least one axes collapsed to zero width or height.)!ÚtransFigureÚinvertedÚtransform_bboxÚsetr   Úhasattrr   Úget_gridspecr   ÚaddÚlenÚwarningsÚwarnÚconstrained_layout_calledÚ_make_ghost_gridspec_slotsÚrangeÚ_logÚdebugÚ_make_layout_marginsZ	_suptitleZget_window_extentÚy1Úy0Zedit_heightÚchildrenÚ_is_gridspec_layoutboxÚ_arrange_subplotspecsÚ_align_spinesZupdate_variablesr   r   r   Z_set_position)r   r   Úh_padÚw_padr   r   ÚinvTransFigZgssr   ÚgsZnnnZsupÚbboxÚheightZfiglbÚchildr   r   r   r   Údo_constrained_layoutS   sJ    A










r<   c       
      C   sð   |  ¡ \}}tj|| td}g }x<| jD ]2}t|dƒr*|jdk	r*| ¡  ¡ |kr*||g7 }q*W x:|D ]2}| ¡ }|j	dkr„|j
|_	d||j
|j	d …< qfW xNt|ƒD ]B\}}	|	s¦|  || ¡}| d¡ | g ¡ | g ¡ | d¡ q¦W d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 shoudn't have too much ill effect.  The worst is that
    someone querrying the figure will wonder why there are more
    axes than they thought.
    )Údtyper   NTr   F)r   r   r   r   )Úget_geometryÚnpÚzerosÚboolr   r#   r   r   r$   Únum2Únum1Ú	enumerateZadd_subplotZset_frame_onZ
set_xticksZ
set_yticksZset_facecolor)
r   r8   ÚnrowsÚncolsZhassubplotspecÚaxsr   Úss0ÚnnZhssr   r   r   r*   æ   s(    






r*   c             C   sœ  | j }|j ¡ j}| jdd}| j|d}||ƒ}| jj}	|	dkrF|}	| jj}
|
dkrZ|}
| j 	|j
 |j
 |
 ¡ | j |j|j |
 ¡ | j |j |j |	 ¡ | j |j|j |	 ¡ t d|j
 |j
 | ¡ t d|j|j | ¡ t d|j |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 )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   Nzleft %fzright %fz	bottom %fr   é   Zweak)Ústrengthr   )Zfigurer   r    r!   Zget_positionZget_tightbboxr   r5   r6   Zedit_left_margin_minZx0Zedit_right_margin_minZx1Zedit_bottom_margin_minr0   Zedit_top_margin_minr/   r,   r-   r   r)   Úconstrain_height_minÚconstrain_width_minZconstrain_top_marginZconstrain_bottom_marginZconstrain_right_marginZconstrain_left_margin)r   r   r5   r6   r   r7   ÚposZ	tightbboxr9   Zh_padtZw_padtr   r   r   r.     s>    r.   c       )      C   sÎ  |  ¡ \}}| ¡ }| ¡ }|dkr.t |¡}|dkr@t |¡}g }x<| jD ]2}t|dƒrL|jdk	rL| ¡  	¡ |krL||g7 }qLW tj
t|ƒtjd}tj
t|ƒtjd}	tj
t|ƒtjd}
tj
t|ƒtjd}t 
t|ƒ¡}t 
t|ƒ¡}x¤t|ƒD ]˜\}}| ¡ }|jdkr|j|_t|j|ƒ\||< |
|< t|j|ƒ\|	|< ||< t ||
| || d … ¡||< t ||| |	| d … ¡||< qøW x2t|dd… ƒD ]\}}| ¡ }|| |
|  }}|	| ||  }}|| ||  }}d}d}d}d}d}d}x¸t|d t|ƒƒD ] }|| }|| |
|  }} |	| ||  }!}"|| ||  }#}$|sŠ|| krŠt |j|jgd¡ d}|s²||"kr²t |j|jgd	¡ d}|sä||krät d
¡ t |j|jgd¡ d}|s||!krt d¡ t |j|jgd¡ d}|!| d }%|| d }&|"|  d }'|| d }(|st|&|%krt|j |jj| |$ ¡ d}nŽt||| |"ƒr||$krÄ|j |jj| |$ ¡ |j |jj|$ |d  ¡ n>||$k r|j |jj|$ | ¡ |j |jj| |$d  ¡ |s0|(|'kr0|j |jj| |# ¡ d}nŽt||||!ƒr||#kr€|j |jj| |# ¡ |j |jj|# |d  ¡ n>||#k r|j |jj|# | ¡ |j |jj| |#d  ¡ qW q¨W d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.
    Nr   )r=   r   éÿÿÿÿFÚleftTÚrightzrownum0min == rownumCminÚtopzrownum0max == rownumCmaxÚbottomgÍÌÌÌÌÌü?) r>   Zget_width_ratiosZget_height_ratiosr?   Zonesr   r#   r   r   r$   r@   r&   Zint8rD   rB   rC   ÚdivmodÚsumr+   Ú	layoutboxÚalignr   r,   r-   Úconstrain_heightr:   r	   rL   Úconstrain_widthÚwidthr   rM   ))r   r8   rE   rF   Zwidth_ratiosZheight_ratiosrG   r   Z	rownumminZ	rownummaxZ	colnumminZ	colnummaxrZ   r:   ÚnrH   rI   r
   r   r   r   Zwidth0Zheight0Z	alignleftZ
alignrightZalignbotZaligntopZalignheightZ
alignwidthZmmZaxcr   r   r   r   ZwidthCZheightCZdrowsCZdrows0ZdcolsCZdcols0r   r   r   r4   3  sÒ    



"











r4   c             C   sŠ  g }xF| j D ]<}| ¡ rx$|j D ]}| ¡ r t|||d q W ||g7 }qW x6|D ],}|j}| ¡  ¡ \}}	|jdkr‚|j|_t	|j|	ƒ\}
}t	|j|	ƒ\}}|dd… }xÒ|D ]Ê}|j}t	|j|	ƒ\}}|jdkrà|j|_t	|j|	ƒ\}}||	 }||k rt
j|j|jg|d ||k r8t
j|j|jg|d || }||k r`t
j|j|jg|d ||
k r´t
j|j|jg|d q´W qTW dS )z
    arrange the subplotspec children of this gridspec, and then recursively
    do the same of any gridspec children of those gridspecs...
    )r   r   Nr   )Úpadding)r1   Z_is_subplotspec_layoutboxr2   r3   Úartistr$   r>   rB   rC   rT   rV   Úhstackr   Úvstack)r8   r   r   Z
sschildrenr;   Zchild2Zchild0rH   rE   rF   Z
rowNum0minZ
colNum0minZ
rowNum0maxZ
colNum0maxZchildcZsscZ
rowNumCminZ
colNumCminZ
rowNumCmaxZ
colNumCmaxZthepadr   r   r   r3   Ñ  sJ    





r3   çš™™™™™©?c             C   sx  | j }| j}|  ¡ j }tj||jd |d}	|dkrÎtj|	|	jd ddd|d}
|dkrttj||	g||j d	d
 ntj|	|g||j d t ||
gd|g¡ t 	||
gd¡ |
j
||j d|  d	d n¢|dkrptj|	|	jd ddd|d}
|dkrt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 )z
    Do the layout for a colorbar, to not oeverly pollute colorbar.py

    `pad` is in fraction of the original axis size.
    z.cbar)ÚparentÚnamer]   )rP   rQ   z.posFT)ra   rb   Ú
tightwidthrN   Úsubplotr]   rQ   Ústrong)r\   rK   )r\   r   Zv_center)rK   )rS   rR   )ra   rb   ÚtightheightrN   rd   r]   rS   Zh_centerÚmedium)r   r   r   rV   Ú	LayoutBoxrb   r^   rZ   Zmatch_heightsrW   rY   r:   r_   Zmatch_widthsrX   )r   ÚcaxÚshrinkÚaspectÚlocationÚpadZaxlbZaxposZaxsslbÚlbÚlbposr   r   r   Úlayoutcolorbarsingle  sP    





rp   c             C   sž   d}d}d }d }d}d}d }d }xh| D ]`}	|	  ¡ }
|
 ¡ \}}}}}}||krV|}|	}||k rf|}|	}||krv|}|	}||k r&|}|	}q&W ||||||||fS )Ni`yþÿi@B )r   Úget_rows_columns)rG   ÚmaxrowÚminrowZmaxaxZminaxÚmaxcolÚmincolÚ	maxax_colÚ	minax_colr   ÚsubspecrE   rF   Ú	row_startÚrow_stopÚ	col_startÚcol_stopr   r   r   Ú_getmaxminrowcolumn?  s0    
r}   c             C   s8  | d   ¡  ¡ }|j}tj|j|jjd |d}t| ƒ\}	}
}}}}}}|dkr¾tj||jd ddd|d}x@| D ]8}|d	krŒ|j|g}n
||jg}tj|||j	 d
d qtW x¢|j
D ]˜}|j}| ¡ \}}}}}}|d	kr||krò|j|g}||kr:||jg}n2|dkr:||kr&||jg}||k r:|j|g}tj|||j	 d
d q¸W |j}|j}|j|j|j | d
d |j|j|j d|  d |j d
d |j|j||  d
d nr|dkr0tj||jd ddd|d}xD| D ]<}|dkr|j|g}n
||jg}tj|||j	 d
d qêW x¦|j
D ]œ}|j}| ¡ \}}}}}}|dkr„||	krn|j|g}||
kr¶||jg}n2|dkr¶||	k r¢|j|g}||
kr¶||jg}tj|||j	 d
d q2W |j}|j}| |j|j | ¡ | |j|j d|  d |j ¡ |j|j	| | dd ||fS )z
    Do the layout for a colorbar, to not oeverly pollute colorbar.py

    `pad` is in fraction of the original axis size.
    r   z.cbar)ra   rb   r]   )rP   rQ   z.posFT)ra   rb   rc   rN   rd   r]   rQ   re   )r\   rK   rP   )rK   r   r   )rS   rR   )ra   rb   rf   rN   rd   r]   rS   rR   rg   )r   r$   r   rV   rh   ra   rb   r}   r^   rZ   r1   r]   rq   r   rX   rR   rS   Zconstrain_bottomrY   r:   r_   rQ   rP   Zconstrain_left)Úparentsri   rj   rk   rl   rm   r8   Zgslbrn   rs   rr   Z	minax_rowZ	maxax_rowru   rt   rw   rv   ro   r   ÚorderZchrx   rE   rF   ry   rz   r{   r|   ZmaxposlbZminposlbr   r   r   Úlayoutcolorbargridspec]  s¦    

























 r€   )NN)r   r   )r`   )r`   )Ú__doc__Znumpyr?   Zloggingr'   Zmatplotlib.legendr   Zmatplotlib.transformsZ
transformsZmatplotlib._layoutboxr   rV   Z	getLoggerÚ__name__r,   r	   r   r   r<   r*   r.   r4   r3   rp   r}   r€   r   r   r   r   Ú<module>   s(    

  - 
3
;