/ lib / IPython / core / pylabtools.pyc
pylabtools.pyc
  1  o

  2  .��c�5�@s�dZddlmZddlmZddlmZddlZddlm	Z	ddl
  3  mZidd	�d
  4  d�dd
�dd�dd�dd�dd�dd�dd�dd�dd�dd�dd�d d �d!d!�d"d"�d#d#�d$d%d%d&��Ze
ee��e����Zd
  5  ed'<ed(<ded)<ded*<ded+<ded,<ded<ded<ded<ed=ed =ed!=ed"=ed#=ed$=ed%=d-d.�Zd/d0�ZdGd4d5�ZdHd6d7�Zd8d9�Zd:d;�Zd<d=�ZdId>d?�Zd@dA�ZdJdCdD�ZdEdF�ZdS)Kz%Pylab (matplotlib) support utilities.�)�BytesIO)�
  6  b2a_base64)�partialN)�_pngxy)�
  7  flag_calls�tkZTkAgg�gtkZGTKAgg�gtk3ZGTK3Agg�gtk4ZGTK4Agg�wxZWXAgg�qt4�Qt4Agg�qt5�Qt5Agg�qt6ZQtAgg�qt�osx�MacOSX�nbaggZnbAgg�webaggZWebAgg�notebook�agg�svg�pdf�psz)module://matplotlib_inline.backend_inline�module://ipympl.backend_nbagg)�inline�ipympl�widgetZGTKZGTKCairoZ	GTK3CairoZ	GTK4Cairo�WXZCocoaAggcGshddlm}|s|��}dd�|D�Sg}|D]}|j�|�}|dur*td|�q|�|jj�q|S)a�Get a list of matplotlib figures by figure numbers.
  8  
  9      If no arguments are given, all available figures are returned.  If the
 10      argument list contains references to invalid figures, a warning is printed
 11      but the function continues pasting further figures.
 12  
 13      Parameters
 14      ----------
 15      figs : tuple
 16          A tuple of ints giving the figure numbers of the figures to return.
 17      r)�GcfcSsg|]}|jj�qS�)�canvas�figure)�.0�fmr!r!��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\pylabtools.py�
 18  <listcomp>X�zgetfigs.<locals>.<listcomp>Nz!Warning: figure %s not available.)	�matplotlib._pylab_helpersr �get_all_fig_managers�figs�get�print�appendr"r#)Zfig_numsr Zfig_managersr+�num�fr!r!r&�getfigsIsr1cCsddl}||g|jd<dS)z�Set the default figure size to be [sizex, sizey].
 19  
 20      This is just an easy to remember, convenience wrapper that sets::
 21  
 22        matplotlib.rcParams['figure.figsize'] = [sizex, sizey]
 23      rNzfigure.figsize)�
 24  matplotlib�rcParams)ZsizexZsizeyr2r!r!r&�figsizedsr4�png�tightFc
 25  Ks�|js|jsdS|j}|dkr|d}d}||��|��||d�}|�|�t�}|jdur8ddlm	}||�|jj
 26  |fi|��|��}	|dkrQ|	�d	�}	|	S|rZt
|	��d
 27  �}	|	S)a�Print a figure to an image, and return the resulting file data
 28  
 29      Returned data will be bytes unless ``fmt='svg'``,
 30      in which case it will be unicode.
 31  
 32      Any keyword args are passed to fig.canvas.print_figure,
 33      such as ``quality`` or ``bbox_inches``.
 34  
 35      If `base64` is True, return base64-encoded str instead of raw bytes
 36      for binary-encoded image formats
 37  
 38      .. versionadded:: 7.29
 39          base64 argument
 40      N�retina�r5)�format�	facecolorZ	edgecolor�dpi�bbox_inchesr)�FigureCanvasBaserzutf-8�ascii)�axes�linesr;�
get_facecolorZ
get_edgecolor�updaterr"�matplotlib.backend_basesr=�print_figure�getvalue�decoder)
 41  �fig�fmtr<�base64�kwargsr;�kwZbytes_ior=�datar!r!r&rDos2�
 42  
 43  
 44  �rDcKsZt|fddd�|��}|durdSt|�\}}|d|dd�}|r)t|��d�}||fS)z�format a figure as a pixel-doubled (retina) PNG
 45  
 46      If `base64` is True, return base64-encoded str instead of raw bytes
 47      for binary-encoded image formats
 48  
 49      .. versionadded:: 7.29
 50          base64 argument
 51      r7F�rHrINr8)�width�heightr>)rDrrrF)rGrIrJZpngdata�w�h�metadatar!r!r&�
retina_figure�s	rScs�fdd�}|S)a\Factory to return a matplotlib-enabled runner for %run.
 52  
 53      Parameters
 54      ----------
 55      safe_execfile : function
 56          This must be a function with the same interface as the
 57          :meth:`safe_execfile` method of IPython.
 58  
 59      Returns
 60      -------
 61      A function suitable for use as the ``runner`` argument of the %run magic
 62      function.
 63      cs�ddl}ddlm}|jd}|�d��|g|�Ri|��|�|�|jjr0|��d|j_z|j}Wn
 64  t	y?YdSw|�dS)z�matplotlib-aware wrapper around safe_execfile.
 65  
 66          Its interface is identical to that of the :func:`execfile` builtin.
 67  
 68          This is ultimately a call to execfile(), but wrapped in safeties to
 69          properly handle interactive rendering.rN�interactiveF)
 70  r2�matplotlib.pyplot�pyplotr3rT�draw_if_interactive�called�drawZdraw_all�AttributeError)�fname�whererKr2�plt�is_interactive�da��
safe_execfiler!r&�mpl_execfile�s
 71  
 72  
 73  
 74  �
 75  z mpl_runner.<locals>.mpl_execfiler!)rarbr!r`r&�
 76  mpl_runner�srcc
 77  Cs:z|jjj}Wnty}zt�|�d}~ww|�dS)zreshow an nbagg figureN)r"�manager�reshowrZ�NotImplementedError)rGre�er!r!r&�_reshow_nbagg_figure�s��
 78  rhcs�ddl}ddlm�|jjd}|jjd}|jjd}|jjd}t|t�r*|h}t|�}�fdd	�|jj��D�|�	��
 79  �}|d
 80  ksI|dkrS|jj}	|	��t
�hd�}
 81  |�|
 82  �}|r~d
d�dd	�|D��}d
d�dd	�|
 83  D��}
td|
|f��d|vr�|��ttfddd�|���d|vs�d|vr�|��ttfddi|���d|vs�d|vr�|��ttfddd�|���d|vr�|��ttfddi|���d|vr�|��ttfddd�|���dSdS)a_Select figure formats for the inline backend.
 84  
 85      Parameters
 86      ----------
 87      shell : InteractiveShell
 88          The main IPython instance.
 89      formats : str or set
 90          One or a set of figure formats to enable: 'png', 'retina', 'jpeg', 'svg', 'pdf'.
 91      **kwargs : any
 92          Extra keyword arguments to be passed to fig.canvas.print_figure.
 93      rN��Figurez
image/svg+xmlz	image/pngz
 94  image/jpegzapplication/pdfcsg|]}|��d��qS)N)�pop�r$r0rir!r&r'
 95  sz)select_figure_formats.<locals>.<listcomp>rr>r5�jpgr7r�png2xr�jpegz%s�,cS�g|]}t|��qSr!��reprrlr!r!r&r'r(cSrqr!rrrlr!r!r&r'r(z supported formats are: %s not %sr5TrMr7rnrIrmrorrHr)r2�matplotlib.figurerj�display_formatter�
 96  formatters�
 97  isinstance�str�set�values�get_backend�lower�ipython_display_formatter�for_typerh�
 98  difference�join�
 99  ValueErrorrrDrS)�shell�formatsrJr2Z
svg_formatterZ
png_formatterZ
jpg_formatterZ
pdf_formatterZ
100  mplbackend�	formatter�	supported�bad�bs�gsr!rir&�select_figure_formats�sJ
101  
102  ����r�cCsfddl}|r|dkrt|}|dkrd}||fS|jd}t�|d�}|r/||kr/|}t|}||fS)a�Given a gui string return the gui and mpl backend.
103  
104      Parameters
105      ----------
106      gui : str
107          Can be one of ('tk','gtk','wx','qt','qt4','inline','agg').
108      gui_select : str
109          Can be one of ('tk','gtk','wx','qt','qt4','inline').
110          This is any gui already selected by the shell.
111  
112      Returns
113      -------
114      A tuple of (gui, backend) where backend is one of ('TkAgg','GTKAgg',
115      'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
116      rN�autor�backend)r2�backendsZrcParamsOrig�backend2guir,)�guiZ
117  gui_selectr2r�r!r!r&�find_gui_and_backend-s
118  �r�cCsJddl}|�d�||jd<ddlm}|�|�d|j_t|j�|_dS)z7Activate the given backend and set interactive to True.rNTr�)rVF)	r2rTr3rVZswitch_backend�showZ	_needmainrrW)r�r2r]r!r!r&�activate_matplotlibXs
119  
120  
121  r�TcCsHd}t||�|rd}t||�t|d<ddlm}||d<t|d<dS)	z�Populate the namespace with pylab-related values.
122  
123      Imports matplotlib, pylab, numpy, and everything from pylab and numpy.
124  
125      Also imports a few names from IPython (figsize, display, getfigs)
126  
127      zbimport numpy
128  import matplotlib
129  from matplotlib import pylab, mlab, pyplot
130  np = numpy
131  plt = pyplot
132  z3from matplotlib.pylab import *
133  from numpy import *
134  r4r)�displayr�r1N)�execr4�IPython.displayr�r1)�user_ns�
135  import_all�sr�r!r!r&�import_pylabps
136  
137  r�cCs*tjdtdd�ddlm}|||�dS)a
138  
139      .. deprecated:: 7.23
140  
141          use `matplotlib_inline.backend_inline.configure_inline_support()`
142  
143      Configure an IPython shell object for matplotlib use.
144  
145      Parameters
146      ----------
147      shell : InteractiveShell instance
148      backend : matplotlib backend
149      z�`configure_inline_support` is deprecated since IPython 7.23, directly use `matplotlib_inline.backend_inline.configure_inline_support()`r8)�
150  stacklevelr)�configure_inline_supportN)�warnings�warn�DeprecationWarning� matplotlib_inline.backend_inliner�)r�r�Zconfigure_inline_support_origr!r!r&r��s
�r�)r5r6F)F)NN)T)�__doc__�ior�binasciir�	functoolsrr��IPython.core.displayr�IPython.utils.decoratorsrr��dict�ziprz�keysr�r1r4rDrSrcrhr�r�r�r�r�r!r!r!r&�<module>s���������	�
151  ���
������
152  
153  10
154  
155  <+
156  !