o
    :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   sd   |  ddg | jdd |s&| jd|d | jd|d | jd|d d S | 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   I/usr/lib/python3/dist-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sliceZ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   r0   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   r/   axsr   r   r   r   test_constrained_layout2,   s   
r5   zconstrained_layout3.pngc                  C   sZ   t jdddd\} }t|jD ]\}}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+   r1   	enumerater2   r'   colorbar)r/   r4   nnr   r&   r7   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   r7   shrinkNr+   r1   r2   r'   r9   r/   r4   r   r&   r   r   r   test_constrained_layout4B   s   
rA   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_gridspecr7   r>   locationNr?   r@   r   r   r   test_constrained_layout5L   s   

rD   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
 q'|d g }|D ]}| |}||g7 }t|d	d
}qC| 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?rB   r   r	   )r   r7   r>   rC   ZticksN)r+   r   r,   add_gridspecsubgridspecr-   r   r   r'   r9   r   ZMaxNLocator)r/   gsgslgsrZaxslr   Zaxsrr&   r   r   r   test_constrained_layout6[   s(   







rL   c                  C   s   t jtdd5 tjdd} tdd}tdd|d }tdd|d }|D ]}| | q(| 	  W d   dS 1 s?w   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)r/   rI   rJ   rK   r   r   r   test_constrained_layout7v   s   
"rU   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 ]-}| |||f }||g7 }t|dd}|dkrB|d |d	k rK|d |d q#q| |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   rF   r   r   r   r      	   r    r   Nr<   r   r=   )
r+   r,   rQ   rR   r-   r'   r   r   r   r9   )r/   rI   r4   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+   r1   r2   r'   r   r   
set_aspectr9   suptitler@   r   r   r   test_constrained_layout9   s   




re   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+   r1   r2   r   r    ZarangeZlegendr3   r   r   r   test_constrained_layout10   s   
ri   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	}q2| j||d
dd | |d }t|dd	 dS )z"Test for multiple nested gridspecsT)   r   r)   rX   r   r   rF   r   r[   r   r        Q@r   r>   aspectN	r+   r,   rQ   rR   rS   r-   r   r'   r9   r/   gs0rJ   Zgsl0r   r4   rI   r&   r   r   r   test_constrained_layout11   s   

rr   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}q9| j||ddd | |d }t|d
d dS )z4Test for multiple nested gridspecs with width_ratiosT)rV   r   rk   r   r      )r,   Zwidth_ratiosr   )height_ratiosr[   r   r   rl   rm   Nro   rp   r   r   r   test_constrained_layout11rat   s   

ru   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)rs      rk   rs   r   rF   Nr   r      r   r   )r   rZ   r
   )r+   r,   rQ   rR   r-   r   r   )r/   rq   ax1ax2r   r   r   r   test_constrained_layout12   s   rz   zconstrained_layout13.pngr6   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 s9w   Y  | jddd dS )Test that padding works.r   Tr(   r   r   r         4@r6   r   r>   rn   r7   zunexpected keyword argumentrM   r   )ZwpadZhpadNgUUUUUU?)w_padh_pad)	r+   r1   r2   r'   r9   rO   Zraises	TypeErrorset_constrained_layout_padsr@   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|   r6   r}   gUUUUUU?皙?r~   r   ZhspaceZwspaceN)r+   r1   r2   r'   r9   r   r@   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+   r1   r2   r   r3   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+   r1   r   add_axesr/   r   ry   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   rF   r   r   r   r   Nr   r   )r+   r,   rQ   rR   r-   r   )r/   rI   rx   ry   ax3Zax4r   r   r   test_constrained_layout17!  s   r   c                  C   sT   t jdd\} }| }t| t|dd |   t| j| jks(J dS )z
Test twinxTr(   r*   r   N)r+   r1   Ztwinxr   rT   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s2J dS )z
Test twinyTr(   r*   r   r\   N)	r+   r1   Ztwinyr   r   rT   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   rZ   N)r   r   r   r   r   )r    ZlinspaceZhypotr+   r,   r   r"   r9   )ZgxZimgr/   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+   r1   rd   rT   r    copyr   r   testingassert_allcloser/   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Suptitlerg   r#   N)
r+   r1   rT   r    r   r   r   rd   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)   clearZnumr   r   z
Suptitle{}N)ranger+   r,   rG   rH   rd   format)r^   r/   rI   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...
    FrE   rZ   r   Tr(   r\   Nr   r   )r   r>   r   r   rg   rB   )r   r>   rC   r   g?)r   r>   rC   r7   r   topr   leftright)r+   r   r1   r2   r'   r   r   r9   r@   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(   rY   Fr   )g|R?g\	b?g&:?g9?q?h㈵>)rtol)
r+   r1   Zset_visiblerT   r    r   r   r   r   r   )r/   r4   r   r   r   r   test_hidden_axes  s   

r   c               	   C   sn  dD ]} 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rU|jj	g  |jj
	g  |g  |g  q|jddddd |  | dv rt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   rB   r   Tr(   in)	directionr   g{Gz?)r   rC   r>   r7   r   gqq?g?r   )r   r   r   r   )r+   r1   r8   r2   tick_paramsr'   r9   r   ZxaxisZ	set_ticksyaxisr   r   r   rT   r    r   r   r   x0y0)rC   r/   r4   Zcbsr:   r   pccbr   r   r   test_colorbar_align  sF   



r   ztest_colorbars_no_overlapV.pngc                  C   s|   t jddd} | jddddd}|D ]$}|jt  |jddd	 |ddgd
dgg}| j	||dd q| 
d d S )N)r   rZ   TrW   r   r   r`   ra   bothr   Zaxisr   r   rZ   Zverticalr   Zorientationfoo)r+   r,   r1   r   set_major_formatterr   NullFormatterr   imshowr9   rd   r/   r4   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 ]$}|jt  |jdd	d
 |	ddgddgg}| j
||dd qd S )N)rZ   r   TrW   r   r   r   r   r   r   r   r   rZ   Z
horizontalr   )r+   r,   rd   r1   r   r   r   r   r   r   r9   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   rg      )r   g)\(?)r+   r1   Zset_positionrT   r   r    r   r   r"   ZrandomZrandr9   )r/   r4   ppr   r   r   r   test_manually_set_position  s   r   ztest_bboxtight.pngZbbox_inchesZtight)r   r   Zsavefig_kwargc                  C      t jdd\} }|d d S NTr(   r   r+   r1   rc   r.   r   r   r   test_bboxtight  s   r   ztest_bbox.pngrg   g      @r   c                  C   r   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snJ 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@rv   rt   )r   r   gffffff?)r)   rX   Zgridspec_kwr   ZLabelg      g      ?)r4   r   r   )r   ZatolN)r+   r1   Zset_ylimr   Zalign_ylabelsrT   r   rf   Zget_window_extentr    r   r   r   )r/   r   rx   ry   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s4J |j	|j	d ks>J t jdd\} }|   || j }| jddd | jddd |   || j }|j|jd ksvJ |j	|j	d ksJ d S )	NTr(   ZBooZBooyg      $@rg   )r$   r   )
r+   r1   rT   Zget_tightbboxZcanvasZget_rendererZ	supxlabelZ	supylabelr   r   )r/   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,   rG   r-   rT   )r/   rI   spr   r   r   test_gridspec_addressing5  s   r   )r   F)r   )6Znumpyr    rO   Zmatplotlib.testing.decoratorsr   Zmatplotlib.pyplotZpyplotr+   Zmatplotlib.gridspecrQ   Zmatplotlib.transformsZ
transformsZmtransformsZ
matplotlibr   r   r   r'   r0   r5   r;   rA   rD   rL   rU   r_   re   ri   rr   ru   rz   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    





	














 



"