Skip to content

Commit 27960ff

Browse files
authored
Merge pull request #125 from jnothman/importmatplotlib
Support 'from matplotlib import' as alias for 'import matplotlib'
2 parents 46a0259 + e872552 commit 27960ff

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

doc/install.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The following options can be set in your Sphinx ``conf.py``:
1919

2020
numpydoc_use_plots : bool
2121
Whether to produce ``plot::`` directives for Examples sections that
22-
contain ``import matplotlib``.
22+
contain ``import matplotlib`` or ``from matplotlib import``.
2323
numpydoc_show_class_members : bool
2424
Whether to show all members of a class in the Methods and Attributes
2525
sections automatically.

numpydoc/docscrape_sphinx.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
sixu = lambda s: unicode(s, 'unicode_escape')
2222

2323

24+
IMPORT_MATPLOTLIB_RE = r'\b(import +matplotlib|from +matplotlib +import)\b'
25+
26+
2427
class SphinxDocString(NumpyDocString):
2528
def __init__(self, docstring, config={}):
2629
NumpyDocString.__init__(self, docstring, config=config)
@@ -321,7 +324,7 @@ def _str_references(self):
321324
def _str_examples(self):
322325
examples_str = "\n".join(self['Examples'])
323326

324-
if (self.use_plots and 'import matplotlib' in examples_str
327+
if (self.use_plots and re.search(IMPORT_MATPLOTLIB_RE, examples_str)
325328
and 'plot::' not in examples_str):
326329
out = []
327330
out += self._str_header('Examples')

numpydoc/tests/test_docscrape.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,15 @@ def test_plot_examples():
832832
""", config=cfg)
833833
assert 'plot::' in str(doc), str(doc)
834834

835+
doc = SphinxDocString("""
836+
Examples
837+
--------
838+
>>> from matplotlib import pyplot as plt
839+
>>> plt.plot([1,2,3],[4,5,6])
840+
>>> plt.show()
841+
""", config=cfg)
842+
assert 'plot::' in str(doc), str(doc)
843+
835844
doc = SphinxDocString("""
836845
Examples
837846
--------

0 commit comments

Comments
 (0)