使用matplotlib绘制excel中的多列

时间:2016-07-13 11:40:41

标签: python excel numpy matplotlib

我的数据看起来像:

sites   mp1     mp2
ry99    0.66    0.54
ry98    0.71    0.54
ry97    0.58    0.45
ry96    0.65    0.55

我在excel文件中有以下列表,我想在第一列和第三列之间绘制第一列,使用matplotlib在同一图表中。

当我尝试以下代码时,收到错误:

plt.plot (arr[1:,0],arr[1:,n],label=names[n])
IndexError: too many indices for array

这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
arr = np.genfromtxt('C:\\Users\\abdulaziz.bello\\Desktop\\geodesy\\es99.csv', delimiter=' ', dtype=None) 
names = (arr[0]) 
for n in range (1,len(names)): 
    plt.plot (arr[1:,0],arr[1:,n],label=names[n]) 
plt.legend()    
plt.show()

任何帮助都将非常感谢您解决我的问题。我从Python开始

1 个答案:

答案 0 :(得分:0)

这是我的代码,可以解决您的问题:

import numpy as np
import matplotlib.pyplot as plt


arr = np.genfromtxt('file', delimiter='   ', dtype=None, names =['sites','mp1','mp2'], skip_header=1)

x = np.arange(4)

plt.xticks(x, arr['sites'])

plt.plot(x, arr['mp1'])
plt.plot(x, arr['mp2'])

plt.show()

结果是:

enter image description here