a
    Sicw*                     @   s   d Z ddlmZ ddlZddlmZ ddlmZ ddl	m
Z
mZ G dd dZG dd	 d	eZG d
d deZG dd deZdS )a  
Classes to layout elements in a `.Figure`.

Figures have a ``layout_engine`` property that holds a subclass of
`~.LayoutEngine` defined here (or *None* for no layout).  At draw time
``figure.get_layout_engine().execute()`` is called, the goal of which is
usually to rearrange Axes on the figure to produce a pleasing layout. This is
like a ``draw`` callback, however when printing we disable the layout engine
for the final draw and it is useful to know the layout engine while the figure
is being created, in particular to deal with colorbars.

Matplotlib supplies two layout engines, `.TightLayoutEngine` and
`.ConstrainedLayoutEngine`.  Third parties can create their own layout engine
by subclassing `.LayoutEngine`.
    )nullcontextN)do_constrained_layout)get_subplotspec_listget_tight_layout_figurec                       sX   e Zd ZdZdZdZ fddZdd Zedd Z	ed	d
 Z
dd Zdd Z  ZS )LayoutEnginea  
    Base class for Matplotlib layout engines.

    A layout engine can be passed to a figure at instantiation or at any time
    with `~.figure.Figure.set_layout_engine`.  Once attached to a figure, the
    layout engine ``execute`` function is called at draw time by
    `~.figure.Figure.draw`, providing a special draw-time hook.

    .. note::

       However, note that layout engines affect the creation of colorbars, so
       `~.figure.Figure.set_layout_engine` should be called before any
       colorbars are created.

    Currently, there are two properties of `LayoutEngine` classes that are
    consulted while manipulating the figure:

    - ``engine.colorbar_gridspec`` tells `.Figure.colorbar` whether to make the
       axes using the gridspec method (see `.colorbar.make_axes_gridspec`) or
       not (see `.colorbar.make_axes`);
    - ``engine.adjust_compatible`` stops `.Figure.subplots_adjust` from being
        run if it is not compatible with the layout engine.

    To implement a custom `LayoutEngine`:

    1. override ``_adjust_compatible`` and ``_colorbar_gridspec``
    2. override `LayoutEngine.set` to update *self._params*
    3. override `LayoutEngine.execute` with your implementation

    Nc                    s   t  jf i | i | _d S N)super__init___paramsselfkwargs	__class__ T/var/www/html/django/DPS/env/lib/python3.9/site-packages/matplotlib/layout_engine.pyr	   >   s    zLayoutEngine.__init__c                 K   s   t d S r   NotImplementedErrorr   r   r   r   setB   s    zLayoutEngine.setc                 C   s   | j du rt| j S )zc
        Return a boolean if the layout engine creates colorbars using a
        gridspec.
        N)_colorbar_gridspecr   r   r   r   r   colorbar_gridspecE   s    
zLayoutEngine.colorbar_gridspecc                 C   s   | j du rt| j S )zn
        Return a boolean if the layout engine is compatible with
        `~.Figure.subplots_adjust`.
        N)_adjust_compatibler   r   r   r   r   adjust_compatibleO   s    
zLayoutEngine.adjust_compatiblec                 C   s
   t | jS )zF
        Return copy of the parameters for the layout engine.
        )dictr
   r   r   r   r   getY   s    zLayoutEngine.getc                 C   s   t dS )zB
        Execute the layout on the figure given by *fig*.
        Nr   r   figr   r   r   execute_   s    zLayoutEngine.execute)__name__
__module____qualname____doc__r   r   r	   r   propertyr   r   r   r   __classcell__r   r   r   r   r      s   
	
	r   c                       s(   e Zd ZdZ fddZdd Z  ZS )PlaceHolderLayoutEnginea  
    This layout engine does not adjust the figure layout at all.

    The purpose of this `.LayoutEngine` is to act as a place holder when the
    user removes a layout engine to ensure an incompatible `.LayoutEngine` can
    not be set later.

    Parameters
    ----------
    adjust_compatible, colorbar_gridspec : bool
        Allow the PlaceHolderLayoutEngine to mirror the behavior of whatever
        layout engine it is replacing.

    c                    s"   || _ || _t jf i | d S r   )r   r   r   r	   )r   r   r   r   r   r   r   r	   v   s    z PlaceHolderLayoutEngine.__init__c                 C   s   d S r   r   r   r   r   r   r   {   s    zPlaceHolderLayoutEngine.execute)r   r    r!   r"   r	   r   r$   r   r   r   r   r%   g   s   r%   c                       sP   e Zd ZdZdZdZddddd fdd
Zd	d
 ZdddddddZ  Z	S )TightLayoutEnginez
    Implements the ``tight_layout`` geometry management.  See
    :doc:`/tutorials/intermediate/tight_layout_guide` for details.
    TgHzG?Nr   r      r(   padh_padw_padrectc                   s<   t  jf i | dD ]}d| j|< q| j||||d dS )a'  
        Initialize tight_layout engine.

        Parameters
        ----------
        pad : float, 1.08
            Padding between the figure edge and the edges of subplots, as a
            fraction of the font size.
        h_pad, w_pad : float
            Padding (height/width) between edges of adjacent subplots.
            Defaults to *pad*.
        rect : tuple (left, bottom, right, top), default: (0, 0, 1, 1).
            rectangle in normalized figure coordinates that the subplots
            (including labels) will fit into.
        r)   N)r   r	   r
   r   )r   r*   r+   r,   r-   r   tdr   r   r   r	      s    zTightLayoutEngine.__init__c                 C   s   | j }t|j}d|v r"td | }t|dt : t||j|||d |d |d |d d}W d   n1 sx0    Y  |r|j	f i | dS )	ad  
        Execute tight_layout.

        This decides the subplot parameters given the padding that
        will allow the axes labels to not be covered by other labels
        and axes.

        Parameters
        ----------
        fig : `.Figure` to perform layout on.

        See also: `.figure.Figure.tight_layout` and `.pyplot.tight_layout`.
        NzcThis figure includes Axes that are not compatible with tight_layout, so results might be incorrect._draw_disabledr*   r+   r,   r-   r)   )
r
   r   axes_apiwarn_external_get_renderergetattrr   r   subplots_adjust)r   r   infosubplotspec_listrendererr   r   r   r   r      s    


$zTightLayoutEngine.execute)r*   r,   r+   r-   c                C   s0   | j jD ]"}t | d urt | | j|< qd S r   r   __kwdefaults__localsr
   )r   r*   r,   r+   r-   r.   r   r   r   r      s    zTightLayoutEngine.set
r   r    r!   r"   r   r   r	   r   r   r$   r   r   r   r   r&      s   r&   c                       sV   e Zd ZdZdZdZddddddd fdd
Zdd	 Zdddddd
ddZ  Z	S )ConstrainedLayoutEnginez
    Implements the ``constrained_layout`` geometry management.  See
    :doc:`/tutorials/intermediate/constrainedlayout_guide` for details.
    FNr'   )r+   r,   hspacewspacer-   compressc                   s\   t  jf i | | jtjd tjd tjd tjd dd | j|||||d || _dS )a  
        Initialize ``constrained_layout`` settings.

        Parameters
        ----------
        h_pad, w_pad : float
            Padding around the axes elements in figure-normalized units.
            Default to :rc:`figure.constrained_layout.h_pad` and
            :rc:`figure.constrained_layout.w_pad`.
        hspace, wspace : float
            Fraction of the figure to dedicate to space between the
            axes.  These are evenly spread between the gaps between the axes.
            A value of 0.2 for a three-column layout would have a space
            of 0.1 of the figure width between each column.
            If h/wspace < h/w_pad, then the pads are used instead.
            Default to :rc:`figure.constrained_layout.hspace` and
            :rc:`figure.constrained_layout.wspace`.
        rect : tuple of 4 floats
            Rectangle in figure coordinates to perform constrained layout in
            (left, bottom, width, height), each from 0-1.
        compress : bool
            Whether to shift Axes so that white space in between them is
            removed. This is useful for simple grids of fixed-aspect Axes (e.g.
            a grid of images).  See :ref:`compressed_layout`.
        zfigure.constrained_layout.w_padzfigure.constrained_layout.h_padz figure.constrained_layout.wspacez figure.constrained_layout.hspacer'   )r,   r+   r?   r>   r-   N)r   r	   r   mplrcParams	_compress)r   r+   r,   r>   r?   r-   r@   r   r   r   r   r	      s    z ConstrainedLayoutEngine.__init__c              	   C   sR   |  \}}| jd | }| jd | }t|||| jd | jd | jd | jdS )z
        Perform constrained_layout and move and resize axes accordingly.

        Parameters
        ----------
        fig : `.Figure` to perform layout on.
        r,   r+   r?   r>   r-   )r,   r+   r?   r>   r-   r@   )get_size_inchesr
   r   rC   )r   r   widthheightr,   r+   r   r   r   r      s    zConstrainedLayoutEngine.execute)r+   r,   r>   r?   r-   c                C   s0   | j jD ]"}t | durt | | j|< qdS )a  
        Set the pads for constrained_layout.

        Parameters
        ----------
        h_pad, w_pad : float
            Padding around the axes elements in figure-normalized units.
            Default to :rc:`figure.constrained_layout.h_pad` and
            :rc:`figure.constrained_layout.w_pad`.
        hspace, wspace : float
            Fraction of the figure to dedicate to space between the
            axes.  These are evenly spread between the gaps between the axes.
            A value of 0.2 for a three-column layout would have a space
            of 0.1 of the figure width between each column.
            If h/wspace < h/w_pad, then the pads are used instead.
            Default to :rc:`figure.constrained_layout.hspace` and
            :rc:`figure.constrained_layout.wspace`.
        rect : tuple of 4 floats
            Rectangle in figure coordinates to perform constrained layout in
            (left, bottom, width, height), each from 0-1.
        Nr9   )r   r+   r,   r>   r?   r-   r.   r   r   r   r     s    zConstrainedLayoutEngine.setr<   r   r   r   r   r=      s   (r=   )r"   
contextlibr   
matplotlibrA   matplotlib._apir1   Zmatplotlib._constrained_layoutr   Zmatplotlib._tight_layoutr   r   r   r%   r&   r=   r   r   r   r   <module>   s   LB