Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5fe1a1

Browse files
committed
Include some examples inline in the api docs.
svn path=/trunk/matplotlib/; revision=5681
1 parent c625af2 commit a5fe1a1

2 files changed

Lines changed: 66 additions & 64 deletions

File tree

doc/sphinxext/plot_directive.py

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
source will be included inline, as well as a link to the source.
1414
"""
1515

16-
import sys, os, glob
16+
import sys, os, glob, shutil
1717
from docutils.parsers.rst import directives
1818

1919
try:
@@ -40,29 +40,10 @@
4040
'class': directives.class_option,
4141
'include-source': directives.flag }
4242

43-
template_no_source = """
43+
template = """
4444
.. htmlonly::
4545
46-
[ `<../%(srcdir)s/%(reference)s>`__,
47-
`png <../%(srcdir)s/%(basename)s.hires.png>`__,
48-
`pdf <../%(srcdir)s/%(basename)s.pdf>`__]
49-
50-
.. image:: ../%(srcdir)s/%(basename)s.png
51-
%(options)s
52-
53-
54-
.. latexonly::
55-
.. image:: ../%(srcdir)s/%(basename)s.pdf
56-
%(options)s
57-
58-
"""
59-
60-
template_source = """
61-
.. literalinclude:: ../pyplots/%(reference)s
62-
63-
.. htmlonly::
64-
65-
[ `py <../%(srcdir)s/%(reference)s>`__,
46+
[`py <../%(srcdir)s/%(basename)s.py>`__,
6647
`png <../%(srcdir)s/%(basename)s.hires.png>`__,
6748
`pdf <../%(srcdir)s/%(basename)s.pdf>`__]
6849
@@ -90,6 +71,9 @@ def makefig(fullpath, outdir):
9071
basename, ext = os.path.splitext(fname)
9172
all_exists = True
9273

74+
if basedir != outdir:
75+
shutil.copyfile(fullpath, os.path.join(outdir, fname))
76+
9377
for format, dpi in formats:
9478
outname = os.path.join(outdir, '%s.%s' % (basename, format))
9579
if not os.path.exists(outname):
@@ -109,10 +93,10 @@ def makefig(fullpath, outdir):
10993
if os.path.exists(outname): continue
11094
plt.savefig(outname, dpi=dpi)
11195

112-
11396
def run(arguments, options, state_machine, lineno):
11497
reference = directives.uri(arguments[0])
115-
basename, ext = os.path.splitext(reference)
98+
basedir, fname = os.path.split(reference)
99+
basename, ext = os.path.splitext(fname)
116100

117101
# todo - should we be using the _static dir for the outdir, I am
118102
# not sure we want to corrupt that dir with autogenerated files
@@ -126,16 +110,16 @@ def run(arguments, options, state_machine, lineno):
126110
# the figs into the right place, so we may want to do that here instead.
127111

128112
if options.has_key('include-source'):
129-
template = template_source
113+
lines = ['.. literalinclude:: ../pyplots/%(reference)s' % locals()]
130114
del options['include-source']
131115
else:
132-
template = template_no_source
116+
lines = []
117+
133118
options = [' :%s: %s' % (key, val) for key, val in
134119
options.items()]
135120
options = "\n".join(options)
136121

137-
lines = template % locals()
138-
lines = lines.split('\n')
122+
lines.extend((template % locals()).split('\n'))
139123

140124
state_machine.insert_input(
141125
lines, state_machine.input_lines.source(0))

lib/matplotlib/axes.py

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2879,6 +2879,9 @@ def hlines(self, y, xmin, xmax, colors='k', linestyles='solid',
28792879
*linestyle*:
28802880
[ 'solid' | 'dashed' | 'dashdot' | 'dotted' ]
28812881
2882+
**Example:**
2883+
2884+
.. plot:: ../mpl_examples/pylab_examples/hline_demo.py
28822885
"""
28832886
if kwargs.get('fmt') is not None:
28842887
raise DeprecationWarning(
@@ -3362,6 +3365,13 @@ def acorr(self, x, **kwargs):
33623365
See the respective :meth:`~matplotlib.axes.Axes.plot` or
33633366
:meth:`~matplotlib.axes.Axes.vlines` functions for
33643367
documentation on valid kwargs.
3368+
3369+
**Example:**
3370+
3371+
:func:`~matplotlib.pyplot.xcorr` above, and
3372+
:func:`~matplotlib.pyplot.acorr` below.
3373+
3374+
.. plot:: ../mpl_examples/pylab_examples/xcorr_demo.py
33653375
"""
33663376
return self.xcorr(x, x, **kwargs)
33673377
acorr.__doc__ = cbook.dedent(acorr.__doc__) % martist.kwdocd
@@ -3410,6 +3420,13 @@ def xcorr(self, x, y, normed=False, detrend=mlab.detrend_none, usevlines=False,
34103420
34113421
*maxlags* is a positive integer detailing the number of lags to show.
34123422
The default value of *None* will return all ``(2*len(x)-1)`` lags.
3423+
3424+
**Example:**
3425+
3426+
:func:`~matplotlib.pyplot.xcorr` above, and
3427+
:func:`~matplotlib.pyplot.acorr` below.
3428+
3429+
.. plot:: ../mpl_examples/pylab_examples/xcorr_demo.py
34133430
"""
34143431

34153432
Nx = len(x)
@@ -3706,6 +3723,12 @@ def bar(self, left, height, width=0.8, bottom=None,
37063723
Other optional kwargs:
37073724
37083725
%(Rectangle)s
3726+
3727+
**Example:**
3728+
3729+
A stacked bar chart.
3730+
3731+
.. plot:: ../mpl_examples/pylab_examples/bar_stacked.py
37093732
"""
37103733
if not self._hold: self.cla()
37113734

@@ -3998,6 +4021,10 @@ def broken_barh(self, xranges, yrange, **kwargs):
39984021
or a sequence of arguments for the various bars, ie::
39994022
40004023
facecolors = ('black', 'red', 'green')
4024+
4025+
**Example:**
4026+
4027+
.. plot:: ../mpl_examples/pylab_examples/broken_barh.py
40014028
"""
40024029
col = mcoll.BrokenBarHCollection(xranges, yrange, **kwargs)
40034030
self.add_collection(col, autolim=True)
@@ -5013,6 +5040,10 @@ def hexbin(self, x, y, gridsize = 100, bins = None,
50135040
:meth:`~matplotlib.collection.PolyCollection.get_array` on
50145041
this :class:`~matplotlib.collections.PolyCollection` to get
50155042
the counts in each hexagon.
5043+
5044+
**Example:**
5045+
5046+
.. plot:: ../mpl_examples/pylab_examples/hexbin_demo.py
50165047
"""
50175048

50185049
if not self._hold: self.cla()
@@ -5166,6 +5197,10 @@ def arrow(self, x, y, dx, dy, **kwargs):
51665197
51675198
Optional kwargs control the arrow properties:
51685199
%(FancyArrow)s
5200+
5201+
**Example:**
5202+
5203+
.. plot:: ../mpl_examples/pylab_examples/arrow_demo.py
51695204
"""
51705205
a = mpatches.FancyArrow(x, y, dx, dy, **kwargs)
51715206
self.add_artist(a)
@@ -5225,6 +5260,10 @@ def fill(self, *args, **kwargs):
52255260
kwargs control the Polygon properties:
52265261
52275262
%(Polygon)s
5263+
5264+
**Example:**
5265+
5266+
.. plot:: ../mpl_examples/pylab_examples/fill_demo.py
52285267
"""
52295268
if not self._hold: self.cla()
52305269

@@ -5334,6 +5373,10 @@ def imshow(self, X, cmap=None, norm=None, aspect=None,
53345373
Additional kwargs are :class:`~matplotlib.artist.Artist` properties:
53355374
53365375
%(Artist)s
5376+
5377+
**Example:**
5378+
5379+
.. plot:: ../mpl_examples/pylab_examples/image_demo.py
53375380
"""
53385381

53395382
if not self._hold: self.cla()
@@ -6081,42 +6124,6 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
60816124
60826125
%(Rectangle)s
60836126
6084-
Here is an example which generates a histogram of normally
6085-
distributed random numbers and plot the analytic PDF over it::
6086-
6087-
import numpy as np
6088-
import matplotlib.pyplot as plt
6089-
import matplotlib.mlab as mlab
6090-
6091-
mu, sigma = 100, 15
6092-
x = mu + sigma * np.random.randn(10000)
6093-
6094-
fig = plt.figure()
6095-
ax = fig.add_subplot(111)
6096-
6097-
# the histogram of the data
6098-
n, bins, patches = ax.hist(x, 50, normed=1, facecolor='green', alpha=0.75)
6099-
6100-
# hist uses np.histogram under the hood to create 'n' and 'bins'.
6101-
# np.histogram returns the bin edges, so there will be 50 probability
6102-
# density values in n, 51 bin edges in bins and 50 patches. To get
6103-
# everything lined up, we'll compute the bin centers
6104-
bincenters = 0.5*(bins[1:]+bins[:-1])
6105-
6106-
# add a 'best fit' line for the normal PDF
6107-
y = mlab.normpdf( bincenters, mu, sigma)
6108-
l = ax.plot(bincenters, y, 'r--', linewidth=1)
6109-
6110-
ax.set_xlabel('Smarts')
6111-
ax.set_ylabel('Probability')
6112-
ax.set_title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
6113-
ax.set_xlim(40, 160)
6114-
ax.set_ylim(0, 0.03)
6115-
ax.grid(True)
6116-
6117-
#fig.savefig('histogram_demo',dpi=72)
6118-
plt.show()
6119-
61206127
You can use labels for your histogram, and only the first
61216128
:class:`~matplotlib.patches.Rectangle` gets the label (the
61226129
others get the magic string '_nolegend_'. This will make the
@@ -6126,6 +6133,9 @@ def hist(self, x, bins=10, range=None, normed=False, cumulative=False,
61266133
ax.hist(12+3*np.random.randn(1000), label='women', alpha=0.5)
61276134
ax.legend()
61286135
6136+
**Example:**
6137+
6138+
.. plot:: ../mpl_examples/pylab_examples/histogram_demo.py
61296139
"""
61306140
if not self._hold: self.cla()
61316141

@@ -6413,6 +6423,10 @@ def csd(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
64136423
kwargs control the Line2D properties:
64146424
64156425
%(Line2D)s
6426+
6427+
**Example:**
6428+
6429+
.. plot:: ../mpl_examples/pylab_examples/csd_demo.py
64166430
"""
64176431
if not self._hold: self.cla()
64186432
pxy, freqs = mlab.csd(x, y, NFFT, Fs, detrend, window, noverlap)
@@ -6466,6 +6480,10 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none,
64666480
properties of the coherence plot:
64676481
64686482
%(Line2D)s
6483+
6484+
**Example:**
6485+
6486+
.. plot:: ../mpl_examples/pylab_examples/cohere_demo.py
64696487
"""
64706488
if not self._hold: self.cla()
64716489
cxy, freqs = mlab.cohere(x, y, NFFT, Fs, detrend, window, noverlap)

0 commit comments

Comments
 (0)