首页 > 解决方案 > IndexError:索引 2 在文件绘图期间超出轴 1 的范围,大小为 2

问题描述

我正在尝试使用 Matplotlib 在同一个图中绘制三个文件。但是,在第三个文件中我收到了这个错误:

IndexError:索引 2 超出轴 1 的范围,大小为 2

import numpy  as np
import matplotlib.pyplot as plt
plt.style.use('classic')
k=0
InputFile = []
markers = ["o" , "s" , "D"]
colors = ["red", "green", "blue"]

for i in range(1,4):
    InputFile.append("max-velocity-%s.out" %i)
    
for dname in InputFile:   
    k=k+1
    data=np.loadtxt(dname, skiprows= 3)
    x=data[:,1]
    y=data[:,2]
    plt.plot(x,y,color=colors[k-1], marker = markers[k-1])

plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.xlabel(r'$t\,$(s)',fontsize=14)
plt.ylabel(r'Max Velocity',fontsize=14)
plt.show() 

有人可以帮帮我吗?

谢谢。

标签: pythonfileindexing

解决方案


推荐阅读