forked from matplotlib/matplotlib.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextensions.html
More file actions
558 lines (458 loc) · 26.5 KB
/
Copy pathextensions.html
File metadata and controls
558 lines (458 loc) · 26.5 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
<!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/default.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
};
</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>
<link rel="top" title="sampledoc 1.0 documentation" href="index.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>
<div style="background-color: white; text-align: left; padding: 10px 10px 15px 15px">
<a href="index.html"><img src="_static/logo.png" border="0" alt="py4sci"/></a>
</div>
<div class="related">
<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">
<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>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/extensions.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<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 <tt class="file docutils literal"><span class="pre">sphinxext</span></tt>
directory from git (see <a class="reference internal" href="getting_started.html#fetching-the-data"><em>Fetching the data</em></a>), and install them in
our <tt class="file docutils literal"><span class="pre">sampledoc</span></tt> project <tt class="file docutils literal"><span class="pre">sphinxext</span></tt> directory:</p>
<div class="highlight-python"><pre>home:~/tmp/sampledoc> mkdir sphinxext
home:~/tmp/sampledoc> cp ../sampledoc_tut/sphinxext/*.py sphinxext/
home:~/tmp/sampledoc> ls sphinxext/
apigen.py docscrape_sphinx.py ipython_console_highlighting.py
docscrape.py inheritance_diagram.py numpydoc.py</pre>
</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 <tt class="file docutils literal"><span class="pre">conf.py</span></tt>
file by adding the following. First we tell it where to find the extensions:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># If your extensions are in another directory, add it here. If the</span>
<span class="c"># directory is relative to the documentation root, use</span>
<span class="c"># 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="s">'sphinxext'</span><span class="p">))</span>
</pre></div>
</div>
<p>And then we tell it what extensions to load:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Add any Sphinx extension module names here, as strings. They can</span>
<span class="c"># be extensions coming with Sphinx (named 'sphinx.ext.*') or your</span>
<span class="c"># custom ones.</span>
<span class="n">extensions</span> <span class="o">=</span> <span class="p">[</span>
<span class="s">'matplotlib.sphinxext.mathmpl'</span><span class="p">,</span>
<span class="s">'matplotlib.sphinxext.only_directives'</span><span class="p">,</span>
<span class="s">'matplotlib.sphinxext.plot_directive'</span><span class="p">,</span>
<span class="s">'matplotlib.sphinxext.ipython_directive'</span><span class="p">,</span>
<span class="s">'sphinx.ext.autodoc'</span><span class="p">,</span>
<span class="s">'sphinx.ext.doctest'</span><span class="p">,</span>
<span class="s">'ipython_console_highlighting'</span><span class="p">,</span>
<span class="s">'inheritance_diagram'</span><span class="p">,</span>
<span class="s">'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 <em class="xref std std-ref">extensions-literal</em>.</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 <tt class="docutils literal"><span class="pre">sourcecode</span></tt> directive:</p>
<div class="highlight-python"><pre>.. sourcecode:: ipython
In [69]: lines = plot([1,2,3])
In [70]: setp(lines)
alpha: float
animated: [True | False]
antialiased or aa: [True | False]
...snip</pre>
</div>
<p>and you will get the syntax highlighted output below.</p>
<div class="highlight-ipython"><div class="highlight"><pre><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 <img src="_images/mathmpl/math-3f38e019c8.png" /> or display math</p>
<img src="_images/mathmpl/math-9141e90839.png" class="center" /><p>To include math in your document, just use the math directive; here is
a simpler equation:</p>
<div class="highlight-python"><pre>.. math::
W^{3\beta}_{\delta_1 \rho_1 \sigma_2} \approx U^{3\beta}_{\delta_1 \rho_1}</pre>
</div>
<p>which is rendered as</p>
<img src="_images/mathmpl/math-d9a8b00dd5.png" class="center" /><p>This documentation framework includes a Sphinx extension,
<tt class="file docutils literal"><span class="pre">sphinxext/mathmpl.py</span></tt>, that uses matplotlib to render math
equations when generating HTML, and LaTeX itself when generating a
PDF. This can be useful on systems that have matplotlib, but not
LaTeX, installed. To use it, add <tt class="docutils literal"><span class="pre">mathmpl</span></tt> to the list of
extensions in <tt class="file docutils literal"><span class="pre">conf.py</span></tt>.</p>
<p>Current SVN versions of Sphinx now include built-in support for math.
There are two flavors:</p>
<blockquote>
<div><ul class="simple">
<li>pngmath: uses dvipng to render the equation</li>
<li>jsmath: renders the math in the browser using Javascript</li>
</ul>
</div></blockquote>
<p>To use these extensions instead, add <tt class="docutils literal"><span class="pre">sphinx.ext.pngmath</span></tt> or
<tt class="docutils literal"><span class="pre">sphinx.ext.jsmath</span></tt> to the list of extensions in <tt class="file docutils literal"><span class="pre">conf.py</span></tt>.</p>
<p>All three of these options for math are designed to behave in the same
way.</p>
<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 <tt class="file docutils literal"><span class="pre">pyplots</span></tt> directory, and
refer to it using the <tt class="docutils literal"><span class="pre">plot</span></tt> directive. First make a
<tt class="file docutils literal"><span class="pre">pyplots</span></tt> directory at the top level of your project (next to
:<tt class="docutils literal"><span class="pre">conf.py</span></tt>) and copy the <tt class="file docutils literal"><span class="pre">ellipses.py`</span></tt> file into it:</p>
<div class="highlight-python"><pre>home:~/tmp/sampledoc> mkdir pyplots
home:~/tmp/sampledoc> cp ../sampledoc_tut/pyplots/ellipses.py pyplots/</pre>
</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 <tt class="docutils literal"><span class="pre">include-source</span></tt> parameter:</p>
<div class="highlight-python"><pre>.. plot:: pyplots/ellipses.py
:include-source:</pre>
</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 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="c"># 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="s">'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-python"><pre>.. 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()</pre>
</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
<tt class="docutils literal"><span class="pre">inheritance-diagram</span></tt> directive.</p>
<p>For example:</p>
<div class="highlight-python"><pre>.. inheritance-diagram:: codecs</pre>
</div>
<p>produces:</p>
<img src="_images/inheritance7e56675552.png" usemap="#inheritance7e56675552" class="inheritance"/><map id="inheritance7e56675552" name="inheritance7e56675552">
</map>
<p id="extensions-literal">See the <a class="reference internal" href="ipython_directive.html#ipython-directive"><em>Ipython Directive</em></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-python"><pre>.. _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_sphinx.py ipython_console_highlighting.py
docscrape.py inheritance_diagram.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.mathmpl',
'matplotlib.sphinxext.only_directives',
'matplotlib.sphinxext.plot_directive',
'matplotlib.sphinxext.ipython_directive',
'sphinx.ext.autodoc',
'sphinx.ext.doctest',
'ipython_console_highlighting',
'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}
This documentation framework includes a Sphinx extension,
:file:`sphinxext/mathmpl.py`, that uses matplotlib to render math
equations when generating HTML, and LaTeX itself when generating a
PDF. This can be useful on systems that have matplotlib, but not
LaTeX, installed. To use it, add ``mathmpl`` to the list of
extensions in :file:`conf.py`.
Current SVN versions of Sphinx now include built-in support for math.
There are two flavors:
- pngmath: uses dvipng to render the equation
- jsmath: renders the math in the browser using Javascript
To use these extensions instead, add ``sphinx.ext.pngmath`` or
``sphinx.ext.jsmath`` to the list of extensions in :file:`conf.py`.
All three of these options for math are designed to behave in the same
way.
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 class="clearer"></div>
</div>
<div class="related">
<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">
© Copyright 2009, John Hunter, Fernando Perez, Michael Droettboom.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>