pythonmatplotlibjupyter-notebookmouse

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


Note: This is a question relating to mouse EEG data plotting.

I made a plot showing the averaged trial signals for filtered EEG electrode channels. While plotting this I saw a few signals that I want to exclude from my plot, but I don't have a way to tell what channels were plotted. Is there a way to add something that would allow me to click on or hover over one of the plotted lines/channels and have my jupyter notebook tell me what channel I clicked/am hovering over?

This is the plot I am hoping to make clickable: EEG channel plot

Here is the code I used to make the plots if that's helpful:

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')   

Solution

  • You can add a label to each of the curves and then use mplcursors to show an annotation while hovering (or when clicking with hover=False).

    Note that to have an interactive plot in a Jupyter notebook, %matplotlib notebook (this might depend on how Jupyter is installed) is needed instead of just %matplotlib inline (which generates static images). See Docs.

    Here is an example showing the general idea with some test data:

    %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()
    

    mplcursors to show the index of a curve