2017-02-20 83 views
0

我目前正在寫一段代碼來分析一些數據,但我已經打了一個小小的障礙。由於分析中存在大量事件,我的小組決定我們應該採取每個文件的方法並繪製這些文件。當我完成這個功能時,我的函數會讀取文件併成功繪製每個點,但是當我試圖在數據點之間繪製一條線時,什麼也沒有繪製。Python pyplot沒有繪製線時,指示

def plotEventSpeedVsDate(startYear): 
    for filename in fileNameGenerator(startYear): 
     date,linearSpeed,width,accel=readData(filename) 
     xAxis=np.median(date) 
     yAxis=np.mean(linearSpeed) 
     plt.plot_date(xAxis, yAxis, '-', color='black') 

它成功地繪製了點,但是當我運行該函數時不會畫線。

回答

0

問題在於,每次您撥打plot_date時,您只需要一個數據點。如果您在循環訪問文件時收集列表中的所有日期和速度,則可以通過一次調用將這些列表繪製到plot_date並用線連接點。

def plotEventSpeedVsDate(startYear): 
    dates = [] 
    speeds = [] 
    for filename in fileNameGenerator(startYear): 
     date,linearSpeed,width,accel=readData(filename) 
     dates.append(np.median(date)) 
     speeds.append(np.mean(linearSpeed)) 

    plt.plot_date(dates, speeds, 'o-', color='black') 
+0

這真是太棒了謝謝你!看起來很明顯,現在我回頭看。 –