Reputation: 149
Plot left-right moviment works with matplotlib, but not with MNE EEG. It scrolls correctly left and right using mouse wheel but doesnot update plot image. Instead when plot is moved left or right there are blank spaces on sides of plot. How update plot image after moving it. I'm using MNE EEG Python 1.8. I don't want to scroll with matplotlib, I want it with MNE.
import mne
import matplotlib.pyplot as plt
class EEGAnalyzer:
def __init__(self):
self.fig_home = None
self.raw = None # Initialize a variable to store raw data
def on_scroll(self, event):
"""Handle mouse wheel scroll events to move the plot left or right."""
if self.raw is None or self.ax is None:
return
current_xlims = self.ax.get_xlim() # Get current limits
if event.button == 'up':
new_xlims = [current_xlims[0] + 1, current_xlims[1] + 1] # Example increment
elif event.button == 'down':
new_xlims = [current_xlims[0] - 1, current_xlims[1] - 1] # Example decrement
else:
return
# Update axes limits
self.ax.set_xlim(new_xlims)
# Optionally update the plot for visibility
self.fig_home.canvas.draw_idle() # Redraw the canvas
def plot_raw_data(self, raw):
"""
Plots raw EEG data using MNE-Python.
Args:
raw: The MNE Raw object containing the EEG data.
"""
if hasattr(self, 'fig_home') and self.fig_home is not None:
plt.close(self.fig_home)
self.raw = raw # Store the raw data
self.fig_home = raw.plot(show=False, block=False) # Create the plot without blocking
# Access the Axes object for future modifications
self.ax = self.fig_home.axes[0]
# Connect the scroll event to the handler
self.cid_scroll = self.fig_home.canvas.mpl_connect('scroll_event', self.on_scroll)
# Use %matplotlib qt for interactive plotting, but only in a Jupyter notebook
try:
get_ipython()
%matplotlib qt
except NameError:
plt.ion() # For other environments (like scripts)
# Show the plot
plt.show()
# Example usage
analyzer = EEGAnalyzer()
# Replace with the path to your EEG data
raw = mne.io.read_raw("C:\\000_tmp\\fif\\3r_eeg.fif", preload=True) # Ensure data is preloaded
analyzer.plot_raw_data(raw)
Upvotes: 0
Views: 38