Search code examples
pythondebuggingmatplotlibplotfigure

plot figure using console command while debugging


I am very new to Python. I am doing a very simple code like following:

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()

I debug by stepping through, and right after the execution of

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

I try to plot the curve by typing command in console:

plot(t,s)
show()

What happens is a figure showed, but there is no curved drawn on the figure. Something like this:

enter image description here

I am a MATLAB user. MATLAB would allow you to do plot using command line in console any time during debugging, so you can visualize your data during debugging if you want to.

Can I do the same with Python? Thanks.


Solution

  • enter image description hereI ran your code, changed it a bit. It doesn't show until plt.show(), did you run that line?

    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