U
    L?h                     @   s@   d dl mZ d dlm  m  mZ ddgiZG dd dZdS )    )import_moduleN)PlotGrid
matplotlibc                   @   s`   e Zd ZdZdddddZdd Zed	d
 Zedd Zdd Z	dd Z
dd Zdd ZdS )r   a	  This class helps to plot subplots from already created SymPy plots
    in a single figure.

    Examples
    ========

    .. plot::
       :context: close-figs
       :format: doctest
       :include-source: True

        >>> from sympy import symbols
        >>> from sympy.plotting import plot, plot3d, PlotGrid
        >>> x, y = symbols('x, y')
        >>> p1 = plot(x, x**2, x**3, (x, -5, 5))
        >>> p2 = plot((x**2, (x, -6, 6)), (x, (x, -5, 5)))
        >>> p3 = plot(x**3, (x, -5, 5))
        >>> p4 = plot3d(x*y, (x, -5, 5), (y, -5, 5))

    Plotting vertically in a single line:

    .. plot::
       :context: close-figs
       :format: doctest
       :include-source: True

        >>> PlotGrid(2, 1, p1, p2)
        PlotGrid object containing:
        Plot[0]:Plot object containing:
        [0]: cartesian line: x for x over (-5.0, 5.0)
        [1]: cartesian line: x**2 for x over (-5.0, 5.0)
        [2]: cartesian line: x**3 for x over (-5.0, 5.0)
        Plot[1]:Plot object containing:
        [0]: cartesian line: x**2 for x over (-6.0, 6.0)
        [1]: cartesian line: x for x over (-5.0, 5.0)

    Plotting horizontally in a single line:

    .. plot::
       :context: close-figs
       :format: doctest
       :include-source: True

        >>> PlotGrid(1, 3, p2, p3, p4)
        PlotGrid object containing:
        Plot[0]:Plot object containing:
        [0]: cartesian line: x**2 for x over (-6.0, 6.0)
        [1]: cartesian line: x for x over (-5.0, 5.0)
        Plot[1]:Plot object containing:
        [0]: cartesian line: x**3 for x over (-5.0, 5.0)
        Plot[2]:Plot object containing:
        [0]: cartesian surface: x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0)

    Plotting in a grid form:

    .. plot::
       :context: close-figs
       :format: doctest
       :include-source: True

        >>> PlotGrid(2, 2, p1, p2, p3, p4)
        PlotGrid object containing:
        Plot[0]:Plot object containing:
        [0]: cartesian line: x for x over (-5.0, 5.0)
        [1]: cartesian line: x**2 for x over (-5.0, 5.0)
        [2]: cartesian line: x**3 for x over (-5.0, 5.0)
        Plot[1]:Plot object containing:
        [0]: cartesian line: x**2 for x over (-6.0, 6.0)
        [1]: cartesian line: x for x over (-5.0, 5.0)
        Plot[2]:Plot object containing:
        [0]: cartesian line: x**3 for x over (-5.0, 5.0)
        Plot[3]:Plot object containing:
        [0]: cartesian surface: x*y for x over (-5.0, 5.0) and y over (-5.0, 5.0)

    TN)showsizec                O   sp   t dddddgidtfd| _|| _|| _g | _d| _|| _|D ]}| j|j q@|| _	|rl| jrl| 
  dS )	a  
        Parameters
        ==========

        nrows :
            The number of rows that should be in the grid of the
            required subplot.
        ncolumns :
            The number of columns that should be in the grid
            of the required subplot.

        nrows and ncolumns together define the required grid.

        Arguments
        =========

        A list of predefined plot objects entered in a row-wise sequence
        i.e. plot objects which are to be in the top row of the required
        grid are written first, then the second row objects and so on

        Keyword arguments
        =================

        show : Boolean
            The default value is set to ``True``. Set show to ``False`` and
            the function will not display the subplot. The returned instance
            of the ``PlotGrid`` class can then be used to save or display the
            plot by calling the ``save()`` and ``show()`` methods
            respectively.
        size : (float, float), optional
            A tuple in the form (width, height) in inches to specify the size of
            the overall figure. The default value is set to ``None``, meaning
            the size will be set by the default backend.
        r   fromlistpyplotcmcollectionsz1.1.0)Zimport_kwargsZmin_module_versionZcatchN)r   RuntimeErrorr   nrowsncolumns_series_figargsappendr   r   )selfr   r   r   r   r   kwargsarg r   I/var/www/html/venv/lib/python3.8/site-packages/sympy/plotting/plotgrid.py__init__\   s    # 
zPlotGrid.__init__c           
      C   s   | j j| j| j}i }d}t| jD ]@}t| jD ]0}|t| jk r\| j| ||||f < |d7 }q4q&| jsri nd| ji}| j j	j
f || _| D ]T\}}t|jdkr|jd jrddini }| jj|f|}	| j|_|	|_|  qd S )Nr      ZfigsizeZ
projectionZ3d)r   ZgridspecZGridSpecr   r   rangelenr   r   r   Zfigurer   itemsr   Zis_3DZadd_subplotZ_plotgrid_figZ_plotgrid_axZprocess_series)
r   gsmappingcijkwspecpZcur_axr   r   r   _create_figure   s(    

zPlotGrid._create_figurec                 C   s   | j s|   | j S N)r   r$   r   r   r   r   fig   s    zPlotGrid.figc                 C   s   | S r%   r   r&   r   r   r   _backend   s    zPlotGrid._backendc                 C   s   | j j| j d S r%   )r   r   closer'   r&   r   r   r   r)      s    zPlotGrid.closec                 C   s*   t jr| j  | jj  n|   d S r%   )base_backendZ_showr'   Ztight_layoutr   r   r   r)   r&   r   r   r   r      s    
zPlotGrid.showc                 C   s   | j | d S r%   )r'   Zsavefig)r   pathr   r   r   save   s    zPlotGrid.savec                 C   s"   dd t | jD }dd| S )Nc                 S   s    g | ]\}}d | t | qS )z	Plot[%d]:)str).0r   Zplotr   r   r   
<listcomp>   s   z$PlotGrid.__str__.<locals>.<listcomp>zPlotGrid object containing:

)	enumerater   join)r   Z	plot_strsr   r   r   __str__   s    zPlotGrid.__str__)__name__
__module____qualname____doc__r   r$   propertyr'   r(   r)   r   r,   r3   r   r   r   r   r      s   K1

r   )Zsympy.externalr   Z$sympy.plotting.backends.base_backendZplottingbackendsr*   Z__doctest_requires__r   r   r   r   r   <module>   s
   	 