Skip to content

Commit 5814336

Browse files
committed
Support from matplotlib import as alias for import matplotlib
Closes #96
1 parent e32e363 commit 5814336

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)
@@ -337,7 +340,7 @@ def _str_references(self):
337340
def _str_examples(self):
338341
examples_str = "\n".join(self['Examples'])
339342

340-
if (self.use_plots and 'import matplotlib' in examples_str
343+
if (self.use_plots and IMPORT_MATPLOTLIB_RE.search(examples_str)
341344
and 'plot::' not in examples_str):
342345
out = []
343346
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)