)Uc@s\dZddlZddlZddlZddlZddlZddlZddlZyddlm Z Wn!e k rddl m Z nXddl m Z yddl mZWn*e k rddl mZejZnXddlZejjdZeged D]"Zeejd ed ^qZddlZddljZejd ddljZddljZdd lm Z dd l!m"Z"de#fdYZ$e%ej&drej&j'Z'nej(dZ'dZ)dZ*dZ+e,Z-dZ.e/dZ0dZ1dZ2e/dZ3dZ4dZ5dZ6dZ7dS(s} A special directive for including a matplotlib plot. The source code for the plot may be included in one of two ways: 1. A path to a source file as the argument to the directive:: .. plot:: path/to/plot.py When a path to a source file is given, the content of the directive may optionally contain a caption for the plot:: .. plot:: path/to/plot.py This is the caption for the plot Additionally, one my specify the name of a function to call (with no arguments) immediately after importing the module:: .. plot:: path/to/plot.py plot_function1 2. Included as inline content to the directive:: .. plot:: import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np img = mpimg.imread('_static/stinkbug.png') imgplot = plt.imshow(img) In HTML output, `plot` will include a .png file with a link to a high-res .png and .pdf. In LaTeX output, it will include a .pdf. To customize the size of the plot, this directive supports all of the options of the `image` directive, except for `target` (since plot will add its own target). These include `alt`, `height`, `width`, `scale`, `align` and `class`. Additionally, if the `:include-source:` option is provided, the literal source will be displayed inline in the text, (as well as a link to the source in HTML). If this source file is in a non-UTF8 or non-ASCII encoding, the encoding must be specified using the `:encoding:` option. If the `:context:` option is plotted, the code will be run in the context of all previous plot directives for which the context option was specified. This only applies to inline code plot directives, not those run from files. If the ``:nofigs:`` option is specified, the code block will be run, but no figures will be inserted. This is usually useful with the ``:context:`` option. The set of file formats to generate can be specified with the `plot_formats` configuration variable. Error handling: Any errors generated during the running of the code are emitted as warnings using the Python `warnings` module, using a custom category called `PlotWarning`. To turn the warnings into fatal errors that stop the documentation build, after adjusting your `sys.path` in your `conf.py` Sphinx configuration file, use:: import plot_directive warnings.simplefilter('error', plot_directive.PlotWarning) iN(tmd5(t directives(talign(tImaget.is[a-z]itAgg(t_pylab_helpers(tonly_directivest PlotWarningcBseZdZRS(soWarning category for all warnings generated by this directive. By printing our warnings with this category, it becomes possible to turn them into errors by using in your conf.py:: warnings.simplefilter('error', plot_directive.PlotWarning) This way, you can ensure that your docs only build if all your examples actually run successfully. (t__name__t __module__t__doc__(((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyRes trelpathcCs]tjj|s"td|ntjj|sDtd|ntjj|jtj}tjj|jtj}tjd kr|d|dkrtd|dj d|dj nxKt t t |t |D]}||||krPqqW|d 7}tj gt ||||}|rUtjj|Sd Sd S( s Return a relative path to the target from either the current dir or an optional base dir. Base can be a directory specified either as absolute or relative to current dir. sTarget does not exist: s+Base is not a directory or does not exist: tnttdostos2is0Target is on a different drive to base. Target: s, base: itN(sntRsos2(tostpathtexiststOSErrortisdirtabspathtsplittseptnametuppertrangetmintlentpardirtjoin(ttargettbaset base_listt target_listtitrel_list((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyR ys #,( "s .. htmlonly:: %(links)s .. figure:: %(prefix)s%(tmpdir)s/%(outname)s.png %(options)s %(caption)s .. latexonly:: .. figure:: %(prefix)s%(tmpdir)s/%(outname)s.pdf %(options)s %(caption)s sh .. htmlonly:: [`source code <%(linkdir)s/%(basename)s.py>`__] Exception occurred rendering plot. s cCsGtjj| pFtjj|oFtj|jtj|jkS(sh Returns True if derivative is out-of-date wrt original, both of which are full file paths. (RRRtstattst_mtime(toriginaltderived((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyt out_of_datesc Bs=|dk r;d|}|r0|defdUq9|dUnej}ejj|\}}ejjdejj|ej }e j e_ ej |d} z4e |} ejd| |ddejf} Wdejd=ej ||e_ | dk r| jnX|dk r9e| |ndS(ss Import a Python module from a path, and run the function given by name, if function_name is not None. s6import numpy as np; import matplotlib.pyplot as plt %sNit__plot__tpytr(tNonet plot_contextRtgetcwdRRtsystinsertRtstdoutt cStringIOtStringIOtchdirtopentimpt load_modulet PY_SOURCEtclosetgetattr( t plot_patht function_namet plot_codetcontextt exec_codetpwdRtfnameR3tfdtmodule((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pytrun_codes.       "     cCs tjj}xt|D]\}}xt|D]\}\} } t|dkrb|} nd||f} | d| } tjj|| } y|jj j | d| Wn+t j d|} t j| tdSX|dkr5tj| tjj|| q5q5WqWt|S(s Once a plot script has been imported, this function runs savefig on all of the figures in all of the desired formats. is%s_%02dRtdpisException saving plot %si(RtGcftget_all_fig_managerst enumerateRRRRtcanvastfiguretsavefigtcbooktexception_to_strtwarningstwarnRtshutiltcopyfile(R=tbasenamettmpdirtdestdirtformatst fig_managersR$tfigmantjtformatRGtoutnametoutpathts((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyt run_savefigs"  *cCstjdtjdS(Ntall(tpltR;t matplotlibtrc_file_defaults(((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyt clear_state s cCst|}tjj|\}}tjj|\} } t} xL|D]D\} } tjj|d| | f}t||rIt} PqIqIW| r| rdSd}xvtr t} xO|D]G\} } tjj|d| || f}t||rt} PqqW| r|d7}qPqW| r8|dkr8|S|sHt nyt |||d|Wn+t j d|}t j|tdSX| st|| |||}dtjkrtjd=n|SdSdS( sY Run a pyplot script and save the low and high res PNGs and a PDF in outdir. s%s.%siis %s_%02d.%sR@sException running plot %sR+N(tstrRRRtsplitexttTrueRR*tFalseRdRFRNRORPRQRR_R1tmodules(R=R>R?RURVRWR@tbasedirRCRTtextt all_existsR[RGR\R$R^tnum_figs((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pytrender_figuressN       c Cs|jd}|r|d=n|jd}|r>|d=ntjj} t| tksnt| tkr}t| } ntj j |} tj j | \} } tj j |j jd\} }tj jd|}ttj| }tj j||}tj jd|}tj j|}tdkrAd}nd }tj j|sitj|ntj jtj jtjjj|}tj j|stj|nd jd |j d D}t|||||| d|}g}|dkr,tj|tj j|| n|jd r|dkrtdkr\d }ntjjj}|jdtj j||dg|jdr|j d|d|d=qnF|jddg|jg|j d D]}d|j!^q|j d|d =ng}|s}|dkr]g|j"D]\}}dt#||f^qD}d j|}xt$|D]}|d kr| }nd| |f}g}|dkr|j dnx5| d D])\}}|j d||||fqWt%|r3ddj|t&}nd}|jt't&j d qWq}|jt(t&j d nt%|r|j)||j*j+dngS(NR@tnofigstsourcetplot_directivetbuildiiRt/s css|]}t|jVqdS(N(ttemplate_content_indenttstrip(t.0tline((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pys ssinclude-sourceis.. include:: %ss :literal:tencodings :encoding: %ss::s %ss %s:%s: %ss%s_%02ds-`source code <%(linkdir)s/%(basename)s.py>`__s`%s <%s/%s.%s>`__s[%s]s, (ii(i(,thas_keytsetuptconfigt plot_formatsttypeRetunicodetevalRRRTRfRtdocumentt attributesRR tconfdirRtsphinx_versionRRNtmkdirstapptbuildertoutdirRnR.RRRStsrcdirtextendtappendtrstriptitemsRtRRtlocalsttemplatetexception_templatet insert_inputt input_linesRp( R=RjR>R?tcaptiontoptionst state_machineR@RoRWRCRTRktrstdirtrstfileRtreldirtlinkdirRUtprefixRVRmtlinestinclude_prefixtrowtkeytvalR$R\tlinksR[RG((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyt_plot_directivePs   $"   *  "      *   /   ! $  c Cst|rtj|d} ttjj| tjj j } dj |} t|dkrq|d} nd} t | | | d| ||Sdj |} t| jd} t | dd| d||SdS( sd Handle the arguments to the plot directive. The real work happens in _plot_directive. is iiitinlineRN(RRturiR RRtdirnameRzRRRRR.RRt hexdigest(Rt argumentsRtcontenttlinenotcontent_offsett block_texttstateRR=RjRR>R?((sV/opt/alt/python27/lib64/python2.7/site-packages/matplotlib/sphinxext/plot_directive.pyRqs $  c Cs1x*|jjD]\}}|s(qn|j|}|dkrGqn|j|}|jdkrx|D]}|jdkrj|}x-|D]%}|jdkr|j}PqqW|dj||dj||dj||dj||j j j ||f|j j j |DsNT    3  6   (   @ z #