a
    ضaJ                  
   @   s  d dl Zd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZ drddZdsdd	Zed
gdd Zedgdd Zedgdd Zedgdd Zedgdddd Zedgdddd Zdd Zed gd!d" Zed#gd$d% Zed&gd'd( Zed)gd*d+ Zed,gd-d. Zed/gd0d1 Zed2gd3dd4d5 Zed6gd7d8 Z ed9gd:d; Z!ed<gd=d> Z"ed?gd@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)edNgdOdPdQdRdS Z*dTdU Z+dVdW Z,edXgddPdQdYdZ Z-ed[gddPdQd\d] Z.d^d_ Z/ed`gdOdPdadbidcddde Z0edfgdOdPdae1dgd gdhdiggidcdjdk Z2dldm Z3dndo Z4dpdq Z5dS )t    N)image_comparison)tickerrcParams   Fc                 C   sb   |  ddg | jdd |sJ| jd|d | jd|d | jd|d n| g  | g  d S )	N         Znbinsx-labelfontsizey-labelTitle)plotZlocator_params
set_xlabel
set_ylabel	set_titleset_xticklabelsset_yticklabels)axr   nodec r   w/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/matplotlib/tests/test_constrainedlayout.pyexample_plot   s    
r   c              	   C   s   d\}}t jtdd| |tdd| |f \}}d|d  |d  |d  t |d  |d   }| j|||d dd df d	d
ddd}| jd|d | jd|d | jd|d |S )N)333333?r   r   r          @   r   ZRdBu_rg            ?T)ZcmapZvminZvmaxZ
rasterizedr
   r   r   r   )npZmgridsliceexp
pcolormeshr   r   r   )r   r   ZdxZdyyxzpcmr   r   r   example_pcolor   s    
4 r(   zconstrained_layout1.pngc                  C   s$   t jdd} |  }t|dd dS )z,Test constrained_layout for a single subplotTconstrained_layout   r   N)pltfigureadd_subplotr   figr   r   r   r   test_constrained_layout1$   s    r1   zconstrained_layout2.pngc                  C   s0   t jdddd\} }|jD ]}t|dd qdS )z(Test constrained_layout for 2x2 subplotsr   Tr)   r+   r   N)r,   subplotsflatr   r0   axsr   r   r   r   test_constrained_layout2,   s    
r6   zconstrained_layout3.pngc                  C   sZ   t jdddd\} }t|jD ]6\}}t|dd}|dkr@d}nd}| j|||d	 qd
S )z3Test constrained_layout for colorbars with subplotsr   Tr)   r+   r   r   g{Gz?{Gz?)r   padN)r,   r2   	enumerater3   r(   colorbar)r0   r5   nnr   r'   r8   r   r   r   test_constrained_layout34   s    r<   zconstrained_layout4.pngc                  C   sB   t jdddd\} }|jD ]}t|dd}q| j||ddd d	S )
z;Test constrained_layout for a single colorbar with subplotsr   Tr)   r+   r   {Gz?r   r   r8   shrinkNr,   r2   r3   r(   r:   r0   r5   r   r'   r   r   r   test_constrained_layout4B   s    
rB   zconstrained_layout5.pnggMb`?)Ztolc                  C   sF   t jdddd\} }|jD ]}t|dd}q| j||dddd	d
 dS )zZ
    Test constrained_layout for a single colorbar with subplots,
    colorbar bottom
    r   Tr)   r+   r   Fr=   r   bottom)r   Zuse_gridspecr8   r?   locationNr@   rA   r   r   r   test_constrained_layout5L   s    
rE   zconstrained_layout6.pngc               	   C   s   dt jd< t jdd} | jdd| d}|d dd}|d dd}g }|D ]$}| |}||g7 }t|d	d
 qN|d g }|D ]$}| |}||g7 }t|d	d
}q| j	||dddt
jddd dS )z,Test constrained_layout for nested gridspecsFpcolormesh.snapTr)   r   r   r-   r   r   r   zx-label
MultiLiner=   gGz?rC   r   r	   )r   r8   r?   rD   ZticksN)r,   r   r-   add_gridspecsubgridspecr.   r   r   r(   r:   r   ZMaxNLocator)r0   gsgslgsrZaxslr   Zaxsrr'   r   r   r   test_constrained_layout6[   s(    






rM   c                  C   s   t jtddh tjdd} tdd}tdd|d }tdd|d }|D ]}| | qP| 	  W d   n1 s|0    Y  dS )	z2Test for proper warning if fig not set in GridSpeczhThere are no gridspecs with layoutgrids. Possibly did not call parent GridSpec with the "figure" keywordmatchTr)   r   r   r   N)
pytestZwarnsUserWarningr,   r-   gridspecGridSpecGridSpecFromSubplotSpecr.   draw_without_rendering)r0   rJ   rK   rL   r   r   r   test_constrained_layout7v   s    rV   zconstrained_layout8.pngc                  C   s   t jddd} tjdd| d}g }dD ]|}|dkr:d	g}ndd
g}|D ]Z}| |||f }||g7 }t|dd}|dkr|d |d	k r|d |d qFq&| |dddf }||g7 }t|dd}| j	||ddd dS )z/Test for gridspecs that are not completely full)
   r   Tfigsizer*   r   r   rG   r   r   r   r      	   r    r   Nr=   r   r>   )
r,   r-   rR   rS   r.   r(   r   r   r   r:   )r0   rJ   r5   jZilistir   r'   r   r   r   test_constrained_layout8   s(    



r`   zconstrained_layout9.pngc                  C   sr   t jdddddd\} }|jD ]$}t|dd}|d |d q|d | j||d	d
d | jddd dS )z4Test for handling suptitle and for sharex and shareyr   TF)r*   sharexshareyr+   r   r]   r   r=   r   r>   zTest Suptitle   N)	r,   r2   r3   r(   r   r   
set_aspectr:   suptitlerA   r   r   r   test_constrained_layout9   s    




rf   zconstrained_layout10.pngc                  C   sF   t jdddd\} }|jD ]}|jtddd q|jddd	 d
S )z%Test for handling legend outside axisr   Tr)   r   zThis is a label)labelzcenter left)g?      ?)locZbbox_to_anchorN)r,   r2   r3   r   r    ZarangeZlegendr4   r   r   r   test_constrained_layout10   s    
rj   zconstrained_layout11.pngc                  C   s   t jddd} tjdd| d}tdd|d }tdd|d }| |d }t|dd	 g }|D ]$}| |}||g7 }t|dd	}qd| j||d
dd | |d }t|dd	 dS )z"Test for multiple nested gridspecsT)   r   r*   rY   r   r   rG   r   r\   r   r        Q@r   r?   aspectN	r,   r-   rR   rS   rT   r.   r   r(   r:   r0   gs0rK   Zgsl0r   r5   rJ   r'   r   r   r   test_constrained_layout11   s    

rs   zconstrained_layout11rat.pngc                  C   s   t jddd} tjdd| ddgd}tdd|d }tjdd|d ddgd	}| |d }t|d
d g }|D ]$}| |}||g7 }t|d
d}qr| j||ddd | |d }t|d
d dS )z4Test for multiple nested gridspecs with width_ratiosT)rW   r   rl   r   r      )r-   Zwidth_ratiosr   )height_ratiosr\   r   r   rm   rn   Nrp   rq   r   r   r   test_constrained_layout11rat   s    

rv   zconstrained_layout12.pngc                  C   s   t jddd} tjdd| d}| |ddd	f }| |ddd	f }t|d
d t|d
d | |dddf }t|dd | |dddf }t|dd | |dddf }t|dd |d dS )z/Test that very unbalanced labeling still works.T)rt      rl   rt   r   rG   Nr   r      r   r   )r   r[   r
   )r,   r-   rR   rS   r.   r   r   )r0   rr   ax1ax2r   r   r   r   test_constrained_layout12   s    r{   zconstrained_layout13.pngr7   c                  C   s   t jdddd\} }|jD ]$}t|dd}| j||dddd	 qtjtd
d | jddd W d   n1 sr0    Y  | jddd dS )Test that padding works.r   Tr)   r   r   r         4@r7   r   r?   ro   r8   zunexpected keyword argumentrN   r   )ZwpadZhpadNgUUUUUU?)w_padh_pad)	r,   r2   r3   r(   r:   rP   Zraises	TypeErrorset_constrained_layout_padsrA   r   r   r   test_constrained_layout13   s    
,r   zconstrained_layout14.pngc                  C   sV   t jdddd\} }|jD ]$}t|dd}| j||dddd	 q| jd
d
ddd dS )r|   r   Tr)   r   r   r   r}   r7   r~   gUUUUUU?皙?r   r   ZhspaceZwspaceN)r,   r2   r3   r(   r:   r   rA   r   r   r   test_constrained_layout14  s    
r   zconstrained_layout15.pngc                  C   s4   dt d< tdd\} }|jD ]}t|dd qdS )zTest that rcparams work.Tzfigure.constrained_layout.user   r   r   N)r   r,   r2   r3   r   r4   r   r   r   test_constrained_layout15  s    
r   zconstrained_layout16.pngc                  C   s.   t jdd\} }t|dd | g d}dS )zTest ax.set_position.Tr)   r   r   )r   r   皙?r   N)r,   r2   r   add_axesr0   r   rz   r   r   r   test_constrained_layout16  s    r   zconstrained_layout17.pngc                  C   s   t jdd} tjdd| d}| |d }| |dddf }| |dddd	f }| |ddd
f }t| t| t| t| dS )zTest uneven gridspecsTr)   r   rG   r   r   r   r   Nr   r   )r,   r-   rR   rS   r.   r   )r0   rJ   ry   rz   ax3Zax4r   r   r   test_constrained_layout17!  s    r   c                  C   sT   t jdd\} }| }t| t|dd |   t| j| jksPJ dS )z
Test twinxTr)   r+   r   N)r,   r2   Ztwinxr   rU   allget_positionextentsr   r   r   r   test_constrained_layout182  s    r   c                  C   sh   t jdd\} }| }t| t|dd |d |d |   t| j| jksdJ dS )z
Test twinyTr)   r+   r   r]   N)	r,   r2   Ztwinyr   r   rU   r   r   r   r   r   r   r   test_constrained_layout19<  s    

r   c                  C   sh   t ddd} t | | dddf }t }|g d}|| | |ddddf }|| dS )z)Smoke test cl does not mess up added axesr   r[   N)r   r   r   r   r   )r    Zlinspacehypotr,   r-   r   r#   r:   )Zgximgr0   r   Zmeshr   r   r   test_constrained_layout20H  s    r   c                  C   sf   t jdd\} }| d |   t| j}| d |   t| j}tj	|| dS )z>#11035: repeated calls to suptitle should not alter the layoutTr)   Z	Suptitle0Z	Suptitle1N)
r,   r2   re   rU   r    copyr   r   testingassert_allcloser0   r   Zextents0extents1r   r   r   test_constrained_layout21S  s    

r   c                  C   s`   t jdd\} }|   t| j}| jddd |   t| j}tj	|| dS )zC#11035: suptitle should not be include in CL if manually positionedTr)   ZSuptitlerh   r$   N)
r,   r2   rU   r    r   r   r   re   r   r   r   r   r   r   test_constrained_layout22b  s    r   c                  C   sN   t dD ]@} tjdddd}|dd}|d dd}|d|  qdS )	zu
    Comment in #11035: suptitle used to cause an exception when
    reusing a figure w/ CL with ``clear=True``.
    r   TZ123)r*   clearnumr   r   z
Suptitle{}N)ranger,   r-   rH   rI   re   format)r_   r0   rJ   subr   r   r   test_constrained_layout23p  s
    r   ztest_colorbar_location.pngTZmpl20)remove_textstylec                  C   s   dt jd< t jdddd\} }|jD ] }t|}|d |d q$| j||ddd	f d
d | j||dddf ddd | j||dddf dddd | j||dddf ddd | j||d ddd | j||d	ddf ddd dS )zY
    Test that colorbar handling is as expected for various complicated
    cases...
    FrF   r[   r   Tr)   r]   Nr   r   )r   r?   r   r   rh   rC   )r   r?   rD   r   g?)r   r?   rD   r8   r   topr   leftright)r,   r   r2   r3   r(   r   r   r:   rA   r   r   r   test_colorbar_location}  s    


 r   c                  C   sX   t jdddd\} }|d d |   t|d  j}tjj	|g ddd	 d S )
Nr   Tr)   rZ   Fr   )g|R?g\	b?g&:?g9?q?h㈵>)rtol)
r,   r2   Zset_visiblerU   r    r   r   r   r   r   )r0   r5   r   r   r   r   test_hidden_axes  s    
r   c               	   C   sr  dD ]f} t jdddd\}}g }t|jD ]\}}|jdd t|}|j||| ddd	}||g7 }|jjdd |d
kr,|jj	g  |jj
	g  |g  |g  q,|jddddd |  | dv r tj|d j j|d j j tj|d
 j j|d j j qtj|d j j|d
 j j tj|d j j|d j j qd S )N)r   r   r   rC   r   Tr)   in)	directionr   g{Gz?)r   rD   r?   r8   r   gqq?g?r   )r   r   r   r   )r,   r2   r9   r3   tick_paramsr(   r:   r   ZxaxisZ	set_ticksyaxisr   r   r   rU   r    r   r   r   x0y0)rD   r0   r5   Zcbsr;   r   pccbr   r   r   test_colorbar_align  sB    




r   ztest_colorbars_no_overlapV.pngc                  C   s|   t jddd} | jddddd}|D ]H}|jt  |jddd	 |ddgd
dgg}| j	||dd q$| 
d d S )N)r   r[   TrX   r   r   ra   rb   bothr   Zaxisr   r   r[   Zverticalr   Zorientationfoo)r,   r-   r2   r   set_major_formatterr   NullFormatterr   imshowr:   re   r0   r5   r   Zimr   r   r   test_colorbars_no_overlapV  s    r   ztest_colorbars_no_overlapH.pngc                  C   s|   t jddd} | d | jddddd}|D ]H}|jt  |jdd	d
 |	ddgddgg}| j
||dd q.d S )N)r[   r   TrX   r   r   r   r   r   r   r   r   r[   Z
horizontalr   )r,   r-   re   r2   r   r   r   r   r   r   r:   r   r   r   r   test_colorbars_no_overlapH  s    
r   c                  C   s   t jdddd\} }|d g d |   |d  }tj|ddgddgg t jdddd\} }|d g d |d tj	
d	d	}| j||d d
 |   |d  }tj|ddgddgg d S )Nr   r   Tr)   r   )r   r   333333?r   r   rh      )r   g)\(?)r,   r2   Zset_positionrU   r   r    r   r   r#   randomZrandr:   )r0   r5   ppr   r   r   r   test_manually_set_position  s    r   ztest_bboxtight.pngZbbox_inchesZtight)r   r   Zsavefig_kwargc                  C   s   t jdd\} }|d d S NTr)   r   r,   r2   rd   r/   r   r   r   test_bboxtight  s    r   ztest_bbox.pngrh   g      @r   c                  C   s   t jdd\} }|d d S r   r   r/   r   r   r   	test_bbox  s    r   c                  C   s   t jddddddid\} \}}}|dd |d	 |d
d |d	 |dd |d	 | j|||fd |   |jj |jj |jj g}t	j
j|d j|d jg|d jddd |d jdksJ dS )z
    Tests for a bug in which constrained layout and align_ylabels on
    three unevenly sized subplots, one of whose y tick labels include
    negative numbers, drives the non-negative subplots' y labels off
    the edge of the plot
    r   r   T)g@rw   ru   )r   r   gffffff?)r*   rY   Zgridspec_kwr   ZLabelg      g      ?)r5   r   r   )r   ZatolN)r,   r2   Zset_ylimr   Zalign_ylabelsrU   r   rg   Zget_window_extentr    r   r   r   )r0   r   ry   rz   Zafter_alignr   r   r   test_align_labels  s(    






r   c                  C   s  t jdd\} }|   || j }| d | d |   || j }|j|jd kshJ |j	|j	d ks|J t jdd\} }|   || j }| jddd | jddd |   || j }|j|jd ksJ |j	|j	d ksJ d S )	NTr)   ZBooZBooyg      $@rh   )r%   r   )
r,   r2   rU   Zget_tightbboxZcanvasZget_rendererZ	supxlabelZ	supylabelr   r   )r0   r   Zpos0posr   r   r   test_suplabels  s$    

r   c                  C   s:   t  } | dd}| |dd dd f }|   d S )Nr   r   r   )r,   r-   rH   r.   rU   )r0   rJ   spr   r   r   test_gridspec_addressing5  s    r   )r   F)r   )6Znumpyr    rP   Zmatplotlib.testing.decoratorsr   Zmatplotlib.pyplotZpyplotr,   Zmatplotlib.gridspecrR   Zmatplotlib.transformsZ
transformsZmtransformsZ
matplotlibr   r   r   r(   r1   r6   r<   rB   rE   rM   rV   r`   rf   rj   rs   rv   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   ZBboxr   r   r   r   r   r   r   r   <module>   s   





	














 



"