我知道有很多这样的线程,但它们都是针对非常简单的情况,例如 3x3 矩阵和类似的东西,而且解决方案甚至不适用于我的情况。所以我试图绘制 G 与 l1 的关系图(这不是十一,而是 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
我理解第一个错误,但只是在修复它时遇到了问题。不过,第二个错误让我感到困惑。我的老板真的很在意我的脖子,所以我们将不胜感激任何帮助!
原文由 Chris 发布,翻译遵循 CC BY-SA 4.0 许可协议
我认为错误消息中给出了问题,尽管不是很容易发现:
“索引过多”意味着您给出了过多的索引值。你已经给出了 2 个值,因为你期望数据是一个二维数组。 Numpy 抱怨是因为
data
不是 2D(它是 1D 或无)。这有点猜测 - 我想知道您传递给 loadfile() 的文件名之一是否指向空文件或格式错误的文件?如果是这样,您可能会返回一个数组,该数组要么是一维的,要么是空的(
np.array(None)
不会抛出Error
,所以你永远不会知道…)。如果您想防止这种失败,您可以在loadfile
函数中插入一些错误检查。我强烈建议在你的
for
循环插入:这将适用于 Python 2.x 或 3.x,并可能揭示问题的根源。您可能会发现只是您的
outputs_l1
列表(即一个文件)中的一个值出现了问题。