diff --git a/doc/changes/dev/13686.bugfix.rst b/doc/changes/dev/13686.bugfix.rst new file mode 100644 index 00000000000..58fbd5229fc --- /dev/null +++ b/doc/changes/dev/13686.bugfix.rst @@ -0,0 +1 @@ +Fix sensor name ordering in :func:`~mne.viz.plot_topomap` when passing an :class:`~mne.Info` object as ``pos`` with ``names`` argument, by :newcontrib:`Famous Raj Bhat`. \ No newline at end of file diff --git a/mne/viz/tests/test_topomap.py b/mne/viz/tests/test_topomap.py index 64a13b46006..d50cefc46d3 100644 --- a/mne/viz/tests/test_topomap.py +++ b/mne/viz/tests/test_topomap.py @@ -987,3 +987,21 @@ def test_plot_ch_adjacency(): msg = "Editing a 3d adjacency plot is not supported." with pytest.raises(ValueError, match=msg): plot_ch_adjacency(info, adj, ch_names, kind="3d", edit=True) + + +def test_plot_topomap_info_names_ordering(): + """Regression test for GH-12700. + + plot_topomap() must preserve correct sensor name ordering when + passing an Info object as pos with a names argument. + """ + info = create_info(ch_names=["Fp1", "Fp2", "Fz"], sfreq=1000.0, ch_types="eeg") + montage = make_standard_montage("standard_1020") + info.set_montage(montage) + data = np.array([1.0, 2.0, 3.0]) + names = ["Fp1", "Fp2", "Fz"] + im, _ = plot_topomap(data, info, names=names, show=False) + assert im is not None + displayed_names = [t.get_text() for t in im.axes.texts] + for name in names: + assert name in displayed_names, f"{name!r} not found in {displayed_names}" diff --git a/mne/viz/topomap.py b/mne/viz/topomap.py index 1bd585bb4f1..fde53616e98 100644 --- a/mne/viz/topomap.py +++ b/mne/viz/topomap.py @@ -1292,9 +1292,13 @@ def _plot_topomap( pos = _find_topomap_coords(pos, picks=picks[::2], sphere=sphere) data, _ = _merge_ch_data(data[picks], ch_type, []) data = data.reshape(-1) + if names is not None: + names = [names[p] for p in picks[::2]] else: picks = list(range(data.shape[0])) pos = _find_topomap_coords(pos, picks=picks, sphere=sphere) + if names is not None: + names = [names[p] for p in picks] extrapolate = _check_extrapolate(extrapolate, ch_type) if data.ndim > 1: