I was trying to create interactive topoplot with slider in MNE and got stuck. Topomap (or topoplot) is drawn, but it doesn’t update when I move slider. Do you have any idea how to make it interactive?
EDIT: I want to scroll through time
data:
import mne
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.widgets import Slider, Button
%matplotlib widget
biosemi_montage = mne.channels.make_standard_montage('biosemi64')
n_channels = len(biosemi_montage.ch_names)
fake_info = mne.create_info(ch_names=biosemi_montage.ch_names, sfreq=250.,
ch_types='eeg')
rng = np.random.RandomState(0)
data = rng.normal(size=(n_channels, 100)) * 1e-6
fake_evoked = mne.EvokedArray(data, fake_info)
fake_evoked.set_montage(biosemi_montage)
code
fig, ax = plt.subplots()
init_time = 0
timing = [round(i, ndigits=3) for i in list(np.linspace(0, 1, 100))]
voltage = fake_evoked.data[:, timing.index(init_time)]
mne.viz.plot_topomap(voltage, fake_evoked.info, axes=ax,
image_interp='cubic',
show=False)
ax.set_title('MNE', fontweight='bold')
axtime = fig.add_axes([0.25, 0.01, 0.65, 0.04])
time_slider = Slider(
ax=axtime,
label='Time [ms]',
valmin=0, valmax=1, valstep=100,
valinit=init_time,
)
plt.text(0.9, 2, init_time)
# The function to be called anytime a slider's value changes
def update(val):
#init_time = round(time_slider.val, ndigits=3)
#mne.viz.plot_topomap(voltage[:, timing.index(init_time)], fake_evoked.info, axes=ax,
# show=False)
#voltage = fake_evoked.data[:, timing.index(init_time)]
#plt.text(0.9, 2, init_time)
fig.canvas.draw_idle()
# register the update function with each slider
time_slider.on_changed(update)
plt.show()
I was inspired with this example, but the use a method line.set_ydata which definetely could be used for topoplot.