Вы можете использовать .diff()
, чтобы найти изменение периода, а затем построить вертикальную линию с plt.axvline()
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data
AccX RecordType
0.204742431640625 0
0.204742431640625 0
0.114715576171875 0
0.114715576171875 0
0.033111572265625 1
0.033111572265625 1
0.0328826904296875 1
0.0328826904296875 2
0.0328826904296875 2
0.154434204101562 2
# Determine when the RecordType Changes
periods = data[data.RecordType.diff()!=0].index.values
figsize = (12, 5)
fig,ax = plt.subplots(figsize=figsize)
ax.plot(data['AccX'])
# Plot the red vertical lines
for item in periods[1::]:
plt.axvline(item, ymin=0, ymax=1,color='red')
# Plot the Record Text.
for i in range(len(periods)-1):
plt.text(y=0.8*data['AccX'].max(), x=(periods[i]+periods[i+1]-1)/2,
s='Record '+ str(i), color='red', fontsize=18)
plt.text(y=0.8*data['AccX'].max(), x=(periods[-1]+len(data)-1)/2,
s='Record '+ str(len(periods)-1), color='red', fontsize=18)
plt.legend()
plt.show()
