循环数组问题

时间:2018-10-10 01:17:30

标签: python arrays matplotlib

import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import find_peaks
import pdb

file = open("Data1r5.txt", 'r')
file.close()

t = []
z = []
y = []
x = []

with open("Data1r5.txt", 'r') as f:
    for line in f:
        parts = line.split(", ")
        x.append(float(parts[0][2:]))  
        y.append(float(parts[1][2:]))  
        z.append(float(parts[2][2:]))
        t.append(float(parts[3][2:]))

xx = np.array(x) 
xminvalue = np.min(xx)
xxx = xx - xminvalue
xpeaks, _ = find_peaks(xxx)
plt.plot(xxx, label='x')
plt.plot(xpeaks,xxx[xpeaks],'o')   

当我循环数组时,我总是出错。 IndexError:索引18超出了大小为18的轴0的范围

for i in range(len(xpeaks)):
  print(x[xpeaks[i]:xpeaks[i+1]])

plt.legend()
plt.show()

这是我的数据文件的样子

X:-245, Y:-7, Z:12, T:262472
X:-245, Y:-1, Z:21, T:262602
X:-245, Y:-1, Z:21, T:262722
X:-245, Y:-1, Z:21, T:262839
X:-240, Y:-30, Z:11, T:262958
X:-240, Y:-30, Z:11, T:263076
X:-240, Y:-30, Z:11, T:263196
X:-230, Y:-56, Z:27, T:263318
X:-230, Y:-56, Z:27, T:263435
X:-230, Y:-56, Z:27, T:263554
X:-248, Y:-42, Z:22, T:263674
X:-248, Y:-42, Z:22, T:263790
X:-248, Y:-42, Z:22, T:263908
X:-240, Y:18, Z:6, T:264028
X:-240, Y:18, Z:6, T:264143
X:-240, Y:18, Z:6, T:264258
X:-242, Y:24, Z:-2, T:264370
X:-242, Y:24, Z:-2, T:264484
X:-242, Y:24, Z:-2, T:264619
X:-232, Y:-41, Z:2, T:264756
X:-232, Y:-41, Z:2, T:264876
X:-232, Y:-41, Z:2, T:264993
X:-232, Y:-58, Z:15, T:265113
X:-232, Y:-58, Z:15, T:265231
X:-232, Y:-58, Z:15, T:265351
X:-252, Y:-35, Z:4, T:265483
X:-252, Y:-35, Z:4, T:265612
X:-252, Y:-35, Z:4, T:265731
X:-237, Y:29, Z:-19, T:265848
X:-237, Y:29, Z:-19, T:265962
X:-237, Y:29, Z:-19, T:266079
X:-239, Y:19, Z:-28, T:266192
X:-239, Y:19, Z:-28, T:266307
X:-239, Y:19, Z:-28, T:266425
X:-258, Y:-19, Z:6, T:266541
X:-258, Y:-19, Z:6, T:266659
X:-258, Y:-19, Z:6, T:266778
X:-235, Y:-65, Z:9, T:266909
X:-235, Y:-65, Z:9, T:267041
X:-235, Y:-65, Z:9, T:267171
X:-236, Y:-13, Z:-9, T:267293
X:-236, Y:-13, Z:-9, T:267411
X:-236, Y:-13, Z:-9, T:267531
X:-240, Y:20, Z:-8, T:267650
X:-240, Y:20, Z:-8, T:267767
X:-240, Y:20, Z:-8, T:267883
X:-240, Y:14, Z:0, T:268003
X:-240, Y:14, Z:0, T:268123
X:-240, Y:14, Z:0, T:268239
X:-248, Y:-43, Z:26, T:268356
X:-248, Y:-43, Z:26, T:268473
X:-248, Y:-43, Z:26, T:268588
X:-239, Y:-52, Z:8, T:268708
X:-239, Y:-52, Z:8, T:268823
X:-239, Y:-52, Z:8, T:268938
X:-237, Y:-20, Z:-27, T:269054
X:-237, Y:-20, Z:-27, T:269171
X:-237, Y:-20, Z:-27, T:269298
X:-239, Y:23, Z:-15, T:269429
X:-239, Y:23, Z:-15, T:269562
X:-239, Y:23, Z:-15, T:269682
X:-245, Y:12, Z:-6, T:269802
X:-245, Y:12, Z:-6, T:269921
X:-245, Y:12, Z:-6, T:270036
X:-246, Y:-40, Z:20, T:270157
X:-246, Y:-40, Z:20, T:270273
X:-246, Y:-40, Z:20, T:270390
X:-238, Y:-47, Z:12, T:270512
X:-238, Y:-47, Z:12, T:270629
X:-238, Y:-47, Z:12, T:270744
X:-252, Y:-19, Z:-3, T:270866
X:-252, Y:-19, Z:-3, T:270981
X:-252, Y:-19, Z:-3, T:271100
X:-237, Y:16, Z:-8, T:271216
X:-237, Y:16, Z:-8, T:271330
X:-237, Y:16, Z:-8, T:271446
X:-243, Y:8, Z:-6, T:271600
X:-243, Y:8, Z:-6, T:271730
X:-243, Y:8, Z:-6, T:271854
X:-244, Y:-30, Z:1, T:271976
X:-244, Y:-30, Z:1, T:272094
X:-244, Y:-30, Z:1, T:272206
X:-234, Y:-55, Z:4, T:272322
X:-234, Y:-55, Z:4, T:272439
X:-234, Y:-55, Z:4, T:272558
X:-249, Y:-36, Z:-6, T:272679
X:-249, Y:-36, Z:-6, T:272796
X:-249, Y:-36, Z:-6, T:272911
X:-243, Y:13, Z:-21, T:273022
X:-243, Y:13, Z:-21, T:273137
X:-243, Y:13, Z:-21, T:273258
X:-238, Y:12, Z:-11, T:273377
X:-238, Y:12, Z:-11, T:273496
X:-238, Y:12, Z:-11, T:273614
X:-245, Y:-49, Z:14, T:273734
X:-245, Y:-49, Z:14, T:273852
X:-245, Y:-49, Z:14, T:273984
X:-239, Y:-54, Z:4, T:274119
X:-239, Y:-54, Z:4, T:274247
X:-239, Y:-54, Z:4, T:274365
X:-249, Y:-2, Z:-15, T:274485
X:-249, Y:-2, Z:-15, T:274601
X:-249, Y:-2, Z:-15, T:274722
X:-239, Y:20, Z:-12, T:274841
X:-239, Y:20, Z:-12, T:274955
X:-239, Y:20, Z:-12, T:275075
X:-258, Y:-1, Z:-7, T:275194
X:-258, Y:-1, Z:-7, T:275312
X:-258, Y:-1, Z:-7, T:275430
X:-236, Y:-57, Z:9, T:275549
X:-236, Y:-57, Z:9, T:275664
X:-236, Y:-57, Z:9, T:275781
X:-236, Y:-50, Z:-2, T:275903
X:-236, Y:-50, Z:-2, T:276021
X:-236, Y:-50, Z:-2, T:276141
X:-244, Y:15, Z:-10, T:276278
X:-244, Y:15, Z:-10, T:276520
X:-244, Y:15, Z:-10, T:276638
X:-244, Y:11, Z:-18, T:276760
X:-244, Y:11, Z:-18, T:276880
X:-244, Y:11, Z:-18, T:276998
X:-247, Y:-23, Z:-8, T:277121
X:-247, Y:-23, Z:-8, T:277238
X:-247, Y:-23, Z:-8, T:277355
X:-230, Y:-60, Z:4, T:277475
X:-230, Y:-60, Z:4, T:277598
X:-230, Y:-60, Z:4, T:277737
X:-250, Y:-38, Z:0, T:277856
X:-250, Y:-38, Z:0, T:277971
X:-250, Y:-38, Z:0, T:278089
X:-241, Y:13, Z:-16, T:278210
X:-241, Y:13, Z:-16, T:278329
X:-241, Y:13, Z:-16, T:278454
X:-241, Y:14, Z:-9, T:278590
X:-241, Y:14, Z:-9, T:278713
X:-241, Y:14, Z:-9, T:278827
X:-248, Y:-33, Z:1, T:278946
X:-248, Y:-33, Z:1, T:279063
X:-248, Y:-33, Z:1, T:279178
X:-240, Y:-51, Z:-2, T:279292
X:-240, Y:-51, Z:-2, T:279406
X:-240, Y:-51, Z:-2, T:279525
X:-244, Y:-18, Z:-9, T:279645
X:-244, Y:-18, Z:-9, T:279759
X:-244, Y:-18, Z:-9, T:279875
X:-239, Y:29, Z:-15, T:279988
X:-239, Y:29, Z:-15, T:280099
X:-239, Y:29, Z:-15, T:280212
X:-236, Y:17, Z:-5, T:280325
X:-236, Y:17, Z:-5, T:280436
X:-236, Y:17, Z:-5, T:280547
X:-246, Y:-6, Z:7, T:280673
X:-246, Y:-6, Z:7, T:280791
X:-246, Y:-6, Z:7, T:280918
X:-239, Y:-45, Z:15, T:281052
X:-239, Y:-45, Z:15, T:281174
X:-239, Y:-45, Z:15, T:281293
X:-240, Y:-32, Z:5, T:281412
X:-240, Y:-32, Z:5, T:281526
X:-240, Y:-32, Z:5, T:281645
X:-246, Y:-33, Z:-3, T:281764
X:-246, Y:-33, Z:-3, T:281882
X:-246, Y:-33, Z:-3, T:282000
X:-172, Y:2, Z:143, T:282118
X:-172, Y:2, Z:143, T:282232
X:-172, Y:2, Z:143, T:282350

我想做的是将一组峰阵列“ xpeaks”(即1个峰的数据)循环到另一个峰,并将所有峰峰数据保存到.csv或.txt文件中。

我所做的硬编码版本就是这样

a = (x[xpeaks[1]:xpeaks[2]]) ,a = (x[xpeaks[2]:xpeaks[3]])

但是这样做并不是那么理想,因为我有不同的数据文件和不同数量的峰值。

1 个答案:

答案 0 :(得分:0)

它看起来像:

 for i in range(len(xpeaks)):
      print(x[xpeaks[i]:xpeaks[i+1]])

保证会在最后一个i上崩溃,因为xpeaks[i+1]将超出数组末尾。

尝试:

 for i in range(len(xpeaks)-1):