有没有办法让绘图可以点击,这样它就会告诉我我正在看哪个 EEG 通道?

Is there a way to make a plot clickable so it will tell me what EEG channel I am looking at?

注:这是一道关于小鼠脑电图数据绘制的题。

我绘制了一个图表,显示过滤后的 EEG 电极通道的平均试验信号。在绘制此图时,我看到了一些我想从我的图中排除的信号,但我没有办法判断绘制了哪些通道。有没有办法添加一些东西,让我可以点击或悬停在其中一个绘制的 lines/channels 上,让我的 jupyter notebook 告诉我我 clicked/am 悬停在哪个频道上?

这是我希望可以点击的情节:

如果有用的话,这是我用来制作图表的代码:

pick_stim = 'opto'
pick_param = '500ms'
pick_sweep = 0

prex = .1 # .2ms before stim to plot
postx = .1 # .6ms after stim to plot

auc_window = [-.04, .1]

fig, axs = plt.subplots(1,2, figsize=(9,5), sharex=True, sharey=True, constrained_layout=True)

run_timex = trial_running[pick_stim][pick_param][pick_sweep][0]
run_trials = trial_running[pick_stim][pick_param][pick_sweep][1] #running speed


ztimex = zscore_traces[pick_stim][pick_param][pick_sweep][0] #need for AUC
zscore_trials_all = zscore_not_mean_traces[pick_stim][pick_param][pick_sweep][1] 

# Run trials #
mean_run_zscore = np.mean(zscore_trials_all[:,:,run_trial], axis=2)
run_zscore_inds = np.nonzero((ztimex >= auc_window[0]) & (ztimex <= auc_window[1]))[0] 
run_zscore_trace = mean_run_zscore[run_zscore_inds,:]
axs[0].plot(ztimex[run_zscore_inds],run_zscore_trace, color='black', linewidth=0.6, alpha=0.8)
#axs[0].plot(run_timex, run_trials, color='k', linewidth=0.6)
axs[0].axvspan(-.001, .001, color='r', alpha=0.5)
#axs[0].set_xlim([-prex, postx])
axs[0].set_title('Run trials')

# No Run #

mean_no_run_zscore = np.mean(zscore_trials_all[:,:,no_run], axis=2)
no_run_zscore_inds = np.nonzero((ztimex >= auc_window[0]) & (ztimex <= auc_window[1]))[0] 
no_run_zscore_trace = mean_no_run_zscore[no_run_zscore_inds,:]
axs[1].plot(ztimex[no_run_zscore_inds],no_run_zscore_trace, color='black', linewidth=0.6, alpha=0.8)
axs[1].axvspan(-.001, .001, color='r', alpha=0.5)
axs[1].set_title('No Run trials')   

您可以为每条曲线添加标签,然后使用 mplcursors 在悬停时(或使用 hover=False 单击时)显示注释。

请注意,要在 Jupyter notebook 中创建交互式绘图,需要 %matplotlib notebook(这可能取决于 Jupyter 的安装方式),而不仅仅是 %matplotlib inline(生成静态图像)。参见 Docs

下面是一个用一些测试数据展示总体思路的例子:

%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
import mplcursors

np.random.seed(123)
x = np.arange(100)
y = np.random.randn(100, 20).cumsum(axis=0)

fig, ax = plt.subplots()
curves = plt.plot(x, y, color='black', alpha=0.3)
for ind, curv in enumerate(curves):
    curv.set_label(f'curve nº{ind}')
cursor = mplcursors.cursor(curves, hover=True)
cursor.connect('add', lambda sel: sel.annotation.set_text(sel.artist.get_label()))
plt.show()