2017-04-23 113 views
2

的第一元素的繪圖我有一個數組:顯示陣列

[[5, 6, 9,...], [3, 7, 7,...], [8, 4, 9,...],...] 

如何使一個曲線圖顯示這些陣列的使用matplotlib y軸的第一要素是什麼? x軸可以只是1,2,3,...

所以劇情會有值:

x -> y 
1 -> 5 
2 -> 3 
3 -> 8 ... 

回答

3

只需選擇一個陣列的第一列,並與喜歡這裏plt.plot命令繪製它:

import matplotlib.pylab as plt 
import numpy as np 

# test data 
a = np.array([[5, 6, 9], [3, 7, 7], [8, 4, 9]]) 
print(a[:,0]) # result is [5 3 8] 

# plot the line 
plt.plot(a[:,0]) 
plt.show() 

enter image description here

0

您可以獲取列表的第一個元素,然後通過附加這些元素創建另一個列表。

import matplotlib.pyplot as plt 

oldList = [[5, 6, 9,...], [3, 7, 7,...], [8, 4, 9,...],...] 
newList= [] 

for element in oldList: 
    newList.append(element[0]) #for every element, append first member of that element 

print(newList) #not necessary line, just for convenience 

plt.plot(newList) 
plt.show() 

enter image description here