代码之家  ›  专栏  ›  技术社区  ›  Nick X Tsui

调试时使用控制台命令绘制图形

  •  0
  • Nick X Tsui  · 技术社区  · 7 年前

    我对python很陌生。我正在做一个非常简单的代码,如下所示:

    import numpy as np
    from matplotlib.pyplot import figure
    from matplotlib.pyplot import plot
    from matplotlib.pyplot import grid
    from matplotlib.pyplot import title
    from matplotlib.pyplot import xlabel
    from matplotlib.pyplot import close
    from matplotlib.pyplot import ylabel
    from matplotlib.pyplot import show
    
    close("all")
    
    figure()
    t = np.arange(0.0, 2.0, 0.01)
    s = 1 + np.sin(2*np.pi*t)
    
    
    plot(t, s)
    xlabel('time (s)')
    ylabel('voltage (mV)')
    title('About as simple as it gets, folks')
    grid(True)
    show()
    

    我通过单步调试,并在执行

    s = 1 + np.sin(2*np.pi*t)
    

    我试图通过在控制台中键入命令来绘制曲线:

    plot(t,s)
    show()
    

    所发生的是一个数字显示,但没有曲线画在数字上。像这样的:

    enter image description here

    我是一个Matlab用户。在调试期间,Matlab允许您随时在控制台中使用命令行进行绘图,因此如果您愿意,可以在调试期间可视化您的数据。

    我可以用python做同样的事情吗?谢谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   user1911226    7 年前

    enter image description here 我运行了你的代码,改了一点。它直到plt.show()才显示,你跑那条线了吗?

    import numpy as np
    import matplotlib.pyplot as plt
    plt.close("all")
    
    plt.figure()
    t = np.arange(0.0, 2.0, 0.01)
    s = 1 + np.sin(2*np.pi*t)
    
    
    plt.plot(t, s)
    plt.xlabel('time (s)')
    plt.ylabel('voltage (mV)')
    plt.title('About as simple as it gets, folks')
    plt.grid(True)
    plt.show()
    

    enter image description here