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

Skip to content

Commit 218b550

Browse files
committed
Merge pull request #1924 from tonysyu/gallery-cleanup-rebase
MEP 12: Gallery cleanup and reorganization
2 parents 386b4e5 + aa6b410 commit 218b550

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+988
-771
lines changed

doc/conf.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,29 @@
104104

105105
plot_formats = [('png', 80), ('hires.png', 200), ('pdf', 50)]
106106

107+
# Subdirectories in 'examples/' directory of package and titles for gallery
108+
# TODO: Change to OrderedDict when Matplotlib drops support for Python < 2.7
109+
mpl_example_sections = (
110+
('lines_bars_and_markers', 'Lines, bars, and markers'),
111+
('shapes_and_collections', 'Shapes and collections'),
112+
('statistics', 'Statistical plots'),
113+
('images_contours_and_fields', 'Images, contours, and fields'),
114+
('pie_and_polar_charts', 'Pie and polar charts'),
115+
('color', 'Color'),
116+
('text_labels_and_annotations', 'Text, labels, and annotations'),
117+
('ticks_and_spines', 'Ticks and spines'),
118+
('subplots_axes_and_figures', 'Subplots, axes, and figures'),
119+
('specialty_plots', 'Specialty plots'),
120+
('showcase', 'Showcase'),
121+
('api', 'API'),
122+
('pylab_examples', 'pylab examples'),
123+
('mplot3d', 'mplot3d toolkit'),
124+
('axes_grid', 'axes_grid toolkit'),
125+
('units', 'units'),
126+
('widgets', 'widgets'),
127+
)
128+
129+
107130
# Github extension
108131

109132
github_project_url = "http://github.com/matplotlib/matplotlib/"

doc/sphinxext/gen_gallery.py

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,74 @@
11
# -*- coding: UTF-8 -*-
2+
import os
3+
import re
4+
import glob
5+
import warnings
6+
7+
import sphinx.errors
8+
9+
import matplotlib.image as image
10+
11+
12+
exclude_example_sections = ['units']
13+
multiimage = re.compile('(.*?)(_\d\d){1,2}')
214

315
# generate a thumbnail gallery of examples
4-
template = """\
5-
{%% extends "layout.html" %%}
6-
{%% set title = "Thumbnail gallery" %%}
16+
gallery_template = """\
17+
{{% extends "layout.html" %}}
18+
{{% set title = "Thumbnail gallery" %}}
719
820
9-
{%% block body %%}
21+
{{% block body %}}
1022
1123
<h3>Click on any image to see full size image and source code</h3>
1224
<br/>
1325
14-
<li><a class="reference internal" href="#">Gallery</a><ul>
15-
%s
16-
</ul>
26+
<li><a class="reference internal" href="#">Gallery</a>
27+
<ul>
28+
{toc}
29+
</ul>
1730
</li>
1831
19-
%s
20-
{%% endblock %%}
32+
{gallery}
33+
34+
{{% endblock %}}
2135
"""
2236

23-
import os, glob, re, sys, warnings
24-
import matplotlib.image as image
37+
header_template = """\
38+
<div class="section" id="{section}">
39+
<h4>
40+
{title}<a class="headerlink" href="#{section}" title="Permalink to this headline">¶</a>
41+
</h4>"""
42+
43+
link_template = """\
44+
<a href="{link}"><img src="{thumb}" border="0" alt="{basename}"/></a>
45+
"""
46+
47+
toc_template = """\
48+
<li><a class="reference internal" href="#{section}">{title}</a></li>"""
2549

26-
multiimage = re.compile('(.*?)(_\d\d){1,2}')
2750

2851
def make_thumbnail(args):
2952
image.thumbnail(args[0], args[1], 0.3)
3053

54+
3155
def out_of_date(original, derived):
3256
return (not os.path.exists(derived) or
3357
os.stat(derived).st_mtime < os.stat(original).st_mtime)
3458

59+
3560
def gen_gallery(app, doctree):
3661
if app.builder.name != 'html':
3762
return
3863

3964
outdir = app.builder.outdir
4065
rootdir = 'plot_directive/mpl_examples'
4166

67+
example_sections = list(app.builder.config.mpl_example_sections)
68+
for i, (subdir, title) in enumerate(example_sections):
69+
if subdir in exclude_example_sections:
70+
example_sections.pop(i)
71+
4272
# images we want to skip for the gallery because they are an unusual
4373
# size that doesn't layout well in a table, or because they may be
4474
# redundant with other images or uninteresting
@@ -53,21 +83,9 @@ def gen_gallery(app, doctree):
5383
rows = []
5484
toc_rows = []
5585

56-
link_template = """\
57-
<a href="%s"><img src="%s" border="0" alt="%s"/></a>
58-
"""
59-
60-
header_template = """<div class="section" id="%s">\
61-
<h4>%s<a class="headerlink" href="#%s" title="Permalink to this headline">¶</a></h4>"""
62-
63-
toc_template = """\
64-
<li><a class="reference internal" href="#%s">%s</a></li>"""
65-
66-
dirs = ('api', 'pylab_examples', 'mplot3d', 'widgets', 'axes_grid' )
67-
68-
for subdir in dirs :
69-
rows.append(header_template % (subdir, subdir, subdir))
70-
toc_rows.append(toc_template % (subdir, subdir))
86+
for subdir, title in example_sections:
87+
rows.append(header_template.format(title=title, section=subdir))
88+
toc_rows.append(toc_template.format(title=title, section=subdir))
7189

7290
origdir = os.path.join('build', rootdir, subdir)
7391
thumbdir = os.path.join(outdir, rootdir, subdir, 'thumbnails')
@@ -99,33 +117,34 @@ def gen_gallery(app, doctree):
99117
data.append((subdir, basename,
100118
os.path.join(rootdir, subdir, 'thumbnails', filename)))
101119

102-
103-
104-
105120
for (subdir, basename, thumbfile) in data:
106121
if thumbfile is not None:
107122
link = 'examples/%s/%s.html'%(subdir, basename)
108-
rows.append(link_template%(link, thumbfile, basename))
123+
rows.append(link_template.format(link=link,
124+
thumb=thumbfile,
125+
basename=basename))
109126

110127
if len(data) == 0:
111128
warnings.warn("No thumbnails were found in %s" % subdir)
112129

113130
# Close out the <div> opened up at the top of this loop
114131
rows.append("</div>")
115132

116-
content = template % ('\n'.join(toc_rows),
117-
'\n'.join(rows))
133+
content = gallery_template.format(toc='\n'.join(toc_rows),
134+
gallery='\n'.join(rows))
118135

119136
# Only write out the file if the contents have actually changed.
120137
# Otherwise, this triggers a full rebuild of the docs
121138

122-
gallery_path = os.path.join(app.builder.srcdir, '_templates', 'gallery.html')
139+
gallery_path = os.path.join(app.builder.srcdir,
140+
'_templates', 'gallery.html')
123141
if os.path.exists(gallery_path):
124142
fh = open(gallery_path, 'r')
125143
regenerate = fh.read() != content
126144
fh.close()
127145
else:
128146
regenerate = True
147+
129148
if regenerate:
130149
fh = open(gallery_path, 'w')
131150
fh.write(content)
@@ -136,5 +155,11 @@ def gen_gallery(app, doctree):
136155
length=len(thumbnails)):
137156
image.thumbnail(key, thumbnails[key], 0.3)
138157

158+
139159
def setup(app):
140160
app.connect('env-updated', gen_gallery)
161+
162+
try: # multiple plugins may use mpl_example_sections
163+
app.add_config_value('mpl_example_sections', [], True)
164+
except sphinx.errors.ExtensionError:
165+
pass # mpl_example_sections already defined

doc/sphinxext/gen_rst.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
"""
44
from __future__ import print_function
55
import io
6-
import os, glob
7-
86
import os
97
import re
108
import sys
11-
fileList = []
9+
10+
import sphinx.errors
11+
12+
13+
exclude_example_sections = ['widgets']
14+
noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-")
15+
1216

1317
def out_of_date(original, derived):
1418
"""
@@ -22,14 +26,18 @@ def out_of_date(original, derived):
2226
return (not os.path.exists(derived) or
2327
os.stat(derived).st_mtime < os.stat(original).st_mtime)
2428

25-
noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-")
26-
2729
def generate_example_rst(app):
2830
rootdir = os.path.join(app.builder.srcdir, 'mpl_examples')
2931
exampledir = os.path.join(app.builder.srcdir, 'examples')
3032
if not os.path.exists(exampledir):
3133
os.makedirs(exampledir)
3234

35+
example_sections = list(app.builder.config.mpl_example_sections)
36+
for i, (subdir, title) in enumerate(example_sections):
37+
if subdir in exclude_example_sections:
38+
example_sections.pop(i)
39+
example_subdirs, titles = zip(*example_sections)
40+
3341
datad = {}
3442
for root, subFolders, files in os.walk(rootdir):
3543
for fname in files:
@@ -115,13 +123,8 @@ def generate_example_rst(app):
115123

116124
fhsubdirIndex.write(' %s <%s>\n'%(os.path.basename(basename),rstfile))
117125

118-
do_plot = (subdir in ('api',
119-
'pylab_examples',
120-
'units',
121-
'mplot3d',
122-
'axes_grid',
123-
) and
124-
not noplot_regex.search(contents))
126+
do_plot = (subdir in example_subdirs
127+
and not noplot_regex.search(contents))
125128
if not do_plot:
126129
fhstatic = io.open(outputfile, 'w', encoding='utf-8')
127130
fhstatic.write(contents)
@@ -158,3 +161,8 @@ def generate_example_rst(app):
158161

159162
def setup(app):
160163
app.connect('builder-inited', generate_example_rst)
164+
165+
try: # multiple plugins may use mpl_example_sections
166+
app.add_config_value('mpl_example_sections', [], True)
167+
except sphinx.errors.ExtensionError:
168+
pass # mpl_example_sections already defined

doc/users/image_tutorial.rst

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,9 @@ object:
229229
imgplot = plt.imshow(lum_img)
230230
imgplot.set_cmap('spectral')
231231

232-
There are many other colormap schemes available. See the `list of
233-
colormaps
234-
<http://matplotlib.org/api/pyplot_summary.html#matplotlib.pyplot.colormaps>`_
235-
and `images of the colormaps
236-
<http://matplotlib.org/examples/pylab_examples/show_colormaps.html>`_.
232+
There are many other colormap schemes available. See the `list and
233+
images of the colormaps
234+
<http://matplotlib.org/examples/color/colormaps_reference.html>`_.
237235

238236
.. _`Color Bars`:
239237

doc/users/screenshots.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Subplot demo
2222
Multiple regular axes (numrows by numcolumns) are created with the
2323
:func:`~matplotlib.pyplot.subplot` command.
2424

25-
.. plot:: mpl_examples/pylab_examples/subplot_demo.py
25+
.. plot:: mpl_examples/subplots_axes_and_figures/subplot_demo.py
2626

2727
.. _screenshots_histogram_demo:
2828

@@ -32,7 +32,7 @@ Histograms
3232
The :func:`~matplotlib.pyplot.hist` command automatically generates
3333
histograms and will return the bin counts or probabilities
3434

35-
.. plot:: mpl_examples/pylab_examples/histogram_demo.py
35+
.. plot:: mpl_examples/statistics/histogram_demo_features.py
3636

3737

3838
.. _screenshots_path_demo:
@@ -43,7 +43,7 @@ Path demo
4343
You can add arbitrary paths in matplotlib as of release 0.98. See
4444
the :mod:`matplotlib.path`.
4545

46-
.. plot:: mpl_examples/api/path_patch_demo.py
46+
.. plot:: mpl_examples/shapes_and_collections/path_patch_demo.py
4747

4848
.. _screenshots_mplot3d_surface:
4949

@@ -103,7 +103,7 @@ or more wedges out from the center of the pie, and a shadow effect.
103103
Take a close look at the attached code that produced this figure; nine
104104
lines of code.
105105

106-
.. plot:: mpl_examples/pylab_examples/pie_demo.py
106+
.. plot:: mpl_examples/pie_and_polar_charts/pie_demo_features.py
107107

108108
.. _screenshots_table_demo:
109109

@@ -153,7 +153,7 @@ The :func:`~matplotlib.pyplot.fill` command lets you
153153
plot filled polygons. Thanks to Andrew Straw for providing this
154154
function
155155

156-
.. plot:: mpl_examples/pylab_examples/fill_demo.py
156+
.. plot:: mpl_examples/lines_bars_and_markers/fill_demo.py
157157

158158

159159
.. _screenshots_date_demo:

doc/users/whats_new.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ In addition to simply plotting the streamlines of the vector field,
312312
line widths of the streamlines to a separate parameter, such as the speed or
313313
local intensity of the vector field.
314314

315-
.. plot:: mpl_examples/pylab_examples/streamplot_demo.py
315+
.. plot:: mpl_examples/images_contours_and_fields/streamplot_demo_features.py
316316

317317

318318
New hist functionality
@@ -559,7 +559,7 @@ Other improvements
559559

560560
* Pim Schellart added a new colormap called "cubehelix".
561561
Sameer Grover also added a colormap called "coolwarm". See it and all
562-
other colormaps :ref:`here <pylab_examples-show_colormaps>`.
562+
other colormaps :ref:`here <color-colormaps_reference>`.
563563

564564
* Many bug fixes and documentation improvements.
565565

0 commit comments

Comments
 (0)