加载 EEG 数据并绘制轨迹
下一步是加载 EEG 数据并绘制轨迹。我们将使用 fromfile() 函数从文件中加载数据,并使用 LineCollection() 来绘制轨迹。我们还将把 y 轴刻度标签设置为电极通道。
## 加载 EEG 数据
n_samples, n_rows = 800, 4
data = np.load('eeg_data.npy')
t = 10 * np.arange(n_samples) / n_samples
## 绘制 EEG
ax2 = fig.add_subplot(2, 1, 2)
ax2.set_xlim(0, 10)
ax2.set_xticks(np.arange(10))
dmin = data.min()
dmax = data.max()
dr = (dmax - dmin) * 0.7 ## 稍微紧凑一点。
y0 = dmin
y1 = (n_rows - 1) * dr + dmax
ax2.set_ylim(y0, y1)
segs = []
for i in range(n_rows):
segs.append(np.column_stack((t, data[:, i])))
offsets = np.zeros((n_rows, 2), dtype=float)
offsets[:, 1] = np.arange(n_rows) * dr
lines = LineCollection(segs, offsets=offsets, transOffset=None)
ax2.add_collection(lines)
## 将 y 轴刻度设置为使用 y 轴上的轴坐标
ax2.set_yticks(offsets[:, 1])
ax2.set_yticklabels(['PG3', 'PG5', 'PG7', 'PG9'])
ax2.set_xlabel('Time (s)')