U
    \	aL                  
   @   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 dpddZdqd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 Z4dS )r    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| d	 | d	 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   K/tmp/pip-unpacked-wheel-wjyw_3jo/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    r2   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   r1   axsr   r   r   r   test_constrained_layout2,   s    
r7   zconstrained_layout3.pngc                  C   sd   dt jd< t jdddd\} }t|jD ]6\}}t|dd}|dkrJd	}nd
}| j|||d q(dS )z3Test constrained_layout for colorbars with subplotsFpcolormesh.snapr   Tr*   r,   r   r   g{Gz?{Gz?)r   padN)r-   r   r3   	enumerater4   r)   colorbar)r1   r6   nnr   r(   r:   r   r   r   test_constrained_layout34   s    
r>   zconstrained_layout4.pngc                  C   sL   dt jd< 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 subplotsFr8   r   Tr*   r,   r   {Gz?r   r   r:   shrinkNr-   r   r3   r4   r)   r<   r1   r6   r   r(   r   r   r   test_constrained_layout4D   s
    

rD   zconstrained_layout5.pnggMb`?)Ztolc                  C   sP   dt jd< 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
    Fr8   r   Tr*   r,   r   r?   r   bottom)r   Zuse_gridspecr:   rA   locationNrB   rC   r   r   r   test_constrained_layout5P   s    

  rG   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 gridspecsFr8   Tr*   r   r   r.   r   r   r   zx-label
MultiLiner?   gGz?rE   r   r	   )r   r:   rA   rF   ZticksN)r-   r   r.   add_gridspecsubgridspecr/   r   r   r)   r<   r   ZMaxNLocator)r1   gsgslgsrZaxslr   Zaxsrr(   r   r   r   test_constrained_layout6a   s,    





  
rN   c               	   C   s~   t jtddf tjdd} tdd}tdd|d }tdd|d }|D ]}| | qP| 	| j
  W 5 Q R X 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canvasget_renderer)r1   rK   rL   rM   r   r   r   test_constrained_layout7|   s     rX   zconstrained_layout8.pngc                  C   s   dt jd< t jddd} tjdd| d}g }d	D ]|}|d
krDdg}nd
dg}|D ]Z}| |||f }||g7 }t|dd}|d
kr|d |dk r|d |	d qPq0| |dddf }||g7 }t|dd}| j
||ddd dS )z/Test for gridspecs that are not completely fullFr8   )
   r   Tfigsizer+   r   r   rH   r   r   r   r      	   r   r   r   Nr?   r   r@   )r-   r   r.   rR   rS   r/   r)   r   r   r   r<   )r1   rK   r6   jZilistir   r(   r   r   r   test_constrained_layout8   s*    




ra   zconstrained_layout9.pngc                  C   s|   dt jd< 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 shareyFr8   r   T)r+   sharexshareyr,   r   r   r   r?   r   r@   zTest Suptitle   N)
r-   r   r3   r4   r)   r   r   
set_aspectr<   suptitlerC   r   r   r   test_constrained_layout9   s    

 



rg   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-   r3   r4   r   r!   ZarangeZlegendr5   r   r   r   test_constrained_layout10   s    
rk   zconstrained_layout11.pngc                  C   s   dt jd< 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}qn| j	||ddd | |d	 }t|d
d dS )z"Test for multiple nested gridspecsFr8   T)   r   r+   r[   r   r   rH   r   r^   r   r        Q@r   rA   aspectN
r-   r   r.   rR   rS   rT   r/   r   r)   r<   r1   gs0rL   Zgsl0r   r6   rK   r(   r   r   r   test_constrained_layout11   s    


rt   zconstrained_layout11rat.pngc                  C   s   dt jd< 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}q|| j	||ddd | |d
 }t|dd dS )z4Test for multiple nested gridspecs with width_ratiosFr8   T)rY   r   rm   r   r      )r.   Zwidth_ratiosr   )height_ratiosr^   r   r   rn   ro   Nrq   rr   r   r   r   test_constrained_layout11rat   s    


rw   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)ru      rm   ru   r   rH   Nr   r      r   r   )r   r]   r
   )r-   r.   rR   rS   r/   r   r   )r1   rs   ax1ax2r   r   r   r   test_constrained_layout12   s    r|   zconstrained_layout13.pngr9   c                  C   s\   dt jd< t jdddd\} }|jD ]$}t|dd}| j||dd	d
d q$| jddd dS )Test that padding works.Fr8   r   Tr*   r   r   r         4@r9   r   rA   rp   r:   gUUUUUU?)w_padh_padNr-   r   r3   r4   r)   r<   set_constrained_layout_padsrC   r   r   r   test_constrained_layout13  s    

r   zconstrained_layout14.pngc                  C   s`   dt jd< 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}   Fr8   r   Tr*   r   r   r   r~   r9   r   gUUUUUU?皙?r   r   ZhspaceZwspaceNr   rC   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-   r3   r4   r   r5   r   r   r   test_constrained_layout15"  s    
r   zconstrained_layout16.pngc                  C   s2   t jdd\} }t|dd | ddddg}dS )zTest ax.set_position.Tr*   r   r   r   皙?N)r-   r3   r   add_axesr1   r   r{   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   rH   r   r   r   r   Nr   r   )r-   r.   rR   rS   r/   r   )r1   rK   rz   r{   ax3Zax4r   r   r   test_constrained_layout173  s    r   c                  C   sV   t jdd\} }| }t| t|dd | j  t| j| jksRt	dS )z
Test twinxTr*   r,   r   N)
r-   r3   Ztwinxr   rV   rU   allget_positionextentsAssertionErrorr   r   r   r   test_constrained_layout18D  s    
r   c                  C   sj   t jdd\} }| }t| t|dd |d |d | j  t| j	| j	ksft
dS )z
Test twinyTr*   r,   r   r   N)r-   r3   Ztwinyr   r   rV   rU   r   r   r   r   r   r   r   r   test_constrained_layout19N  s    


r   c                  C   sl   t ddd} t | | dddf }t }|ddddg}|| | |ddddf }|| dS )z)Smoke test cl does not mess up added axesr   r]   Nr   r   r   )r!   Zlinspacehypotr-   r.   r   r$   r<   )Zgximgr1   r   Zmeshr   r   r   test_constrained_layout20Z  s    r   c                  C   sj   t jdd\} }| d | j  t| j}| d | j  t| j}tj	
|| dS )z>#11035: repeated calls to suptitle should not alter the layoutTr*   Z	Suptitle0Z	Suptitle1N)r-   r3   rf   rV   rU   r!   copyr   r   testingassert_allcloser1   r   Zextents0extents1r   r   r   test_constrained_layout21e  s    



r   c                  C   sd   t jdd\} }| j  t| j}| jddd | j  t| j}tj	
|| dS )zC#11035: suptitle should not be include in CL if manually positionedTr*   ZSuptitleri   r%   N)r-   r3   rV   rU   r!   r   r   r   rf   r   r   r   r   r   r   test_constrained_layout22t  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.   rI   rJ   rf   format)r`   r1   rK   subr   r   r   test_constrained_layout23  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...
    Fr8   r]   r   Tr*   r   Nr   r   )r   rA   r   r   ri   rE   )r   rA   rF   r   g?)r   rA   rF   r:   r   topr   leftright)r-   r   r3   r4   r)   r   r   r<   rC   r   r   r   test_colorbar_location  s    


 r   c                  C   s^   t jdddd\} }|d d | j  t|d  j}tj	j
|ddd	d
gdd d S )Nr   Tr*   r\   Fr   g|R?g\	b?g&:?g9?q?h㈵>)rtol)r-   r3   Zset_visiblerV   rU   r!   r   r   r   r   r   )r1   r6   r   r   r   r   test_hidden_axes  s    
 
 r   c               	   C   st  dD ]h} 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  |d |d q,|jddddd |j  | dk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   rE   r   Tr*   in)	directionr   g{Gz?)r   rF   rA   r:   r   r   gqq?g?r   )r   r   r   r   )r-   r3   r;   r4   tick_paramsr)   r<   r   ZxaxisZ	set_ticksyaxisr   r   r   rV   rU   r!   r   r   r   x0y0)rF   r1   r6   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]   TrZ   r   r   rb   rc   bothr   Zaxisr   r   r]   Zverticalr   Zorientationfoo)r-   r.   r3   r   set_major_formatterr   NullFormatterr   imshowr<   rf   r1   r6   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   TrZ   r   r   r   r   r   r   r   r   r]   Z
horizontalr   )r-   r.   rf   r3   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 ddddg | j  |d  }tj|ddgddgg t jdddd\} }|d ddddg |d 	tj
d	d	}| j||d d
 | j  |d  }tj|ddgddgg d S )Nr   r   Tr*   r   r   g333333?ri      )r   g)\(?)r-   r3   Zset_positionrV   rU   r   r!   r   r   r$   randomZrandr<   )r1   r6   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-   r3   re   r0   r   r   r   test_bboxtight  s    r   ztest_bbox.pngri   g      @r   c                  C   s   t jdd\} }|d d S r   r   r0   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j	 g}t
jj|d j|d jg|d jddd |d jdkst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@rx   rv   )r   r   gffffff?)r+   r[   Zgridspec_kwr   ZLabelg      g      ?)r6   r   r   )r   ZatolN)r-   r3   Zset_ylimr   Zalign_ylabelsrV   rU   r   rh   Zget_window_extentr!   r   r   r   r   )r1   r   rz   r{   Zafter_alignr   r   r   test_align_labels  s,    







  r   c                  C   s  t jdd\} }| j  || j }| d | d | j  || j }|j|jd kslt	|j
|j
d kst	t jdd\} }| j  || j }| jddd | jddd | j  || j }|j|jd kst	|j
|j
d ks
t	d S )	NTr*   ZBooZBooyg      $@ri   )r&   r   )r-   r3   rV   rU   Zget_tightbboxrW   Z	supxlabelZ	supylabelr   r   r   )r1   r   Zpos0posr   r   r   test_suplabels0  s$    





r   )r   F)r   )5Znumpyr!   rP   Zmatplotlib.testing.decoratorsr   Zmatplotlib.pyplotZpyplotr-   Zmatplotlib.gridspecrR   Zmatplotlib.transformsZ
transformsZmtransformsZ
matplotlibr   r   r   r)   r2   r7   r>   rD   rG   rN   rX   ra   rg   rk   rt   rw   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   <module>   s   



















 
  
 
 
 
"