我有这样的数据:
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
构建情节的代码:
import matplotlib as mpl
mpl.use('MacOSX')
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
data = pd.read_csv('myData.csv')
figsize = (15, 7)
fig,ax = plt.subplots(figsize=figsize)
ax.plot(data['AccX'])
plt.legend()
plt.show()
如何在每次更改记录类型的值后添加行? 是否可以在行之间的位置添加标题?
我的意思是构造这样的东西:
答案 0 :(得分:1)
您可以使用.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()