
&9_[c           @  s  d  Z  d d l m Z d d l m Z d d l m Z d d l m Z d d l	 m
 Z
 i d d 6d	 d
 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d d! 6Z e e e j   e j     Z d e d <d
 e d" <e d# <d e d$ <d e d% <d e d& <e d =e d =e d =e d =e d =e d =d'   Z d(   Z d) d* d+  Z d,   Z d-   Z d.   Z d/   Z d4 d4 d0  Z d1   Z e d2  Z d3   Z d4 S(5   s%   Pylab (matplotlib) support utilities.i(   t   print_function(   t   BytesIO(   t   _pngxy(   t
   flag_calls(   t	   py3compatt   TkAggt   tkt   GTKAggt   gtkt   GTK3Aggt   gtk3t   WXAggt   wxt   Qt4Aggt   qt4t   Qt5Aggt   qt5t   qtt   MacOSXt   osxt   nbAggt   nbaggt   notebookt   aggt   svgt   pdft   pss'   module://ipykernel.pylab.backend_inlinet   inlines   module://ipympl.backend_nbaggt   ipymplt   widgett   GTKt   GTKCairot	   GTK3Cairot   WXt   CocoaAggc          G  s   d d l  m } |  s? | j   } g  | D] } | j j ^ q) Sg  } xP |  D]H } | j j |  } | d k r t d |  qL | j	 | j j  qL W| Sd S(   s  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.
    i(   t   Gcfs!   Warning: figure %s not available.N(
   t   matplotlib._pylab_helpersR#   t   get_all_fig_managerst   canvast   figuret   figst   gett   Nonet   printt   append(   t   fig_numsR#   t   fig_managerst   fmR(   t   numt   f(    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   getfigs=   s    c         C  s#   d d l  } |  | g | j d <d S(   s   Set 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]
    iNs   figure.figsize(   t
   matplotlibt   rcParams(   t   sizext   sizeyR3   (    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   figsizeX   s    t   pngt   tightc   	      K  s   d d l  m } |  j r( |  j r( d S|  j } | d k rP | d } d } n  t d | d |  j   d	 |  j   d
 | d |  } | j |  t	   } |  j
 j | |  | j   } | d k r | j d  } n  | S(   s  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``.
    i(   R4   Nt   retinai   R8   t   formatt	   facecolort	   edgecolort   dpit   bbox_inchesR   s   utf-8(   R3   R4   t   axest   linesR>   t   dictt   get_facecolort   get_edgecolort   updateR   R&   t   print_figuret   getvaluet   decode(	   t   figt   fmtR?   t   kwargsR4   R>   t   kwt   bytes_iot   data(    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyRF   c   s(    		
			c         K  s^   t  |  d d | } | d k r% d St |  \ } } t d | d d | d  } | | f S(   s/   format a figure as a pixel-doubled (retina) PNGRJ   R:   Nt   widthi   t   height(   RF   R*   R   RB   (   RI   RK   t   pngdatat   wt   ht   metadata(    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   retina_figure   s    c           s     f d   } | S(   sX  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 j } | j d } | j t    |  | |  | j |  | j j rw | j   t | j _ n  y | j	 } Wn t
 k
 r n X|   d S(   s   matplotlib-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.iNt   interactive(   R3   t   matplotlib.pyplott   pyplotR4   RV   t   Falset   draw_if_interactivet   calledt   drawt   draw_allt   AttributeError(   t   fnamet   whereRL   R3   t   pltt   is_interactivet   da(   t   safe_execfile(    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   mpl_execfile   s    
(    (   Rd   Re   (    (   Rd   s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt
   mpl_runner   s    c         C  s;   y |  j  j j } Wn t k
 r/ t    n X|   d S(   s   reshow an nbagg figureN(   R&   t   managert   reshowR^   t   NotImplementedError(   RI   Rh   (    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   _reshow_nbagg_figure   s
    c           s  d d l  } d d l m } |  j j d } |  j j d } |  j j d } |  j j d } t | t j  rz | h } n  t |  } g  |  j j j	   D] }	 |	 j
 | d  ^ q | j   j   }
 |
 d k s |
 d	 k r |  j j } | j | t  n  d
 d d d d d d h } | j |  } | rd d j g  | D] }	 t |	  ^ q> } d d j g  | D] }	 t |	  ^ qj } t d | | f   n  d
 | k r| j |   f d    n  d | k sd | k r| j |   f d    n  d | k sd | k r/| j |   f d    n  d | k rW| j |   f d    n  d | k r| j |   f d    n  d S(   s_  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.
    iN(   t   Figures   image/svg+xmls	   image/pngs
   image/jpegs   application/pdfR   s   module://ipympl.backend_nbaggR8   t   png2xR:   t   jpgt   jpegR   R   s   %st   ,s    supported formats are: %s not %sc           s   t  |  d    S(   NR8   (   RF   (   RI   (   RK   (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   <lambda>   s    c           s   t  |     S(   N(   RU   (   RI   (   RK   (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyRp      s    c           s   t  |  d    S(   NRm   (   RF   (   RI   (   RK   (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyRp      s    c           s   t  |  d    S(   NR   (   RF   (   RI   (   RK   (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyRp      s    c           s   t  |  d    S(   NR   (   RF   (   RI   (   RK   (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyRp      s    (   R3   t   matplotlib.figureRk   t   display_formattert
   formatterst
   isinstanceR   t   string_typest   sett   valuest   popR*   t   get_backendt   lowert   ipython_display_formattert   for_typeRj   t
   differencet   joint   reprt
   ValueError(   t   shellt   formatsRK   R3   Rk   t   svg_formattert   png_formattert   jpg_formattert   pdf_formatterR1   t
   mplbackendt	   formattert	   supportedt   badt   bst   gs(    (   RK   s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   select_figure_formats   s<    /,,c         C  sy   d d l  } |  r+ |  d k r+ t |  } nD | j d } t j | d  }  | ro |  | k ro | }  t |  } n  |  | f S(   s  Given a gui string return the gui and mpl backend.

    Parameters
    ----------
    gui : str
        Can be one of ('tk','gtk','wx','qt','qt4','inline').
    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://ipykernel.pylab.backend_inline').
    iNt   autot   backend(   R3   t   backendst   rcParamsOrigt   backend2guiR)   R*   (   t   guit
   gui_selectR3   R   (    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   find_gui_and_backend  s    c         C  ss   d d l  } | j t  |  | j d <d d l } | j j |   d d l j } t | j _	 t
 | j  | _ d S(   s7   Activate the given backend and set interactive to True.iNR   (   R3   RV   t   TrueR4   RW   RX   t   switch_backendRY   t   showt	   _needmainR   RZ   (   R   R3   Ra   (    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   activate_matplotlib/  s    c         B  sW   d } | |  U| r% d } | |  Un  e  |  d <d d l m } | |  d <e |  d <d S(	   s   Populate 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)
    
    sb   import numpy
import matplotlib
from matplotlib import pylab, mlab, pyplot
np = numpy
plt = pyplot
s3   from matplotlib.pylab import *
from numpy import *
R7   i(   t   displayR   R2   N(   R7   t   IPython.core.displayR   R2   (   t   user_nst
   import_allt   sR   (    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   import_pylabG  s    

c   	      C  s  y d d l  m } Wn t k
 r( d SXd d l } | j d |   } |  | _ | |  j k rr |  j j |  n  | t d k r d d l  m	 } |  j
 j d |  t   |  _ x% | j D] } | j | |  j | <q W| j j | j  d } nl d d l  m	 } y |  j
 j d |  Wn t k
 r/n Xt |  d  r[| j j |  j  |  ` n  d	 } t t d
 d  } | | k rt |  | j | j  | t _ n  d S(   s   Configure an IPython shell object for matplotlib use.

    Parameters
    ----------
    shell : InteractiveShell instance

    backend : matplotlib backend
    i(   t   InlineBackendNt   parentR   (   t   flush_figurest   post_executet   _saved_rcParamst   othert   current_backendt   unset(   t   ipykernel.pylab.backend_inlineR   t   ImportErrorR3   t   instanceR   t   configurablesR,   R   R   t   eventst   registerRB   R   t   rcR4   RE   t
   unregisterR   t   hasattrt   getattrt   configure_inline_supportR   t   figure_formatst   print_figure_kwargsR   (	   R   R   R   R3   t   cfgR   t   kt   new_backend_namet   cur_backend(    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyR   h  s<    			N(   t   __doc__t
   __future__R    t   ioR   R   R   t   IPython.utils.decoratorsR   t   IPython.utilsR   R   RB   t   zipRw   t   keysR   R2   R7   RF   RU   Rf   Rj   R   R*   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/IPython/core/pylabtools.pyt   <module>   sZ   

!



		&		0	
	6)	!