微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将matplotlib图的上下文保留在文档字符串中

如何解决将matplotlib图的上下文保留在文档字符串中

我知道我可以使用选项:context: close-figs代码段的上下文保存在 .rst 文件中。

我正在尝试在 .py 文件中执行相同的操作,并使用matplotlib为模块文档中的每种方法进行绘制。但是我收到了一个错误,好像(在第一种方法中)在第一段代码中定义的变量不在第二个图的同一上下文中。

复制步骤:

  1. 方法的文档字符串中包含一个上下文:
import matplotlib.pyplot as plt

def blah(x,y): 
    """ 
    Do whatever and then plot it.

    .. plot::
        :context: close-figs
        
        import your_lib as yl

        a = [0,1,2,3,4,5]
        b = [-1,-2,1]
        bleh = ly.blah(a,b)
    """
    return x,y

def plot_coordinates(x,y): 
    """ 
    Plots a series of coordinates.

    .. plot::
        :context: close-figs

        yl.plot_coordinates(a,b)
    """
    plt.figure()
    plt.plot(x,y)

    return plt.show()
  1. 在您的docs文件夹中运行Sphinxmake html

  2. 结果是一个NameError,好像变量“ a”和“ b”没有在前面的代码块中定义。

(...)/matplotlib/sphinxext/plot_directive.py",line 484,in run_code
    exec(code,ns)
  File "<string>",line 1,in <module>
NameError: name 'a' is not defined

参考:https://matplotlib.org/3.1.1/devel/plot_directive.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。