【发布时间】:2019-10-26 01:23:42
【问题描述】:
我想自动化在 numpy 中加载一些 ASCII 数据文件的过程,以便绘制它们。文件名通过终端提供给程序,内容被加载并保存在列表中。所以基本上这个想法是有一个包含 numpy 数组的列表,我可以稍后通过索引调用这些数组来绘制每个单独的数据。 我遇到的问题是索引不适用于我制作的这些列表
subplots_array = [[0,0],[0,0],[0,0],[0,0]]
subplots_axes = [0,0,0,0] #this array will allow to create subplots for '''
# each of the above data
fig = plt.figure()
counter = 0
for x in arguments_list:
for filename in glob.glob(x):
mydata = np.loadtxt(filename)
subplots_array[counter] = mydata # This loads the data from files
#specified in arguments argv into a subplot array as numpy sub-array
counter += 1
counter = 0
for x in subplots_array:
subplots_axes[counter] = fig.add_subplot(counter+1, 1, 1)
subplots_axes[counter].scatter(subplots_array[counter][:, 0]), subplots_array[counter][:, 1], s = 12, marker = "x")
counter = counter + 1
这是我得到的错误。有趣的是,如果我用数字索引(如 0 或 1 或 2 等)替换“计数器”,尽管计数器也被定义为索引,但数据绘制正确。所以我没有想法。
Traceback (most recent call last):
File "FirstTrial.py", line 89, in <module>
subplots_axes[counter].scatter(subplots_array[counter][:,0], subplots_array[counter][:, 1], s = 12, marker = "x")
TypeError: list indices must be integers or slices, not tuple
我希望这是足够的描述来帮助我解决问题。
【问题讨论】:
-
你说如果你用 counter 代替它可以工作的数字,你能发布示例代码吗?
-
for x in ...循环看起来很有趣。你根本不使用x。[:,0]索引实际上传递了一个元组(slilce(NOne),0)。如果接收者是一个数组,那没关系,但如果它是列表,那就不行了。 -
唯一改变 subplots_axes[counter].scatter(subplots_array[0][:, 0]), subplots_array[0][:, 1], s = 12, marker = "x") 的行