绘制列表,清除,然后使用python在同一窗口上绘制不同的列表

时间:2014-03-27 20:48:04

标签: python dynamic matplotlib plot

我已经在这几个星期了 我想绘制1个列表,然后在同一个窗口中快速删除该列表并绘制另一个列表。

原因是我有一个实时进入的EEG信号,我将每40个样本转换为频域然后绘制它们以试图检测不同的认知状态。

现在我刚从脑袋里创建了两个列表,我希望列表1出现,然后列出两个列表,很快就会出现在它的位置

我已经尝试了matplotlib动画,但我还没弄明白如何使用它对我来说很复杂。

from numpy import sin, linspace, pi
from pylab import plot, show, title, xlabel, ylabel, subplot
from scipy import fft, arange
import time

lst1 = [1000,1100,1000,1150,1100,1090,1300,1700,2000,1500,1200,1100,1000,1100,1100,1000,1150,1100]

lst = [1000,1060,1200,1600,2000,1400,1030,1300,1600,2000,1400,1100,1000,1400,1700,1800,1500,1100]


def plotSpectrum(y,Fs):
 """
 Plots a Single-Sided Amplitude Spectrum of y(t)
 """

 n = len(lst) # length of the signal
 k = arange(n)
 T = n/Fs
 frq = k/T # two sides frequency range
 frq = frq[range(n/2)] # one side frequency range

 Y = fft(lst)/n # fft computing and normalization
 Y = Y[range(n/2)]

 plot(frq,abs(Y),'r') # plotting the spectrum
 xlabel('Freq (Hz)')
 ylabel('|Y(freq)|')


Fs = 18.0;  # sampling rate
Ts = 1.0/Fs; # sampling interval
t = arange(0,1,Ts) # time vector


subplot(2,1,1)
plot(t,lst)
xlabel('Time')
ylabel('Amplitude')
subplot(2,1,2)
plotSpectrum(lst,Fs)
show()

#time.sleep(5) #here is where id like the second list to appear
#plotSpectrum(lst1,Fs)
#show()

1 个答案:

答案 0 :(得分:1)

对于简单的动画,您可以使用pause

from pylab import plot, show,  xlabel, ylabel, subplot, draw, pause

# Lists to plot
list1 = [1000,1100,1000,1150,1100,1090,1300,1700,2000,1500,1200,1100,1000,1100,1100,1000,1150,1100]
list2 = [1000,1060,1200,1600,2000,1400,1030,1300,1600,2000,1400,1100,1000,1400,1700,1800,1500,1100]

# Setup the axis
subplot(1,1,1)
xlabel('Time')
ylabel('Amplitude')

# Plot the first line
lines = plot(list1,'r')
show() 
pause(1) # pause

# Remove the first line
oldLine = lines.pop()
oldLine.remove()

# Plot the second line
lines = plot(list2,'g')
draw()