2017-05-04 45 views
2

我有這麼多麻煩,我想如果我能得到一隻手。我試圖通過csv文件讀取並提取列並繪製column_index中列出的任何列,這些列實際上是提供給用戶的輸入並且可以更改。通過csv文件循環並繪製列?

這裏是我的.csv文件引擎收錄的link,這是我的嘗試:

with open('./P14_data.csv', 'rb') as csvfile: 
     data = csv.reader(csvfile, delimiter=',') 

     #retrieves rows of data and saves it as a list of list 
     x = [row for row in data] 

     #forces list as array to type cast as int 
     int_x = np.array(x, int) 

     column_index = [1,2,3] 
     column_values = np.empty(0) 

     for col in column_index: 
     #loops through array 
      for array in range(len(int_x)): 
     #gets correct column number 
       column_values = np.append(column_values,np.array(int_x[array][col-1])) 
      plt.plot(column_values) 

然而,這只是圖一個行所有3列,當我想爲列3行不同:

enter image description here

+0

顯示打印(int_x),印刷(column_values)結果,並打印(column_values)剛剛plt.plot()之前 – Exprator

回答

1

內循環之前重置column_values。否則,值會附加到同一個列表中。

column_index = [1,2,3] 
for col in column_index: 
    column_values = np.empty(0) # <--- reset for each line chart. 
    for array in range(len(int_x)): 
     column_values = np.append(column_values, np.array(int_x[array][col-1])) 
    plt.plot(column_values) 

enter image description here

+0

非常感謝你!!!! – Nikitau