3
ƽhw                 @   s0   d Z ddlmZmZmZ dddZd	ddZdS )
zE
Helper module for the *bbox_inches* parameter in `.Figure.savefig`.
    )BboxTransformedBboxAffine2DNc                s  j jj jj jd g g t xljD ]b}j|j	  |j
ddj }|j|fdd d|jkrj|j n
j ddd|_q<W  fdd	}|dkrʈj}t j|}|j }t||}tjd
d
|j|j_|j|j }	}
j j| j j|  }}tj|	 |
 ||j_jj  tj|_ jj|	| |
| j j| j j|  |S )aO  
    Temporarily adjust the figure so that only the specified area
    (bbox_inches) is saved.

    It modifies fig.bbox, fig.bbox_inches,
    fig.transFigure._boxout, and fig.patch.  While the figure size
    changes, the scale of the original figure is conserved.  A
    function which restores the original values are returned.
    F)originalc             S   s   |S )N )ar_posr   r   ;/tmp/pip-build-7iwl8md4/matplotlib/matplotlib/tight_bbox.py<lambda>   s    zadjust_bbox.<locals>.<lambda>apply_aspectNc             S   s   d S )Nr   )posr   r   r
   r   %   s    c                 sz   x:t jD ](\} }}| j| |kr2| `q|| _qW __j  j_jj	  j
jdddd d S )Nr      )zipaxesset_axes_locatorr   bboxbbox_inchesset_tight_layouttransFigure_boxout
invalidatepatch
set_bounds)axlocZaspect)r   figlocator_list
old_aspectorigBboxorigBboxInchesorig_tight_layoutsentinelr   r
   restore_bbox'   s    



z!adjust_bbox.<locals>.restore_bboxr   )N)r   r   Zget_tight_layoutr   r   r   objectr   appendZget_axes_locatorget_positionfrozenr   __dict__r   Zdpir   Zscaler   r   Zfrom_boundswidthheightx0y0r   r   r   )r   r   	fixed_dpir   Zcurrent_posr#   trZ	dpi_scaleZ_bboxr+   r,   Zw1h1r   )r   r   r   r   r   r    r!   r"   r
   adjust_bbox   s@    






r0   c             C   s"   |\}}|  t | ||}||fS )z
    A function that needs to be called when figure dpi changes during the
    drawing (e.g., rasterizing).  It recovers the bbox and re-adjust it with
    the new dpi.
    )r0   )r   Zbbox_inches_restorer-   r   r#   r   r   r   r
   process_figure_for_rasterizingM   s    r1   )N)N)__doc__Zmatplotlib.transformsr   r   r   r0   r1   r   r   r   r
   <module>   s   
E