a
    Fb5                     @   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 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d?d,d-Zd@d.d/Zd0d1 Zd2d3 Zd4d5 ZdAd6d7Zd8d9 ZdBd;d<Zd=d> ZdS )Cz%Pylab (matplotlib) support utilities.    )BytesIO)
b2a_base64)partialN)_pngxy)
flag_callsZTkAggZGTKAggZGTK3AggZGTK4AggZWXAggZQt4AggZQt5AggZQtAggZMacOSXZnbAggaggsvgpdfpsz)module://matplotlib_inline.backend_inlinemodule://ipympl.backend_nbagg)Ztkgtkgtk3gtk4wxZqt4Zqt5Zqt6qtosxnbaggZnotebookr   r   r	   r
   ZinlineZipymplZwidgetr   ZGTKZGTKCairor   Z	GTK3Cairor   Z	GTK4Cairor   ZWXr   ZCocoaAggr   c                  G   sl   ddl m} | s&| }dd |D S g }| D ]4}|j|}|du rTtd|  q.||jj q.|S d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).0Zfmr   r   6lib/python3.9/site-packages/IPython/core/pylabtools.py
<listcomp>W       zgetfigs.<locals>.<listcomp>Nz!Warning: figure %s not available.)	Zmatplotlib._pylab_helpersr   Zget_all_fig_managersfigsgetprintappendr   r   )Zfig_numsr   Zfig_managersr   Znumfr   r   r   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)ZsizexZsizeyr!   r   r   r   figsizec   s    r#   pngtightFc           
      K   s   | j s| jsdS | j}|dkr*|d }d}||  |  ||d}|| t }| jdu rpddlm	} ||  | jj
|fi | | }	|dkr|	d	}	n|rt|	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   r$   )formatZ	facecolorZ	edgecolordpibbox_inchesr   )FigureCanvasBaser   zutf-8ascii)Zaxeslinesr)   Zget_facecolorZget_edgecolorupdater   r   Zmatplotlib.backend_basesr+   print_figuregetvaluedecoder   )
figfmtr*   base64kwargsr)   kwZbytes_ior+   datar   r   r   r/   n   s0    

r/   c                 K   sZ   t | fddd|}|du r"dS t|\}}|d |d d}|rRt|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
    r&   Fr3   r4   Nr'   )widthZheightr,   )r/   r   r   r1   )r2   r4   r5   Zpngdatawhmetadatar   r   r   retina_figure   s    	r=   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r`|  d|j_z
|j}W n t	y|   Y n0 |  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!   Zmatplotlib.pyplotpyplotr"   r>   draw_if_interactiveZcalledZdrawZdraw_allAttributeError)fnamewherer6   r!   pltZis_interactiveZdasafe_execfiler   r   mpl_execfile   s    



z mpl_runner.<locals>.mpl_execfiler   )rF   rG   r   rE   r   
mpl_runner   s    rH   c              
   C   sF   z| j jj}W n, ty: } zt |W Y d}~nd}~0 0 |  dS )zreshow an nbagg figureN)r   ZmanagerreshowrA   NotImplementedError)r2   rI   er   r   r   _reshow_nbagg_figure   s
    rL   c                    s  ddl }ddlm  | jjd }| jjd }| jjd }| jjd }t|trT|h}t|} fdd	| jj D  |	 
 }|d
ks|dkr| jj}	|	 t h d}
||
}|rdddd	 |D  }dddd	 |
D  }td||f d|v r$| ttfddd| d|v s8d|v rT| ttfddi| d|v shd|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 )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   rM   r   r   r   	  r   z)select_figure_formats.<locals>.<listcomp>r   r   >   png2xjpegr	   r&   jpgr   r$   z%s,c                 S   s   g | ]}t |qS r   reprrP   r   r   r   r     r   c                 S   s   g | ]}t |qS r   rU   rP   r   r   r   r     r   z supported formats are: %s not %sr$   Tr8   r&   rQ   r4   rS   rR   r   r3   r	   )r!   Zmatplotlib.figurerN   Zdisplay_formatterZ
formatters
isinstancestrsetvaluesZget_backendlowerZipython_display_formatterZfor_typerL   
differencejoin
ValueErrorr   r/   r=   )shellZformatsr5   r!   Zsvg_formatterZpng_formatterZjpg_formatterZpdf_formatterZ
mplbackendZ	formatterZ	supportedZbadZbsZgsr   rM   r   select_figure_formats   sH    




r`   c                 C   s`   ddl }| r*| dkr*t|  }| dkrXd} n.|jd }t|d} |rX| |krX|} 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!   backendsZrcParamsOrigbackend2guir   )ZguiZ
gui_selectr!   rb   r   r   r   find_gui_and_backend,  s    
re   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   NTrb   )r?   F)	r!   r>   r"   r?   Zswitch_backendZshowZ	_needmainr   r@   )rb   r!   rD   r   r   r   activate_matplotlibW  s    


rf   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 *
r#   r   )displayrg   r    N)execr#   ZIPython.displayrg   r    )Zuser_nsZ
import_allsrg   r   r   r   import_pylabo  s    

rj   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()`r'   )
stacklevelr   )configure_inline_supportN)warningswarnDeprecationWarningZ matplotlib_inline.backend_inlinerl   )r_   rb   Zconfigure_inline_support_origr   r   r   rl     s    rl   )r$   r%   F)F)NN)T)__doc__ior   Zbinasciir   	functoolsr   rm   ZIPython.core.displayr   ZIPython.utils.decoratorsr   rc   dictziprZ   keysrd   r    r#   r/   r=   rH   rL   r`   re   rf   rj   rl   r   r   r   r   <module>   sj   
1
0
<
+
!