2014-02-20 40 views
1

在一個while循環中,我更新了一組圖中的兩組數據(一些數據X和一個閾值)。現在我想在同一個圖上添加單點(X的峯)。我怎樣才能做到這一點?如何在已包含數據的圖上繪製單點?

import matplotlib.pyplot as plt 

plt.ion() 

fig = plt.figure() 

plt_ps = fig.add_subplot(111) 

# initialize plots 
powerspectrum, = plt_ps.plot(np.zeros([windowSize,])) 
threshold, = plt_ps.plot(np.zeros([windowSize,])) 
peaks, = plt_ps.plot([], [], 'or') # peaks will just be a set of coordinates, eg peaks_x=[2,4,7] and peaks_y=[3,7,6] 

while(somecondition): 

    # some data processing 

    powerspectrum.set_ydata(new_powerspectrum_data) 
    threshold.set_ydata(new_threshold_data) 
    #peaks.? how do I set new peaks? Tried peaks.set_data(peaks_x, peaks_y) but peaks do not show up 
    plt_ps.relim() 
    plt_ps.autoscale_view() 
    fig.canvas.draw() 

回答

1

只要使用plot用正確的方式:

import matplotlib.pyplot as plt 

xs = [1,2,5,3,6,7,1,3,4,5,2,6,7,8,2,1] 
ys = [3,4,5,2,7,1,3,4,1,2,3,4,5,2,3,1] 

plt.plot(xs,ys,'.') 
plt.show() 
相關問題