U
    1e6                     @   sb  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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dAd/d0Zd1d2 Zd3d4 Zd5d6 ZdBd7d8Zd9d: ZdCd<d=Zd>d? ZdS )Dz%Pylab (matplotlib) support utilities.    )BytesIO)
b2a_base64)partialN)_pngxy)
flag_callsZTkAggZGTKAggZGTK3AggZGTK4AggZWXAggZQt4AggZQt5AggZQtAggZMacOSXZnbAggZWebAgg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webaggZnotebookr   r   r	   r
   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k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   ;/tmp/pip-unpacked-wheel-3hxk2k58/IPython/core/pylabtools.py
<listcomp>X   s     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   numfr   r   r   getfigsI   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   figsized   s    r$   pngtightFc           
      K   s   | j s| jsdS | j}|dkr*|d }d}||  |  ||d}|| t }| jdkrpddlm	} ||  | jj
|f| | }	|dkr|	d	}	n|rt|	d
d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-8Fnewline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   r2   o   s0    

r2   c                 K   s^   t | fddd|}|dkr"dS t|\}}|d |d d}|rVt|dd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r6   r7   Nr(   )widthheightr-   r/   )r2   r   r   r4   )r5   r7   r8   Zpngdatawhmetadatar   r   r   retina_figure   s    	rA   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  | f|| || |jjrZ|  d|j_z
|j}W n t	k
rx   Y nX |  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#   rB   draw_if_interactivecalledZdrawZdraw_allAttributeError)fnamewherer9   r"   pltZis_interactivedasafe_execfiler   r   mpl_execfile   s    



z mpl_runner.<locals>.mpl_execfiler   )rL   rM   r   rK   r   
mpl_runner   s    rN   c              
   C   sD   z| j jj}W n* tk
r8 } zt |W 5 d}~X Y nX |  dS )zreshow an nbagg figureN)r   managerreshowrF   NotImplementedError)r5   rP   er   r   r   _reshow_nbagg_figure   s
    rS   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 dddddddh}
||
}|rdddd	 |D  }dddd	 |
D  }td||f d|kr0| ttfddd| d|ksDd|kr`| ttfddi| d|kstd|kr| ttfddd| d|kr| ttfddi| d|k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    rT   r   r   r   
  s     z)select_figure_formats.<locals>.<listcomp>r   r   r%   Zpng2xr'   ZjpgZjpegr   r	   z%s,c                 S   s   g | ]}t |qS r   reprrW   r   r   r   r     s     c                 S   s   g | ]}t |qS r   rY   rW   r   r   r   r     s     z supported formats are: %s not %sTr;   r7   r6   )r"   Zmatplotlib.figurerU   Zdisplay_formatter
formatters
isinstancestrsetvaluesget_backendlowerZipython_display_formatterZfor_typerS   
differencejoin
ValueErrorr   r2   rA   )shellformatsr8   r"   Zsvg_formatterZpng_formatterZjpg_formatterZpdf_formatterZ
mplbackend	formatter	supportedbadbsZgsr   rT   r   select_figure_formats   sN    


  

 rk   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   )guiZ
gui_selectr"   rm   r   r   r   find_gui_and_backend-  s    
rq   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   NTrm   )rC   F)	r"   rB   r#   rC   Zswitch_backendshowZ	_needmainr   rD   )rm   r"   rI   r   r   r   activate_matplotlibX  s    


rs   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   )displayrt   r!   N)execr$   ZIPython.displayrt   r!   )Zuser_nsZ
import_allsrt   r   r   r   import_pylabp  s    

rw   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_inlinery   )re   rm   Zconfigure_inline_support_origr   r   r   ry     s    ry   )r%   r&   F)F)NN)T)__doc__ior   binasciir   	functoolsr   rz   ZIPython.core.displayr   ZIPython.utils.decoratorsr   rn   dictzipr_   keysro   r!   r$   r2   rA   rN   rS   rk   rq   rs   rw   ry   r   r   r   r   <module>   sl   
1
0
<
+
!