【问题标题】:plotting multiple column from excel with matplotlib使用 matplotlib 从 excel 中绘制多列
【发布时间】:2016-07-13 11:40:41
【问题描述】:

我的数据看起来像:

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 开始

【问题讨论】:

  • 由于您正在读取 .csv 文件,您可能应该使用delimiter=','。这可能会解决您的问题,但除非您也发布数据,否则很难知道。

标签: python excel numpy matplotlib


【解决方案1】:

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

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()

结果是:

【讨论】:

  • 我尝试了建议的解决方案,但出现同样的错误
猜你喜欢
  • 2022-11-15
  • 1970-01-01
  • 2014-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 2018-08-29
  • 2017-09-30
相关资源
最近更新 更多