GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — matplotlib.github.com/sampledoc/extensions.html at main · matplotlib/matplotlib.github.com Expand file tree/ Copy pathextensions.htmlMore file actions 563 lines (465 loc) · 32.9 KB / Copy pathextensions.htmlFile metadata and controls563 lines (465 loc) · 32.9 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Sphinx extensions for embedded plots, math and more — sampledoc 1.0 documentation</title> <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: './', VERSION: '1.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true, SOURCELINK_SUFFIX: '.txt' }; </script> <script type="text/javascript" src="_static/jquery.js"></script> <script type="text/javascript" src="_static/underscore.js"></script> <script type="text/javascript" src="_static/doctools.js"></script> <script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="next" title="Ipython Directive" href="ipython_directive.html" /> <link rel="prev" title="Customizing the look and feel of the site" href="custom_look.html" /> </head> <body role="document"> <div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px"> <a href="index.html"><h1 style="font-size: 3em;">Sampledoc</h1></a> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="ipython_directive.html" title="Ipython Directive" accesskey="N">next</a> |</li> <li class="right" > <a href="custom_look.html" title="Customizing the look and feel of the site" accesskey="P">previous</a> |</li> <li><a href="index.html">home</a>| </li> <li><a href="search.html">search</a>| </li> </ul> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="index.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">Sphinx extensions for embedded plots, math and more</a><ul> <li><a class="reference internal" href="#ipython-sessions">ipython sessions</a></li> <li><a class="reference internal" href="#using-math">Using math</a></li> <li><a class="reference internal" href="#inserting-matplotlib-plots">Inserting matplotlib plots</a></li> <li><a class="reference internal" href="#inheritance-diagrams">Inheritance diagrams</a></li> <li><a class="reference internal" href="#this-file">This file</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="custom_look.html" title="previous chapter">Customizing the look and feel of the site</a></p> <h4>Next topic</h4> <p class="topless"><a href="ipython_directive.html" title="next chapter">Ipython Directive</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="_sources/extensions.rst.txt" rel="nofollow">Show Source</a></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <form class="search" action="search.html" method="get"> <div><input type="text" name="q" /></div> <div><input type="submit" value="Go" /></div> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="sphinx-extensions-for-embedded-plots-math-and-more"> <span id="extensions"></span><h1>Sphinx extensions for embedded plots, math and more<a class="headerlink" href="#sphinx-extensions-for-embedded-plots-math-and-more" title="Permalink to this headline">¶</a></h1> <p>Sphinx is written in python, and supports the ability to write custom extensions. We’ve written a few for the matplotlib documentation, some of which are part of matplotlib itself in the matplotlib.sphinxext module, some of which are included only in the sphinx doc directory, and there are other extensions written by other groups, eg numpy and ipython. We’re collecting these in this tutorial and showing you how to install and use them for your own project. First let’s grab the python extension files from the <code class="file docutils literal"><span class="pre">sphinxext</span></code> directory from git (see <a class="reference internal" href="getting_started.html#fetching-the-data"><span class="std std-ref">Fetching the data</span></a>), and install them in our <code class="file docutils literal"><span class="pre">sampledoc</span></code> project <code class="file docutils literal"><span class="pre">sphinxext</span></code> directory:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">home</span><span class="p">:</span><span class="o">~/</span><span class="n">tmp</span><span class="o">/</span><span class="n">sampledoc</span><span class="o">></span> <span class="n">mkdir</span> <span class="n">sphinxext</span> <span class="n">home</span><span class="p">:</span><span class="o">~/</span><span class="n">tmp</span><span class="o">/</span><span class="n">sampledoc</span><span class="o">></span> <span class="n">cp</span> <span class="o">../</span><span class="n">sampledoc_tut</span><span class="o">/</span><span class="n">sphinxext</span><span class="o">/*.</span><span class="n">py</span> <span class="n">sphinxext</span><span class="o">/</span> <span class="n">home</span><span class="p">:</span><span class="o">~/</span><span class="n">tmp</span><span class="o">/</span><span class="n">sampledoc</span><span class="o">></span> <span class="n">ls</span> <span class="n">sphinxext</span><span class="o">/</span> <span class="n">apigen</span><span class="o">.</span><span class="n">py</span> <span class="n">docscrape</span><span class="o">.</span><span class="n">py</span> <span class="n">docscrape_sphinx</span><span class="o">.</span><span class="n">py</span> <span class="n">numpydoc</span><span class="o">.</span><span class="n">py</span> </pre></div> </div> <p>In addition to the builtin matplotlib extensions for embedding pyplot plots and rendering math with matplotlib’s native math engine, we also have extensions for syntax highlighting ipython sessions, making inhertiance diagrams, and more.</p> <p>We need to inform sphinx of our new extensions in the <code class="file docutils literal"><span class="pre">conf.py</span></code> file by adding the following. First we tell it where to find the extensions:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># If your extensions are in another directory, add it here. If the</span> <span class="c1"># directory is relative to the documentation root, use</span> <span class="c1"># os.path.abspath to make it absolute, like shown here.</span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">abspath</span><span class="p">(</span><span class="s1">'sphinxext'</span><span class="p">))</span> </pre></div> </div> <p>And then we tell it what extensions to load:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Add any Sphinx extension module names here, as strings. They can be extensions</span> <span class="c1"># coming with Sphinx (named 'sphinx.ext.*') or your custom ones.</span> <span class="n">extensions</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'matplotlib.sphinxext.only_directives'</span><span class="p">,</span> <span class="s1">'matplotlib.sphinxext.plot_directive'</span><span class="p">,</span> <span class="s1">'IPython.sphinxext.ipython_directive'</span><span class="p">,</span> <span class="s1">'IPython.sphinxext.ipython_console_highlighting'</span><span class="p">,</span> <span class="s1">'sphinx.ext.mathjax'</span><span class="p">,</span> <span class="s1">'sphinx.ext.autodoc'</span><span class="p">,</span> <span class="s1">'sphinx.ext.doctest'</span><span class="p">,</span> <span class="s1">'sphinx.ext.inheritance_diagram'</span><span class="p">,</span> <span class="s1">'numpydoc'</span><span class="p">]</span> </pre></div> </div> <p>Now let’s look at some of these in action. You can see the literal source for this file at <span class="xref std std-ref">extensions-literal</span>.</p> <div class="section" id="ipython-sessions"> <span id="ipython-highlighting"></span><h2>ipython sessions<a class="headerlink" href="#ipython-sessions" title="Permalink to this headline">¶</a></h2> <p>Michael Droettboom contributed a sphinx extension which does <a class="reference external" href="http://pygments.org">pygments</a> syntax highlighting on <a class="reference external" href="http://ipython.scipy.org">ipython</a> sessions. Just use ipython as the language in the <code class="docutils literal"><span class="pre">sourcecode</span></code> directive:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">sourcecode</span><span class="p">::</span> <span class="n">ipython</span> <span class="n">In</span> <span class="p">[</span><span class="mi">69</span><span class="p">]:</span> <span class="n">lines</span> <span class="o">=</span> <span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span> <span class="n">In</span> <span class="p">[</span><span class="mi">70</span><span class="p">]:</span> <span class="n">setp</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="n">alpha</span><span class="p">:</span> <span class="nb">float</span> <span class="n">animated</span><span class="p">:</span> <span class="p">[</span><span class="kc">True</span> <span class="o">|</span> <span class="kc">False</span><span class="p">]</span> <span class="n">antialiased</span> <span class="ow">or</span> <span class="n">aa</span><span class="p">:</span> <span class="p">[</span><span class="kc">True</span> <span class="o">|</span> <span class="kc">False</span><span class="p">]</span> <span class="o">...</span><span class="n">snip</span> </pre></div> </div> <p>and you will get the syntax highlighted output below.</p> <div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [69]: </span><span class="n">lines</span> <span class="o">=</span> <span class="n">plot</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span> <span class="gp">In [70]: </span><span class="n">setp</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="go"> alpha: float</span> <span class="go"> animated: [True | False]</span> <span class="go"> antialiased or aa: [True | False]</span> <span class="go"> ...snip</span> </pre></div> </div> <p>This support is included in this template, but will also be included in a future version of Pygments by default.</p> </div> <div class="section" id="using-math"> <span id="id1"></span><h2>Using math<a class="headerlink" href="#using-math" title="Permalink to this headline">¶</a></h2> <p>In sphinx you can include inline math <span class="math">\(x\leftarrow y\ x\forall y\ x-y\)</span> or display math</p> <div class="math"> \[W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right]\]</div> <p>To include math in your document, just use the math directive; here is a simpler equation:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">math</span><span class="p">::</span> <span class="n">W</span><span class="o">^</span><span class="p">{</span><span class="mi">3</span>\<span class="n">beta</span><span class="p">}</span><span class="n">_</span><span class="p">{</span>\<span class="n">delta_1</span> \<span class="n">rho_1</span> \<span class="n">sigma_2</span><span class="p">}</span> \<span class="n">approx</span> <span class="n">U</span><span class="o">^</span><span class="p">{</span><span class="mi">3</span>\<span class="n">beta</span><span class="p">}</span><span class="n">_</span><span class="p">{</span>\<span class="n">delta_1</span> \<span class="n">rho_1</span><span class="p">}</span> </pre></div> </div> <p>which is rendered as</p> <div class="math"> \[W^{3\beta}_{\delta_1 \rho_1 \sigma_2} \approx U^{3\beta}_{\delta_1 \rho_1}\]</div> <p>Recent versions of Sphinx include built-in support for math. There are three flavors:</p> <blockquote> <div><ul class="simple"> <li>sphinx.ext.pngmath: uses dvipng to render the equation</li> <li>sphinx.ext.mathjax: renders the math in the browser using Javascript</li> <li>sphinx.ext.jsmath: it’s an older code, but it checks out</li> </ul> </div></blockquote> <p>Additionally, matplotlib has its own math support:</p> <blockquote> <div><ul class="simple"> <li>matplotlib.sphinxext.mathmpl</li> </ul> </div></blockquote> <p>See the matplotlib <a class="reference external" href="http://matplotlib.sourceforge.net/users/mathtext.html">mathtext guide</a> for lots more information on writing mathematical expressions in matplotlib.</p> </div> <div class="section" id="inserting-matplotlib-plots"> <span id="pyplots"></span><h2>Inserting matplotlib plots<a class="headerlink" href="#inserting-matplotlib-plots" title="Permalink to this headline">¶</a></h2> <p>Inserting automatically-generated plots is easy. Simply put the script to generate the plot in the <code class="file docutils literal"><span class="pre">pyplots</span></code> directory, and refer to it using the <code class="docutils literal"><span class="pre">plot</span></code> directive. First make a <code class="file docutils literal"><span class="pre">pyplots</span></code> directory at the top level of your project (next to :<code class="docutils literal"><span class="pre">conf.py</span></code>) and copy the <code class="file docutils literal"><span class="pre">ellipses.py`</span></code> file into it:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">home</span><span class="p">:</span><span class="o">~/</span><span class="n">tmp</span><span class="o">/</span><span class="n">sampledoc</span><span class="o">></span> <span class="n">mkdir</span> <span class="n">pyplots</span> <span class="n">home</span><span class="p">:</span><span class="o">~/</span><span class="n">tmp</span><span class="o">/</span><span class="n">sampledoc</span><span class="o">></span> <span class="n">cp</span> <span class="o">../</span><span class="n">sampledoc_tut</span><span class="o">/</span><span class="n">pyplots</span><span class="o">/</span><span class="n">ellipses</span><span class="o">.</span><span class="n">py</span> <span class="n">pyplots</span><span class="o">/</span> </pre></div> </div> <p>You can refer to this file in your sphinx documentation; by default it will just inline the plot with links to the source and PF and high resolution PNGS. To also include the source code for the plot in the document, pass the <code class="docutils literal"><span class="pre">include-source</span></code> parameter:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">plot</span><span class="p">::</span> <span class="n">pyplots</span><span class="o">/</span><span class="n">ellipses</span><span class="o">.</span><span class="n">py</span> <span class="p">:</span><span class="n">include</span><span class="o">-</span><span class="n">source</span><span class="p">:</span> </pre></div> </div> <p>In the HTML version of the document, the plot includes links to the original source code, a high-resolution PNG and a PDF. In the PDF version of the document, the plot is included as a scalable PDF.</p> <div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pylab</span> <span class="kn">import</span> <span class="o">*</span> <span class="kn">from</span> <span class="nn">matplotlib.patches</span> <span class="kn">import</span> <span class="n">Ellipse</span> <span class="n">delta</span> <span class="o">=</span> <span class="mf">45.0</span> <span class="c1"># degrees</span> <span class="n">angles</span> <span class="o">=</span> <span class="n">arange</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">360</span><span class="o">+</span><span class="n">delta</span><span class="p">,</span> <span class="n">delta</span><span class="p">)</span> <span class="n">ells</span> <span class="o">=</span> <span class="p">[</span><span class="n">Ellipse</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">angles</span><span class="p">]</span> <span class="n">a</span> <span class="o">=</span> <span class="n">subplot</span><span class="p">(</span><span class="mi">111</span><span class="p">,</span> <span class="n">aspect</span><span class="o">=</span><span class="s1">'equal'</span><span class="p">)</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">ells</span><span class="p">:</span> <span class="n">e</span><span class="o">.</span><span class="n">set_clip_box</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">bbox</span><span class="p">)</span> <span class="n">e</span><span class="o">.</span><span class="n">set_alpha</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span> <span class="n">a</span><span class="o">.</span><span class="n">add_artist</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="n">xlim</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>(<a class="reference external" href="./pyplots/ellipses.py">Source code</a>, <a class="reference external" href="./pyplots/ellipses.png">png</a>, <a class="reference external" href="./pyplots/ellipses.hires.png">hires.png</a>, <a class="reference external" href="./pyplots/ellipses.pdf">pdf</a>)</p> <div class="figure"> <img alt="_images/ellipses.png" src="_images/ellipses.png" /> </div> <p>You can also inline code for plots directly, and the code will be executed at documentation build time and the figure inserted into your docs; the following code:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">plot</span><span class="p">::</span> <span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">hist</span><span class="p">(</span> <span class="n">x</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="n">plt</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span> <span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">r'Normal: $\mu=</span><span class="si">%.2f</span><span class="s1">, \sigma=</span><span class="si">%.2f</span><span class="s1">$'</span><span class="o">%</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">mean</span><span class="p">(),</span> <span class="n">x</span><span class="o">.</span><span class="n">std</span><span class="p">()))</span> <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span> </pre></div> </div> <p>produces this output:</p> <p>(<a class="reference external" href=".//extensions-1.py">Source code</a>, <a class="reference external" href=".//extensions-1.png">png</a>, <a class="reference external" href=".//extensions-1.hires.png">hires.png</a>, <a class="reference external" href=".//extensions-1.pdf">pdf</a>)</p> <div class="figure"> <img alt="_images/extensions-1.png" src="_images/extensions-1.png" /> </div> <p>See the matplotlib <a class="reference external" href="http://matplotlib.sourceforge.net/users/pyplot_tutorial.html">pyplot tutorial</a> and the <a class="reference external" href="http://matplotlib.sourceforge.net/gallery.html">gallery</a> for lots of examples of matplotlib plots.</p> </div> <div class="section" id="inheritance-diagrams"> <h2>Inheritance diagrams<a class="headerlink" href="#inheritance-diagrams" title="Permalink to this headline">¶</a></h2> <p>Inheritance diagrams can be inserted directly into the document by providing a list of class or module names to the <code class="docutils literal"><span class="pre">inheritance-diagram</span></code> directive.</p> <p>For example:</p> <div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">inheritance</span><span class="o">-</span><span class="n">diagram</span><span class="p">::</span> <span class="n">codecs</span> </pre></div> </div> <p>produces:</p> <img src="_images/inheritance-9b9f0ab18aee8e265d88f8c102de49defb25c694.png" alt="Inheritance diagram of codecs" usemap="#inheritance7e56675552" class="inheritance"/> <map id="inheritance7e56675552" name="inheritance7e56675552"> <area shape="rect" id="node1" title="This subclass of IncrementalDecoder can be used as the baseclass for an" alt="" coords="256,276,515,301"/> <area shape="rect" id="node2" title="An IncrementalDecoder decodes an input in multiple steps. The input can" alt="" coords="5,276,208,301"/> <area shape="rect" id="node3" title="This subclass of IncrementalEncoder can be used as the baseclass for an" alt="" coords="257,227,513,252"/> <area shape="rect" id="node4" title="An IncrementalEncoder encodes an input in multiple steps. The input can" alt="" coords="6,227,207,252"/> <area shape="rect" id="node5" title="Defines the interface for stateless encoders/decoders." alt="" coords="51,153,162,179"/> <area shape="rect" id="node6" title="Codec details when looking up the codec registry" alt="" coords="39,104,175,129"/> <area shape="rect" id="node8" title="StreamReaderWriter instances allow wrapping streams which" alt="" coords="5,55,208,80"/> <area shape="rect" id="node9" title="StreamRecoder instances translate data from one encoding to another." alt="" coords="21,5,192,31"/> </map> <p id="extensions-literal">See the <a class="reference internal" href="ipython_directive.html#ipython-directive"><span class="std std-ref">Ipython Directive</span></a> for a tutorial on embedding stateful, matplotlib aware ipython sessions into your rest docs with multiline and doctest support.</p> </div> <div class="section" id="this-file"> <h2>This file<a class="headerlink" href="#this-file" title="Permalink to this headline">¶</a></h2> <div class="highlight-default"><div class="highlight"><pre><span></span>.. _extensions: **************************************************** Sphinx extensions for embedded plots, math and more **************************************************** Sphinx is written in python, and supports the ability to write custom extensions. We've written a few for the matplotlib documentation, some of which are part of matplotlib itself in the matplotlib.sphinxext module, some of which are included only in the sphinx doc directory, and there are other extensions written by other groups, eg numpy and ipython. We're collecting these in this tutorial and showing you how to install and use them for your own project. First let's grab the python extension files from the :file:`sphinxext` directory from git (see :ref:`fetching-the-data`), and install them in our :file:`sampledoc` project :file:`sphinxext` directory:: home:~/tmp/sampledoc> mkdir sphinxext home:~/tmp/sampledoc> cp ../sampledoc_tut/sphinxext/*.py sphinxext/ home:~/tmp/sampledoc> ls sphinxext/ apigen.py docscrape.py docscrape_sphinx.py numpydoc.py In addition to the builtin matplotlib extensions for embedding pyplot plots and rendering math with matplotlib's native math engine, we also have extensions for syntax highlighting ipython sessions, making inhertiance diagrams, and more. We need to inform sphinx of our new extensions in the :file:`conf.py` file by adding the following. First we tell it where to find the extensions:: # If your extensions are in another directory, add it here. If the # directory is relative to the documentation root, use # os.path.abspath to make it absolute, like shown here. sys.path.append(os.path.abspath('sphinxext')) And then we tell it what extensions to load:: # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = ['matplotlib.sphinxext.only_directives', 'matplotlib.sphinxext.plot_directive', 'IPython.sphinxext.ipython_directive', 'IPython.sphinxext.ipython_console_highlighting', 'sphinx.ext.mathjax', 'sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.inheritance_diagram', 'numpydoc'] Now let's look at some of these in action. You can see the literal source for this file at :ref:`extensions-literal`. .. _ipython-highlighting: ipython sessions ================ Michael Droettboom contributed a sphinx extension which does `pygments <http://pygments.org>`_ syntax highlighting on `ipython <http://ipython.scipy.org>`_ sessions. Just use ipython as the language in the ``sourcecode`` directive:: .. sourcecode:: ipython In [69]: lines = plot([1,2,3]) In [70]: setp(lines) alpha: float animated: [True | False] antialiased or aa: [True | False] ...snip and you will get the syntax highlighted output below. .. sourcecode:: ipython In [69]: lines = plot([1,2,3]) In [70]: setp(lines) alpha: float animated: [True | False] antialiased or aa: [True | False] ...snip This support is included in this template, but will also be included in a future version of Pygments by default. .. _using-math: Using math ========== In sphinx you can include inline math :math:`x\leftarrow y\ x\forall y\ x-y` or display math .. math:: W^{3\beta}_{\delta_1 \rho_1 \sigma_2} = U^{3\beta}_{\delta_1 \rho_1} + \frac{1}{8 \pi 2} \int^{\alpha_2}_{\alpha_2} d \alpha^\prime_2 \left[\frac{ U^{2\beta}_{\delta_1 \rho_1} - \alpha^\prime_2U^{1\beta}_{\rho_1 \sigma_2} }{U^{0\beta}_{\rho_1 \sigma_2}}\right] To include math in your document, just use the math directive; here is a simpler equation:: .. math:: W^{3\beta}_{\delta_1 \rho_1 \sigma_2} \approx U^{3\beta}_{\delta_1 \rho_1} which is rendered as .. math:: W^{3\beta}_{\delta_1 \rho_1 \sigma_2} \approx U^{3\beta}_{\delta_1 \rho_1} Recent versions of Sphinx include built-in support for math. There are three flavors: - sphinx.ext.pngmath: uses dvipng to render the equation - sphinx.ext.mathjax: renders the math in the browser using Javascript - sphinx.ext.jsmath: it's an older code, but it checks out Additionally, matplotlib has its own math support: - matplotlib.sphinxext.mathmpl See the matplotlib `mathtext guide <http://matplotlib.sourceforge.net/users/mathtext.html>`_ for lots more information on writing mathematical expressions in matplotlib. .. _pyplots: Inserting matplotlib plots ========================== Inserting automatically-generated plots is easy. Simply put the script to generate the plot in the :file:`pyplots` directory, and refer to it using the ``plot`` directive. First make a :file:`pyplots` directory at the top level of your project (next to :``conf.py``) and copy the :file:`ellipses.py`` file into it:: home:~/tmp/sampledoc> mkdir pyplots home:~/tmp/sampledoc> cp ../sampledoc_tut/pyplots/ellipses.py pyplots/ You can refer to this file in your sphinx documentation; by default it will just inline the plot with links to the source and PF and high resolution PNGS. To also include the source code for the plot in the document, pass the ``include-source`` parameter:: .. plot:: pyplots/ellipses.py :include-source: In the HTML version of the document, the plot includes links to the original source code, a high-resolution PNG and a PDF. In the PDF version of the document, the plot is included as a scalable PDF. .. plot:: pyplots/ellipses.py :include-source: You can also inline code for plots directly, and the code will be executed at documentation build time and the figure inserted into your docs; the following code:: .. plot:: import matplotlib.pyplot as plt import numpy as np x = np.random.randn(1000) plt.hist( x, 20) plt.grid() plt.title(r'Normal: $\mu=%.2f, \sigma=%.2f$'%(x.mean(), x.std())) plt.show() produces this output: .. plot:: import matplotlib.pyplot as plt import numpy as np x = np.random.randn(1000) plt.hist( x, 20) plt.grid() plt.title(r'Normal: $\mu=%.2f, \sigma=%.2f$'%(x.mean(), x.std())) plt.show() See the matplotlib `pyplot tutorial <http://matplotlib.sourceforge.net/users/pyplot_tutorial.html>`_ and the `gallery <http://matplotlib.sourceforge.net/gallery.html>`_ for lots of examples of matplotlib plots. Inheritance diagrams ==================== Inheritance diagrams can be inserted directly into the document by providing a list of class or module names to the ``inheritance-diagram`` directive. For example:: .. inheritance-diagram:: codecs produces: .. inheritance-diagram:: codecs .. _extensions-literal: See the :ref:`ipython_directive` for a tutorial on embedding stateful, matplotlib aware ipython sessions into your rest docs with multiline and doctest support. This file ========= .. literalinclude:: extensions.rst </pre></div> </div> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="ipython_directive.html" title="Ipython Directive" >next</a> |</li> <li class="right" > <a href="custom_look.html" title="Customizing the look and feel of the site" >previous</a> |</li> <li><a href="index.html">home</a>| </li> <li><a href="search.html">search</a>| </li> </ul> </div> <div class="footer" role="contentinfo"> © Copyright 2009, John Hunter, Fernando Perez, Michael Droettboom. Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1. </div> </body> </html> |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |