2014-03-27 77 views
1

我已經在這個星期, 我想繪製1列表,然後在同一窗口中很快刪除該列表並繪製另一個。繪圖清單,清除,然後繪製與python相同的窗口上的不同列表

原因是我有一個實時進入的腦電信號,我將每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

對於簡單的動畫,您可以使用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() 
相關問題