检测变化是列值和绘图



第2列中的My Data(此处显示标题名称N(包括从0到15或从0到7等重复的周期数。第3列CNCN = np.cumsum(np.abs(np.diff(N)))计算

我想根据CN值从数据中获取一个切片。从值1到14,从值29-36,依此类推,然后绘制NCN。我面临的问题是,在打印之前,我必须手动检查CN的切片值。我想要一个程序,它可以在CN值突然跳跃时对数据进行切片,然后绘制


+-------+-----+----+-------+
| index |  N  | CN | Vdiff |
+-------+-----+----+-------+
|   524 |   2 |  1 |   0.0 |
|   525 | 2   |  1 |   0.0 |
|   526 |   2 |  1 |   0.0 |
|     . |     |    |       |
|     . |     |    |       |
|  5900 |  15 | 14 |   0.0 |
|  5901 |  15 | 29 |   0.1 |
|  5902 |   0 | 29 |   0.0 |
|     . |     |    |       |
|     . |     |    |       |
| 33001 |   7 | 36 |   0.0 |
| 33002 |   7 | 36 |   0.0 |
| 33003 |   7 | 43 |   0.1 |
| 33004 |   0 | 43 |   0.0 |
+-------+-----+----+-------+
import matplotlib.pyplot as plt
flt1 = (data['CN'] > 0) & (data['CN'] <= 14) 
Amp1 = data.loc[flt1] 
flt2 = (data['CN'] > 30) & (data['CN'] <= 36)   
Amp2 = data.loc[flt2]  
Amp1.plot(x='N',y='CN',kind='line')

如果我正确理解你的问题,你想在CN中生成跳跃之间的图。然后,下面的代码应该可以做到:

import matplotlib.pyplot as plt
jump_size = 5 # the size of what you consider to be a "jump" in CN
idx = list( data.index[data.CN.diff() >= jump_size] + 1 ) # find jump indexes
# if necessary, add the first index and the last index of your data
if idx[0] != 0: idx = [0]+idx
if idx[-1] != data.shape[0]: idx = idx+[None]
# produce plots between the jumps in CN
for imin,imax in zip(idx[:-1], idx[1:]):
data.iloc[imin:imax].plot(x='N',y='CN',kind='line')
plt.show()

如果这能产生你想要的结果,请告诉我。

编辑:根据注释中的要求,这里有与以前相同的for循环,但使用matplotlib绘图,而不是直接从pandas数据帧进行绘图:

for imin,imax in zip(idx[:-1], idx[1:]):
data_slice = data.iloc[imin:imax]
plt.plot(data_slice.N, data_slice.CN)
plt.show()

最新更新