Skip to content
Merged
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/dev/13765.newfeature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow per-channel color overrides in :func:`mne.viz.plot_raw` via channel name keys in the ``color`` dict, by :newcontrib:`Hansuja Budhiraja`.
9 changes: 7 additions & 2 deletions mne/viz/_mpl_figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2006,8 +2006,13 @@ def _draw_traces(self):
)
offsets = self.mne.trace_offsets[offset_ixs]
bad_bool = np.isin(ch_names, self.mne.info["bads"])
# colors
good_ch_colors = [self.mne.ch_color_dict[_type] for _type in ch_types]
# colors: allow overrides by channel name, then by channel type
good_ch_colors = []
for _name, _type in zip(ch_names, ch_types):
if _name in self.mne.ch_color_dict:
good_ch_colors.append(self.mne.ch_color_dict[_name])
else:
good_ch_colors.append(self.mne.ch_color_dict[_type])
ch_colors = to_rgba_array(
[
self.mne.ch_color_bad if _bad else _color
Expand Down
4 changes: 4 additions & 0 deletions mne/viz/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def plot_raw(
emg='k', ref_meg='steelblue', misc='k', stim='k',
resp='k', chpi='k')

If a dict, keys can be channel *types* (e.g., ``'eeg'``) and/or
channel *names* (e.g., ``'SFG, Left'``); name-based entries
take precedence over type-based ones.

bad_color : color object
Color to make bad channels.
%(event_color)s
Expand Down
26 changes: 26 additions & 0 deletions mne/viz/tests/test_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from copy import deepcopy
from pathlib import Path

import matplotlib.colors as mcolors
import matplotlib.pyplot as plt
import numpy as np
import pytest
Expand Down Expand Up @@ -317,6 +318,31 @@ def test_scale_bar(browser_backend):
bar_lims = bar.get_ydata()
assert_allclose(y_lims, bar_lims, atol=1e-4)

# Per-channel color overrides via channel names (matplotlib only).
if ismpl:
sfreq = 100.0
ch_names = ["SFG, Left", "SFG, Right", "MFG, Left"]
info = create_info(ch_names=ch_names, sfreq=sfreq, ch_types="eeg")
data = np.zeros((len(ch_names), int(sfreq))) # 1 second of zeros
raw2 = RawArray(data, info)

color = {"eeg": "k", "SFG, Left": "red"}
browser_backend._close_all()
fig2 = plot_raw(raw2, color=color, show=False)

# ch_colors stores the "good" (non-bad) colors, in visible channel order
assert fig2.mne.ch_colors[0] == "red"
assert fig2.mne.ch_colors[1] == "k"
assert fig2.mne.ch_colors[2] == "k"

# check colours on the plot are also correct
for trace, ch_color in zip(fig2.mne.traces, fig2.mne.ch_colors):
assert np.allclose(
mcolors.to_rgba(trace.get_color()), mcolors.to_rgba(ch_color)
), f"Expected {ch_color}, got {trace.get_color()}"

browser_backend._close_all()


def test_plot_raw_selection(raw, browser_backend):
"""Test selection mode of plot_raw()."""
Expand Down
Loading