o
    ˷e5                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	 ddl
mZ i dd	d
dddddddddddddddddddddddddd d d!d!d"d#d$d$d%Zeee e Zd
 ed&< ed'< ded(< ded)< ded*< ded+< ded< ded< ded< ed= ed= ed= ed = ed!= ed#= ed$= d,d- Zd.d/ ZdFd3d4ZdGd5d6Zd7d8 Zd9d: Zd;d< ZdHd=d>Zd?d@ ZdIdBdCZdDdE ZdS )Jz%Pylab (matplotlib) support utilities.    )BytesIO)
b2a_base64)partialN)_pngxy)
flag_callstkTkAgggtkGTKAgggtk3GTK3Agggtk4GTK4AggwxWXAggqt4Qt4Aggqt5Qt5Aggqt6QtAggqtosxMacOSXnbaggnbAggnotebookaggsvgpdfpsinlinez)module://matplotlib_inline.backend_inlinemodule://ipympl.backend_nbagg)ipymplwidgetGTKGTKCairo	GTK3Cairo	GTK4CairoWXCocoaAggc                  G   sh   ddl m} | s| }dd |D S g }| D ]}|j|}|du r*td|  q||jj q|S )a  Get a list of matplotlib figures by figure numbers.

    If no arguments are given, all available figures are returned.  If the
    argument list contains references to invalid figures, a warning is printed
    but the function continues pasting further figures.

    Parameters
    ----------
    figs : tuple
        A tuple of ints giving the figure numbers of the figures to return.
    r   )Gcfc                 S   s   g | ]}|j jqS  )canvasfigure).0fmr,   r,   N/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/core/pylabtools.py
<listcomp>W       zgetfigs.<locals>.<listcomp>Nz!Warning: figure %s not available.)	matplotlib._pylab_helpersr+   get_all_fig_managersfigsgetprintappendr-   r.   )fig_numsr+   fig_managersr6   numfr,   r,   r1   getfigsH   s   r>   c                 C   s   ddl }| |g|jd< dS )zSet the default figure size to be [sizex, sizey].

    This is just an easy to remember, convenience wrapper that sets::

      matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
    r   Nzfigure.figsize)
matplotlibrcParams)sizexsizeyr?   r,   r,   r1   figsizec   s   rC   pngtightFc           
      K   s   | j s| jsdS | j}|dkr|d }d}||  |  ||d}|| t }| jdu r8ddlm	} ||  | jj
|fi | | }	|dkrQ|	d	}	|	S |rZt|	d
}	|	S )a  Print a figure to an image, and return the resulting file data

    Returned data will be bytes unless ``fmt='svg'``,
    in which case it will be unicode.

    Any keyword args are passed to fig.canvas.print_figure,
    such as ``quality`` or ``bbox_inches``.

    If `base64` is True, return base64-encoded str instead of raw bytes
    for binary-encoded image formats

    .. versionadded:: 7.29
        base64 argument
    Nretina   rD   )format	facecolor	edgecolordpibbox_inchesr   )FigureCanvasBaser   zutf-8ascii)axeslinesrK   get_facecolorget_edgecolorupdater   r-   matplotlib.backend_basesrM   print_figuregetvaluedecoder   )
figfmtrL   base64kwargsrK   kwbytes_iorM   datar,   r,   r1   rU   n   s2   


rU   c                 K   sZ   t | fddd|}|du rdS t|\}}|d |d d}|r)t|d}||fS )zformat a figure as a pixel-doubled (retina) PNG

    If `base64` is True, return base64-encoded str instead of raw bytes
    for binary-encoded image formats

    .. versionadded:: 7.29
        base64 argument
    rF   FrY   rZ   NrG   )widthheightrN   )rU   r   r   rW   )rX   rZ   r[   pngdatawhmetadatar,   r,   r1   retina_figure   s   	rf   c                    s    fdd}|S )a\  Factory to return a matplotlib-enabled runner for %run.

    Parameters
    ----------
    safe_execfile : function
        This must be a function with the same interface as the
        :meth:`safe_execfile` method of IPython.

    Returns
    -------
    A function suitable for use as the ``runner`` argument of the %run magic
    function.
    c                    s   ddl }ddlm} |jd }|d  | g|R i | || |jjr0|  d|j_z|j}W n
 t	y?   Y dS w |  dS )zmatplotlib-aware wrapper around safe_execfile.

        Its interface is identical to that of the :func:`execfile` builtin.

        This is ultimately a call to execfile(), but wrapped in safeties to
        properly handle interactive rendering.r   NinteractiveF)
r?   matplotlib.pyplotpyplotr@   rg   draw_if_interactivecalleddrawdraw_allAttributeError)fnamewherer\   r?   pltis_interactivedasafe_execfiler,   r1   mpl_execfile   s   




z mpl_runner.<locals>.mpl_execfiler,   )ru   rv   r,   rt   r1   
mpl_runner   s   rw   c              
   C   s:   z| j jj}W n ty } zt |d}~ww |  dS )zreshow an nbagg figureN)r-   managerreshowrn   NotImplementedError)rX   ry   er,   r,   r1   _reshow_nbagg_figure   s   
r|   c                    s  ddl }ddlm  | jjd }| jjd }| jjd }| jjd }t|tr*|h}t|} fdd	| jj D  |	 
 }|d
ksI|dkrS| jj}	|	 t h d}
||
}|r~dddd	 |D  }dddd	 |
D  }td||f d|v r| ttfddd| d|v sd|v r| ttfddi| d|v sd|v r| ttfddd| d|v r| ttfddi| d|v r| ttfddd| dS dS )a_  Select figure formats for the inline backend.

    Parameters
    ----------
    shell : InteractiveShell
        The main IPython instance.
    formats : str or set
        One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
    **kwargs : any
        Extra keyword arguments to be passed to fig.canvas.print_figure.
    r   NFigurezimage/svg+xmlz	image/pngz
image/jpegzapplication/pdfc                    s   g | ]}|  d qS )N)popr/   r=   r}   r,   r1   r2   	  s    z)select_figure_formats.<locals>.<listcomp>r   r"   >   jpgr   rD   r   jpegpng2xrF   z%s,c                 S      g | ]}t |qS r,   reprr   r,   r,   r1   r2     r3   c                 S   r   r,   r   r   r,   r,   r1   r2     r3   z supported formats are: %s not %srD   Tr_   rF   r   rZ   r   r   r   rY   r   )r?   matplotlib.figurer~   display_formatter
formatters
isinstancestrsetvaluesget_backendloweripython_display_formatterfor_typer|   
differencejoin
ValueErrorr   rU   rf   )shellformatsr[   r?   svg_formatterpng_formatterjpg_formatterpdf_formatter
mplbackend	formatter	supportedbadbsgsr,   r}   r1   select_figure_formats   sJ   

r   c                 C   sf   ddl }| r| dkrt|  }| dkrd} | |fS |jd }t|d} |r/| |kr/|} t|  }| |fS )a  Given a gui string return the gui and mpl backend.

    Parameters
    ----------
    gui : str
        Can be one of ('tk','gtk','wx','qt','qt4','inline','agg').
    gui_select : str
        Can be one of ('tk','gtk','wx','qt','qt4','inline').
        This is any gui already selected by the shell.

    Returns
    -------
    A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
    'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
    r   Nautor   backend)r?   backendsrcParamsOrigbackend2guir7   )gui
gui_selectr?   r   r,   r,   r1   find_gui_and_backend,  s   
r   c                 C   sJ   ddl }|d | |jd< ddl m} ||  d|j_t|j|_dS )z7Activate the given backend and set interactive to True.r   NTr   )ri   F)	r?   rg   r@   ri   switch_backendshow	_needmainr   rj   )r   r?   rq   r,   r,   r1   activate_matplotlibW  s   


r   Tc                 C   sH   d}t ||  |rd}t ||  t| d< ddlm} || d< t| d< dS )	zPopulate the namespace with pylab-related values.

    Imports matplotlib, pylab, numpy, and everything from pylab and numpy.

    Also imports a few names from IPython (figsize, display, getfigs)

    zbimport numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
z3from matplotlib.pylab import *
from numpy import *
rC   r   )displayr   r>   N)execrC   IPython.displayr   r>   )user_ns
import_allsr   r,   r,   r1   import_pylabo  s   

r   c                 C   s*   t jdtdd ddlm} || | dS )a
  
    .. deprecated:: 7.23

        use `matplotlib_inline.backend_inline.configure_inline_support()`

    Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance
    backend : matplotlib backend
    z`configure_inline_support` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.configure_inline_support()`rG   )
stacklevelr   )configure_inline_supportN)warningswarnDeprecationWarning matplotlib_inline.backend_inliner   )r   r   configure_inline_support_origr,   r,   r1   r     s   r   )rD   rE   F)F)NN)T)__doc__ior   binasciir   	functoolsr   r   IPython.core.displayr   IPython.utils.decoratorsr   r   dictzipr   keysr   r>   rC   rU   rf   rw   r|   r   r   r   r   r   r,   r,   r,   r1   <module>   s   	


10

<+
!