Skip to content

Tweak y-axis label #13298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changes/devel/13298.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix y-axis label in PSD plot when ``dB=True`` to show a more conventional label (i.e., dB/Hz or dB/√Hz), by `Clemens Brunner`_.
15 changes: 7 additions & 8 deletions mne/utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,21 +1013,20 @@ def _reflow_param_docstring(docstring, has_first_line=True, width=75):

_dB = """
dB : bool
Whether to plot on a decibel-like scale. If ``True``, plots
Whether to plot on a decibel scale. If ``True``, plots
10 × log₁₀({quantity}){caveat}.{extra}
"""
_ignored_if_normalize = " Ignored if ``normalize=True``."
_psd = "spectral power"

docdict["dB_plot_psd"] = """\
dB : bool
Plot Power Spectral Density (PSD), in units (amplitude**2/Hz (dB)) if
``dB=True``, and ``estimate='power'`` or ``estimate='auto'``. Plot PSD
in units (amplitude**2/Hz) if ``dB=False`` and,
``estimate='power'``. Plot Amplitude Spectral Density (ASD), in units
(amplitude/sqrt(Hz)), if ``dB=False`` and ``estimate='amplitude'`` or
``estimate='auto'``. Plot ASD, in units (amplitude/sqrt(Hz) (dB)), if
``dB=True`` and ``estimate='amplitude'``.
Plot power spectral density (PSD) in units (dB/Hz) if ``dB=True`` and
``estimate='power'`` or ``estimate='auto'``. Plot PSD in units (amplitude**2/Hz) if
``dB=False`` and ``estimate='power'``. Plot amplitude spectral density (ASD) in
units (amplitude/sqrt(Hz)) if ``dB=False`` and ``estimate='amplitude'`` or
``estimate='auto'``. Plot ASD in units (dB/sqrt(Hz)) if ``dB=True`` and
``estimate='amplitude'``.
"""
docdict["dB_plot_topomap"] = _dB.format(
quantity=_psd,
Expand Down
21 changes: 15 additions & 6 deletions mne/viz/tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,19 @@ def test_plot_raw_filtered(filtorder, raw, browser_backend):
RawArray(np.zeros((1, 100)), create_info(1, 20.0, "stim")).plot(lowpass=5)


def _check_ylabel_psd(ylabel, amplitude, dB, unit):
"""Check that the ylabel is correct."""
if amplitude:
assert r"\sqrt{Hz}" in ylabel
else:
assert "Hz" in ylabel and r"\sqrt{Hz}" not in ylabel
if dB:
assert "dB" in ylabel
else:
assert unit in ylabel, ylabel
assert "dB" not in ylabel


def test_plot_raw_psd(raw, raw_orig):
"""Test plotting of raw psds."""
raw_unchanged = raw.copy()
Expand Down Expand Up @@ -1045,17 +1058,13 @@ def test_plot_raw_psd(raw, raw_orig):
ylabel = fig.axes[0].get_ylabel()
unit = r"fT/cm/\sqrt{Hz}" if amplitude else "(fT/cm)²/Hz"
assert title == "Gradiometers", title
assert unit in ylabel, ylabel
if dB:
assert "dB" in ylabel
else:
assert "dB" not in ylabel
_check_ylabel_psd(ylabel, amplitude, dB, unit)
# check mag axes
title = fig.axes[1].get_title()
ylabel = fig.axes[1].get_ylabel()
unit = r"fT/\sqrt{Hz}" if amplitude else "fT²/Hz"
assert title == "Magnetometers", title
assert unit in ylabel, ylabel
_check_ylabel_psd(ylabel, amplitude, dB, unit)

# test xscale value checking
raw = raw_unchanged
Expand Down
5 changes: 4 additions & 1 deletion mne/viz/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2445,7 +2445,10 @@ def _convert_psds(
if dB:
np.log10(np.maximum(psds, np.finfo(float).tiny), out=psds)
psds *= coef
ylabel = r"$\mathrm{dB}\ $" + ylabel
if estimate == "amplitude":
ylabel = r"$\mathrm{dB/\sqrt{Hz}}$"
else:
ylabel = r"$\mathrm{dB/Hz}$"
ylabel = "Power (" + ylabel if estimate == "power" else "Amplitude (" + ylabel
ylabel += ")"

Expand Down