我知道这类主题有很多,但它们都是针对非常简单的情况,比如3x3矩阵之类的东西,解决方案甚至无法适用于我的情况。所以我正在尝试绘制G关于l1(不是数字11,而是L1)的图形。数据在从Excel文件加载的文件中。 Excel文件为14x250,因此有14个参数,每个参数有250个数据点。我曾经有另一个用户(向Hugh Bothwell大喊一声!)帮助我解决代码中的错误,但现在另一个错误出现了。
所以这就是问题所在的代码:
# format for CSV file:
header = ['l1', 'l2', 'l3', 'l4', 'l5', 'EI',
'S', 'P_right', 'P1_0', 'P3_0',
'w_left', 'w_right', 'G_left', 'G_right']
def loadfile(filename, skip=None, *args):
skip = set(skip or [])
with open(filename, *args) as f:
cr = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
return np.array(row for i,row in enumerate(cr) if i not in skip)
#plot data
outputs_l1 = [loadfile('C:\\Users\\Chris\\Desktop\\Work\\Python Stuff\\BPCROOM - Shingles analysis\\ERR analysis\\l_1 analysis//BS(1) ERR analysis - l_1 - P_3 = {}.csv'.format(p)) for p in p3_arr]
col = {name:i for i,name in enumerate(header)}
fig = plt.figure()
for data,color in zip(outputs_l1, colors):
xs = data[:, col["l1" ]]
gl = data[:, col["G_left" ]] * 1000.0 # column 12
gr = data[:, col["G_right"]] * 1000.0 # column 13
plt.plot(xs, gl, color + "-", gr, color + "--")
for output, col in zip(outputs_l1, colors):
plt.plot(output[:,0], output[:,11]*1E3, col+'--')
plt.ticklabel_format(axis='both', style='plain', scilimits=(-1,1))
plt.xlabel('$l1 (m)$')
plt.ylabel('G $(J / m^2) * 10^{-3}$')
plt.xlim(xmin=.2)
plt.ylim(ymax=2, ymin=0)
plt.subplots_adjust(top=0.8, bottom=0.15, right=0.7)
在运行整个程序之后,我收到了错误信息:Traceback (most recent call last):
File "C:/Users/Chris/Desktop/Work/Python Stuff/New Stuff from Brenday 8 26 2014/CD_ssa_plot(2).py", line 115, in <module>
xs = data[:, col["l1" ]]
IndexError: too many indices for array
在我遇到那个问题之前,我曾遇到另一个问题,涉及到错误信息所指的下面几行中的一行:
Traceback (most recent call last): File "FILE", line 119, in <module>
gl = data[:, col["G_left" ]] * 1000.0 # column 12
IndexError: index 12 is out of bounds for axis 1 with size 12
我理解第一个错误,但只是在解决它的过程中遇到了问题。对于第二个错误,我感到困惑。我的老板正在紧盯着我,所以任何帮助都将不胜感激!
for data, color in zip(outputs_l1, colors):
后面加上print data
来查看每一行数据的样子了吗?看起来它可能没有按照你预期的格式进行排列(你认为它应该是一个由14个元素组成的数组,对吧?但似乎有时只有12个元素)。 - zehnpaardprint(data)
代替。IndexError: index 12 is out of bounds for axis 1 with size 12
说明数据行中某处仅包含12个元素。 - zehnpaard