效果图如下:
实现代码如下:
import mne
import numpy as np
import matplotlib.pyplot as plt
import mpl_toolkits.axisartist as axisartist
from matplotlib.pyplot import MultipleLocator
#从edf文件读取脑电数据
raw = mne.io.read_raw_edf(filename, preload=True)
tmin = 651 - 5
tmax = 651 + 5
chans = ["POL G9", "POL G5", "POL G4", "POL G2", "POL G3", "POL H3", "POL G7", "POL G8", "POL G6", "POL H1"]
selection = raw.crop(tmin, tmax)
selection = selection.pick_channels(chans)
sl = selection[:, :] #抽取为array格式
offset = np.arange(0, 10*0.002, 0.002)
x = sl[1] #x轴数据
y = sl[0].T + offset #y轴数据
ylabel = ['$G2$', '$G3$','$G4$', '$G5$', '$G6$', '$G7$', '$G8$', '$G9$', '$H1$', '$H3$'] #y轴刻度的名称
fig = plt.figure()
ax = axisartist.Subplot(fig, 111)
fig.add_axes(ax)
ax.axis["left"].set_axisline_style("->", size = 1.5) #设置y轴样式为->箭头
ax.axis["bottom"].set_axisline_style("->", size = 1.5) #设置x轴样式为->箭头
ax.axis["top"].set_visible(False) #隐藏上面的轴
ax.axis["right"].set_visible(False) #隐藏右侧的轴
x_major_locator=MultipleLocator(1) #设置刻度间距为1
ax.xaxis.set_major_locator(x_major_locator)
plt.yticks(offset.tolist(), ylabel) #修改y轴刻度的名称
plt.xlabel("Time(s)")
plt.ylabel("Channels")
plt.axvline(5, linestyle="dotted", color='k') #在x=5的地方画垂直点状线
plt.text(5.17, 0.02, "Seizure") #在x=5.17,y=0.02处写上Seizure字样
plt.plot(x,y,'-k',linewidth=0.5) #设置线条颜色、宽度
plt.show()