Skip to content

Add option to show a zero line in browser#14018

Open
cbrnr wants to merge 3 commits into
mne-tools:mainfrom
cbrnr:browser-horizontal-zero
Open

Add option to show a zero line in browser#14018
cbrnr wants to merge 3 commits into
mne-tools:mainfrom
cbrnr:browser-horizontal-zero

Conversation

@cbrnr

@cbrnr cbrnr commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This PR adds an option to show a zero line (toggleable with "0") to the Matplotlib backend. I've implemented this feature for the Qt backend in mne-tools/mne-qt-browser#423.

Short example:

import numpy as np

import mne

mne.viz.set_browser_backend("matplotlib")

sfreq = 200.0
n_channels = 20
t = np.arange(int(60 * sfreq)) / sfreq

rng = np.random.default_rng(0)
offsets = rng.uniform(-100e-6, 100e-6, size=n_channels)
data = rng.standard_normal((n_channels, t.size)) * 20e-6 + offsets[:, np.newaxis]

ch_names = [f"EEG {i + 1}" for i in range(n_channels)]
info = mne.create_info(ch_names, sfreq, "eeg")
raw = mne.io.RawArray(data, info)

raw.plot(show_zero_line=True, scalings=dict(eeg=100e-6), n_channels=5, block=True)

@cbrnr cbrnr requested a review from larsoner as a code owner July 3, 2026 12:11
@cbrnr

cbrnr commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

The docs error (AssertionError: Duplicates in PYPI_PACKAGES and installer packages: ['mne-kit-gui']) is very likely unrelated.

The Azure error is also unrelated (hitting 403: rate limit exceeded).

Comment thread mne/utils/docs.py Outdated
Comment on lines +4277 to +4279
initialized. The zero line marks the true zero of each channel's own
displayed trace, independent of any DC removal or highpass filtering
already applied to the data. Can be toggled after initialization by

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm somehow it's hard for me to parse this. I expected at first that hitting "d" would change the position of the line in the plot. I think calling it "true zero" made me think of "where zero actually is for the signal". So maybe

Suggested change
initialized. The zero line marks the true zero of each channel's own
displayed trace, independent of any DC removal or highpass filtering
already applied to the data. Can be toggled after initialization by
initialized. The zero line marks the centering point of each channel's
**displayed trace**, independent of any DC removal or highpass filtering
already applied to the data. Can be toggled after initialization by

I wonder if there is value in having a third mode where toggling DC removal changes where the "zero line" is displayed so that it's actually at where the zero level is for that channel (in other words, treating "DC removal" as more of a visual vertical translation rather than a subtraction of any values from the trace)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about that as well. Can you think of a good way to visually distinguish the two modes that show the line? Otherwise it might be confusing if the line doesn't simple toggle when pressing "0".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe dashed for one (fixed location at the ch_name) and dotted for the other (where true zero is for the signal)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this might work. Or maybe even clearer, dashed for the fixed location and solid for the true zero? True and solid is intuitive I think.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure that works! That way the fixed looks more like an axis line anyway, which is what is basically is at that point

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But does it make sense to show a zero line at the channel midpoint when DC removal is off? Could we couple the zero line to the currently set DC removal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now set the style of the zero line depending on whether DC removal is on or off. If it's off, we show the true zero line as a solid line. When it's on, we show a "virtual" zero line as a dashed line. Yes, if the signal has an actual mean of zero, the virtual mean will be equal to the true mean, but let's not overcomplicate this. Also, the difference between solid and dashed is pretty hard to spot, but I think it looks OK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants