Skip to content

Commit 4b52dd6

Browse files
committed
BUG: Fix bug with too many legend entries
1 parent 2a97333 commit 4b52dd6

File tree

6 files changed

+23
-10
lines changed

6 files changed

+23
-10
lines changed

Diff for: doc/changes/devel/bugfix.rst

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix bug with :meth:`mne.preprocessing.ICA.plot_sources` for ``evoked`` data where the
2+
legend contained too many entries, by `Eric Larson`_.

Diff for: examples/preprocessing/muscle_ica.py

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
artifact is produced during postural maintenance. This is more appropriately
1212
removed by ICA otherwise there wouldn't be any epochs left! Note that muscle
1313
artifacts of this kind are much more pronounced in EEG than they are in MEG.
14-
1514
"""
1615
# Authors: Alex Rockhill <[email protected]>
1716
#

Diff for: mne/report/report.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ def _fig_to_img(fig, *, image_format="png", own_figure=True):
430430
if pil_kwargs:
431431
# matplotlib modifies the passed dict, which is a bug
432432
mpl_kwargs["pil_kwargs"] = pil_kwargs.copy()
433-
with warnings.catch_warnings():
434-
fig.savefig(output, format=image_format, dpi=dpi, **mpl_kwargs)
433+
434+
fig.savefig(output, format=image_format, dpi=dpi, **mpl_kwargs)
435435

436436
if own_figure:
437437
plt.close(fig)

Diff for: mne/viz/ica.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,18 @@ def _plot_ica_sources_evoked(evoked, picks, exclude, title, show, ica, labels=No
855855
lines[-1].set_pickradius(3.0)
856856

857857
ax.set(title=title, xlim=times[[0, -1]], xlabel="Time (ms)", ylabel="(NA)")
858-
if len(lines):
859-
ax.legend(lines, exclude_labels, loc="best")
858+
leg_lines_labels = list(
859+
zip(
860+
*[
861+
(line, label)
862+
for line, label in zip(lines, exclude_labels)
863+
if label is not None
864+
]
865+
)
866+
)
867+
if len(leg_lines_labels):
868+
leg_lines, leg_labels = leg_lines_labels
869+
ax.legend(leg_lines, leg_labels, loc="best")
860870

861871
texts.append(
862872
ax.text(

Diff for: mne/viz/tests/test_ica.py

+3
Original file line numberDiff line numberDiff line change
@@ -362,12 +362,15 @@ def test_plot_ica_sources(raw_orig, browser_backend, monkeypatch):
362362
ica.plot_sources(epochs)
363363
ica.plot_sources(epochs.average())
364364
evoked = epochs.average()
365+
ica.exclude = [0]
365366
fig = ica.plot_sources(evoked)
366367
# Test a click
367368
ax = fig.get_axes()[0]
368369
line = ax.lines[0]
369370
_fake_click(fig, ax, [line.get_xdata()[0], line.get_ydata()[0]], "data")
370371
_fake_click(fig, ax, [ax.get_xlim()[0], ax.get_ylim()[1]], "data")
372+
leg = ax.get_legend()
373+
assert len(leg.get_texts()) == len(ica.exclude) == 1
371374

372375
# plot with bad channels excluded
373376
ica.exclude = [0]

Diff for: tutorials/preprocessing/40_artifact_correction_ica.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,10 @@
416416
ica.plot_sources(eog_evoked)
417417

418418
# %%
419-
# Note that above we used `~mne.preprocessing.ICA.plot_sources` on both
420-
# the original `~mne.io.Raw` instance and also on an
421-
# `~mne.Evoked` instance of the extracted EOG artifacts. This can be
422-
# another way to confirm that `~mne.preprocessing.ICA.find_bads_eog` has
423-
# identified the correct components.
419+
# Note that above we used :meth:`~mne.preprocessing.ICA.plot_sources` on both the
420+
# original :class:`~mne.io.Raw` instance and also on an `~mne.Evoked` instance of the
421+
# extracted EOG artifacts. This can be another way to confirm that
422+
# :meth:`~mne.preprocessing.ICA.find_bads_eog` has identified the correct components.
424423
#
425424
#
426425
# Using a simulated channel to select ICA components

0 commit comments

Comments
 (0)