2017-04-23 115 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