
&Kc           @   s  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d   Z	 d   Z
 d   Z d   Z d d  Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d d d d d d d d d g d d g d d  Z d d e e d d d d  Z d d d  Z d d d d   Z e e e e d!  Z d"   Z d#   Z d$   Z d%   Z  d&   Z! d S('   s\   
deco.py

Routines and Matplotlib Settings for Various Useful Plot Formats

Lev, 8 Apr 2009
iNc           C   s   t  i d d t d S(   s   Turn TeX rendering ont   textt   usetexN(   t   pylabt   rct   True(    (    (    s   c:\py\lib\deco.pyt   texon#   s    c           C   s   t  i d d t d S(   s   Turn TeX rendering offR    R   N(   R   R   t   False(    (    (    s   c:\py\lib\deco.pyt   texoff$   s    c           C   s   t  i d S(   s/   Check if TeX rendering is on. Return True/Falses   text.usetex(   R   t   rcParams(    (    (    s   c:\py\lib\deco.pyt   istex%   s    c          C   s:   x3 t  t i i    D] }  d |  t i |  f GHq Wd S(   s/   Print matplotlib settings in alphabetical orders   %s: %sN(   t   sortedR   R   t   keys(   t   k(    (    s   c:\py\lib\deco.pyt   dumprc'   s     c      	   K   sP   |  d j o t i   }  n t i d d d t i |   d d d d | d S(   s   
    Add a date stamp to the top right corner of the plot. Use today if 'tm'
    is not specified. Extra keywords are passed to 'pylab.figtext'
    g\(\?s   Plotted t   vat   topt   hat   rightN(   t   Nonet   timeR   t   figtextt   flibt   dt2str(   t   tmt   vargs(    (    s   c:\py\lib\deco.pyt	   datestamp,   s     c      	   K   s   t  i   i } t   oD t  i   i   d } d | | i | i |  i d d  f }  n t  i | i d |  d d d d	 | d
 S(   s1   
    Add a comment to the bottom of the page
    i    s   \parbox{%.2fin}{%s}s   
s   \\g{Gz?R   t   bottomR   t   leftN(	   R   t   gcft   subplotparsR	   t   get_size_inchesR   R   t   replaceR   (   R    R   t   parst   width_inches(    (    s   c:\py\lib\deco.pyt   footer4   s
    
.c      	   K   s  t  |   } t |  d j o t d   n t t |  \ } } } t i   i } | i | i	 } | i
 | i }	 |	 | | i | d }
 | i |
 } | | | i | d } | i | } t | d |  \ } } | i
 | d |
 | | } | i	 | | | } | |
 } t   o: t i   i   d } d | | | i d d  f } n t i | | | d d	 d
 d | d S(   s*   
    Add a text in place of a subplot
    i   s#   Subplot index must be 3 digits longi   i    s   \parbox{%.2fin}{%s}s   
s   \\R   R   R   R   N(   t   strt   lent
   ValueErrort   mapt   intR   R   R   R   R   R   R   t   hspacet   wspacet   divmodR	   R   R   R   (   t   subplotR    R   t   st   rowst   colst   numR    t   totWidtht	   totHeightt   figHt   sepHt   figWt   sepWt   rowNumt   colNumt	   figBottomt   figLeftt   figTopR!   (    (    s   c:\py\lib\deco.pyt   subtext>   s&    

$c          K   s$  t  i i h  d d <d d <d d <d d <d d <d	 d
 <d g d <d g d <d g d <d d <d d <d d <d d <d d <d d <d d <d d < d |  j o t  i i |  d  |  d =n d |  j o |  d =d" |  d  <n d# |  d  <t  i |    } t   | S($   s(  
    Create a figure for an A4 plot in portrait orientation. TeX rendering is not altered.
    
    optional arguments:
    'override' - a dictionary with RC parameters to override
    'letter' - alters the page size from 8 x 11 to 8 x 10.8 inches,
    suitable for american Letter page size
    i   s   xtick.major.padi   s	   font.sizet   mediums   xtick.labelsizes   ytick.labelsizes   legend.fontsizet   serifs   font.familyt   cms   font.sans-serifs
   font.serifs   font.monospacei   s   legend.numpointsg333333?s   figure.subplot.leftg333333?s   figure.subplot.bottomg?s   figure.subplot.tops   figure.subplot.rightg      ?s   lines.markeredgewidthg      ?s   lines.linewidths   axes.linewidtht   overridet   letterg      %@t   figsizei   (   i   g      %@(   i   i   (   R   R   t   updatet   figureR   (   R   t   fig(    (    s   c:\py\lib\deco.pyt
   portraitA4a   s8    
													
c          K   s-  t  i i h  d d <d d <d d <d d <d d <d	 d
 <d g d <d g d <d g d <d d <d d <d d <d d <d d <d d <d d <d d <d d < d  |  j o t  i i |  d   |  d  =n d! |  j o |  d! =d% |  d# <n d& |  d# <t  i |    } t   | S('   s)  
    Create a figure for an A4 plot in landscape orientation. TeX rendering is not altered.
    
    optional arguments:
    'override' - a dictionary with RC parameters to override
    'letter' - alters the page size from 11 x 8 to 10.8 x 8 inches,
    suitable for american Letter page size
    i   s   xtick.major.padi   s	   font.sizeR<   s   xtick.labelsizes   ytick.labelsizes   legend.fontsizeR=   s   font.familyR>   s   font.sans-serifs
   font.serifs   font.monospacei   s   legend.numpointsg?s   figure.subplot.leftgQ?s   figure.subplot.bottomgףp=
?s   figure.subplot.topgffffff?s   figure.subplot.rightg333333?s   figure.subplot.wspaceg      ?s   lines.markeredgewidthg      ?s   lines.linewidths   axes.linewidthR?   R@   g      %@RA   i   (   g      %@i   (   i   i   (   R   R   RB   RC   R   (   R   RD   (    (    s   c:\py\lib\deco.pyt   landscapeA4   s:    
														
c          K   s3  t    t i i h  d d <d d <d d <d d <d d <d	 d
 <d g d <d g d <d g d <d d <t d <d d <d d <d d <d d <d d <d d <d d < d |  j o t i i |  d  |  d =n d  |  i   j o d# |  d  <n t i |    } | i |  d    | S($   s   
    Create a figure for a publication plot. Default size is 3x1.9 inch, a
    landscape plot with a golden aspect ratio that fits into a column of PRL.
    TeX rendering is turned on.
    i   s   xtick.major.padi
   s	   font.sizet   smalls   xtick.labelsizes   ytick.labelsizes   legend.fontsizeR=   s   font.familyR>   s   font.sans-serifs
   font.serifs   font.monospacei   s   legend.numpointss   text.usetexg333333?s   figure.subplot.leftg?s   figure.subplot.bottomgffffff?s   figure.subplot.tops   figure.subplot.rightg333333?s   lines.markeredgewidthg      ?s   lines.linewidthg      ?s   axes.linewidthR?   RA   i   gffffff?(   i   gffffff?(   R   R   R   RB   R   R   RC   t   set_size_inches(   R   RD   (    (    s   c:\py\lib\deco.pyt   pubplot   s8    														c          K   s<  t    t i i h  d d <d d <d d <d d <d d <d	 d
 <d g d <d g d <d g d <d d <t d <d d <d d <d d <d d <d d <d d <d d <d d < d  |  j o t i i |  d   |  d  =n d! |  i   j o d$ |  d! <n t i |    } | i |  d!   | S(%   s   
    Create a figure for a overhead presentation plot. Default size is 8x6 inch, a
    landscape plot with a golden aspect ratio that fits into a column of PRL.
    TeX rendering is turned on.
    i   s   xtick.major.padi   s	   font.sizeRG   s   xtick.labelsizes   ytick.labelsizes   legend.fontsizes
   sans-serifs   font.familyR>   s   font.sans-serifs
   font.serifs   font.monospacei   s   legend.numpointss   text.usetexg?s   figure.subplot.leftg333333?s   figure.subplot.bottomgffffff?s   figure.subplot.tops   figure.subplot.righti,  s   savefig.dpig      ?s   lines.markeredgewidths   lines.linewidthg      ?s   axes.linewidthR?   RA   i   i   (   i   i   (   R   R   R   RB   R   R   RC   RH   (   R   RD   (    (    s   c:\py\lib\deco.pyt   overheadplot   s:    															c         C   s=   |  i  d d  }  x$ d D] } |  i  | d |  }  q W|  S(   sL   
    Escape TeX special characters &, $, %, #, _, {, }, \ in the string
    s   \s   \textbackslash s   &$%#_{}(   R   (   R#   t   ch(    (    s   c:\py\lib\deco.pyt	   escapetex  s
     i,  i   i   i   i   i    i
   i   c         C   s=  |  i  } x= d d g D]/ }	 | i |	  o d | t |	  } Pq q W| i d d  } d }
 | d j	 o |
 | d 7}
 n |
 d t |  i d d	  i d
 d  7}
 |  i d j o |
 d |  i 7}
 n |
 d |  i   7}
 | d j	 o x | D] } x | D] } | i d | d |  } | i	 |  i
 | |  i |  } | i |  i
 |  i  } t |  d j  o qn t i | i    } t i | i    } |
 d | | | i d d d d  | i d d d d  f 7}
 qWq Wn | d j	 o | i	 |  i
 | |  i |  } | i |  i
 |  i  } t |  d j oZ |
 d t i | i    i d d d d  t i | i    i d d d d  f 7}
 qn | d j	 o | i	 |  i
 | |  i |  } | i |  i
 |  i  } t |  d j o3 |
 d t i | i    i d d d d  7}
 q/n |
 d 7}
 |
 S(   sY  
    Convert trace info to TeX. If 'plm'/'mct'/'paro' is specified,
    temperature/pressure during taking this log is returned.
    'maxextratime' is the maximum extention of time interval around the trace aquisition time
    in a search for thermometry/pressure points.
    'xmits', 'gains' are PLM4 XMIT pulses and GAINs to be considered
    s   /data/archive/phpc329/exps   /mnt/phpc329/exps   ...t   *t   #s   \begin{flushleft}s   \\s   Filename: \texttt{%s}\\s   \\\-t   /s   /\-i    s   %d averages, s   captured: %st   xmitt   gaini   s'   \\PLM4 xmit %d gain %d: $%s$ [mK], $%s$t   varnamet   Tt   fmts   %.3ft   M_0s   %.1fs0   \\MCTNS $%s$ [mK,Greywall] / $%s$ [mK,PLTS high]s   %.2fs   \\Paroscientific: $%s$ [mbar]t   Ps   \end{flushleft}N(   t   patht
   startswithR$   R   R   RL   t   Navt   timespant   att   periodt	   starttimet	   finaltimet   aroundt   avert   averaget   M0RS   t   totext   T_grwlt   T_pltsHRV   (   t   infot   plmt   mctt   parot   maxextratimet   xmitst   gainst   captionRW   t   loct   texRP   RQ   t   pRb   RS   (    (    s   c:\py\lib\deco.pyt   traceinfo2tex  sR    	 	,    J $4 7
t   .s   %d/%m/%y %H:%M:%SiZ   c	         K   s   | d j	 o t i |  n t i |  | | | | | |	  }
 t i i | |  } | oO t i   i i	 |  t i   i
 t t t  t i   i d | d d  n | o3 t i   i i	 |  t i   i
 t t t  n t i   |
 S(   s   
    A version of pylab.plot_date()
    Use a matplotlib.dates.DateFormatter with datefmt format.
    Call autofmt_xdate(rotation=45, ha='center') for a current figure.
    t   rotationR   t   centerN(   R   R   t   axest	   plot_datet
   matplotlibt   datest   DateFormattert   gcat   xaxist   set_major_formattert   autoscale_viewR   R   R   t   autofmt_xdatet   yaxist   draw_if_interactive(   t   xt   yRT   t   tzt   xdatet   ydatet   datefmtRu   Rs   t   kwargsRp   t   fmtr(    (    s   c:\py\lib\deco.pyt   plotdateM  s    ! 
R   s   %+gc         C   s   t  |   } d } | d j oH xE t d | d  D], } | | |  | | d | | d 7} q3 Wn | d j o | | |  d | 7} n | d j o | | |  d 7} n | d j o
 d } n | d d	 j o | d } n | S(
   s  
    Convert a list of polynomial coefficients 'p' into a TeX-style string using
    'argname' for the argument. 'p' is assumed to be in power-decending order,
    as returned by 'numpy.polyfit' routine.
    
    When overriding coefficient format 'format', please remember to use '+' in
    the format abbreviation that puts '+' signes in front of positive
    coefficients. No extra '+' are added between terms in the polynomial.
    
    '+' is automatically deleted in front of the first argument, '0' is
    returned for a zeroth-order polynomail (empty array 'p')
    The string returned is not surrounded by math-mode delimiters (e.g. '$').
    t    i   i    s   ^%di   iit   0t   +(   R$   t   range(   Rp   t   argnamet   formatt   NR,   t   i(    (    s   c:\py\lib\deco.pyt   poly2strf  s     .   
 s   %.3fc         C   sf   t  i t  i t |     } | | j  p | | j o" |  d | :}  | |  d | Sn	 | |  Sd S(   s?   
    Convert a number to scientific representation in TeX 
    i
   s   \times 10^{%d}N(   t   numpyt   floort   log10t   abs(   t   nRT   t	   min_ordert	   max_ordert   power10(    (    s   c:\py\lib\deco.pyt	   float2tex  s
    c         C   s  g  } t  |  t i  o |  g }  n xh |  D]` } t  | t i i  o | i |  q- t  | t i i  o | | i 7} q- t d   q- Wt	 |  d j  o d Sn t
 i g  } | D] } | | i   q ~  } t
 i g  }	 | D] } |	 | i   q ~	  }
 | o t |  t |  f n t |  t |  f } | o t |
  t |
  f n t |
  t |
  f }
 x> | D]6 } | o | i |  n | o | i |
  qqWt
 i | |
 g  S(   ss  
    Equalise limits of all given axes and all given figures to show all the data.
    'axes_and_figures' - an array of axes and figures to modify, can be a figure, then all subplots are adjusted
    'x','y' - determine whenever to adjust x/y range
    'x_inverse', 'y_inverse' - determine whenever the range should be from small to large or
    the other way round.
    sQ   'axes_and_figures' contains an element that is neither a set of axes nor a figurei   N(   t
   isinstanceR   t   FigureRw   Ru   t   Axest   appendRC   R%   R$   R   t   concatenatet   get_xlimt   get_ylimt   maxt   mint   set_xlimt   set_ylimt   vstack(   t   axes_and_figuresR   R   t	   x_inverset	   y_inverseRu   t   at   _[1]t   xlimt   _[2]t   ylim(    (    s   c:\py\lib\deco.pyt   eqlims  s.    	 0088 c         C   s3   t  i   i i t i i d |    t  i   d S(   sJ   
    Set a LinearLocator with 'n' ticks as the current x-axis locator
    t   numticksN(   R   Rz   R{   t   set_major_locatorRw   t   tickert   LinearLocatort   draw(   R   (    (    s   c:\py\lib\deco.pyt   setnumxticks  s    %c         C   s3   t  i   i i t i i d |    t  i   d S(   sJ   
    Set a LinearLocator with 'n' ticks as the current y-axis locator
    R   N(   R   Rz   R   R   Rw   R   R   R   (   R   (    (    s   c:\py\lib\deco.pyt   setnumyticks  s    %c         O   s?   d t  i d <t  i |  d | |  t  i |  d | |  d S(   s   
    Save the current figure as PDF and PNG
    the 'filename' is augmented by '.pdf'/'.png' extension.
    All other arguments are passed to 'pylab.savefig()'
    iX  s   savefig.dpis   .pdfs   .pngN(   R   R   t   savefig(   t   filenamet   argsR   (    (    s   c:\py\lib\deco.pyt   savefigs  s    c      	   C   s"   t  i g  g  d d d d d |  S(   s   
    Plot a void line in order to add a line to the legend.
    The legend must autogenerated for this line to be present.
    t   lsR   t   markert   label(   R   t   plot(   R    (    (    s   c:\py\lib\deco.pyt
   legendline  s    c         C   sN   t  i d d |  i t  i   } | d j	 o | i |   n t  i   d S(   sV   
    Set the default colormap to a given value and apply to current image if any.
    t   imaget   cmapN(   R   R   t   namet   gciR   t   set_cmapR   (   R   t   im(    (    s   c:\py\lib\deco.pyt   set_colormap  s
    ("   t   __doc__R   t   matplotlib.datesRw   R   t   matplotlib.tickerR   R   R`   R   R   R	   R   R   R   R"   R;   RE   RF   RI   RJ   RL   Rq   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s   c:\py\lib\deco.pys   <module>   s0   <$					
	#	-	.	(	)		37'				