1

我将 sphinx 与numpydocs,doctest和 scipy 主题一起使用。

在文档字符串中使用.. plot::时,所有图都被创建,除了没有显示它们被调用的位置,它们都移动到文档的末尾。有没有办法强制它内联,而不需要在.rst文件中重写示例?这是我的example.py文件文档字符串的样子:

.. plot::

    >>> import numpy as np
    >>> import matplotlib.pylab as plt

    # commentary breaking the block 
    Finished Imports

    >>> x = np.linspace(-np.pi*2, np.pi*2, 2000)
    >>> y = np.sin(x)
    >>> plt.plot(x, y)#doctest: +SKIP
    >>> plt.show()#doctest: +SKIP

First plot should be here

    >>> x2 = x
    >>> plt.plot(x2, y)#doctest: +SKIP
    >>> plt.show()#doctest: +SKIP

Second plot should be here

……

But both are here.

或者,我尝试添加另一种.. plot::方法,但它改变了范围。

.. plot::

    >>> import numpy as np
    >>> import matplotlib.pylab as plt

    # commentary breaking the block 
    Finished Imports

    >>> x = np.linspace(-np.pi*2, np.pi*2, 2000)
    >>> y = np.sin(x)
    >>> plt.plot(x, y)#doctest: +SKIP
    >>> plt.show()#doctest: +SKIP

First plot should be here

.. plot::

    >>> x2 = x
    >>> plt.plot(x2, y)#doctest: +SKIP
    >>> plt.show()#doctest: +SKIP

使用make htmlwill state时x未定义。

4

1 回答 1

1

我遇到的两个问题现在已经解决了。一方面,我没有正确显示所有更新,所以我需要在每次 make clean之前运行。make html

原始问题的解决方案是在每次调用:context: close-figs之后添加。.. plot::plot.show()

.. plot::
    :context: close-figs

所以我的文档字符串现在看起来像:

.. plot::
    :context: close-figs

    >>> import numpy as np
    >>> import matplotlib.pylab as plt

    # commentary breaking the block 
    Finished Imports

    >>> x = np.linspace(-np.pi*2, np.pi*2, 2000)
    >>> y = np.sin(x)
    >>> plt.plot(x, y)#doctest: +SKIP
    >>> plt.show()#doctest: +SKIP

.. plot::
    :context: close-figs

First plot shows here

    >>> x2 = x
    >>> plt.plot(x2, y)#doctest: +SKIP
    >>> plt.show()#doctest: +SKIP

.. plot::
    :context: close-figs

Second plot shows here
于 2020-03-26T15:29:13.137 回答