Repository: mne-tools/mne-python

Issue:
Simplify auto legend from evoked. comment in plot_compare_evokeds
I am thinking of simplifying the `nave` proportions algebra in `evoked. comment` for the results from `plot_compare_evokdes` to make a more informative legend. Here is an, e.g., output **screenshot**

<img width="1994" alt="Screen Shot 2022-08-02 at 5 09 25 PM" src="https://user-images.githubusercontent.com/4381290/182497475-4d3a64d1-7788-4f27-9db2-047cd4aa8ed1.png">

The text in `evoked.comment` renders unusable image file formats unless figure legends are dealt with on the fly. If that's the intended usage, then disregard this. Otherwise, I am curious if a fix is a suitable task to piggyback here?

Resulting `.png`
![compare-evokeds](https://user-images.githubusercontent.com/4381290/182498000-b9508dd4-c5e0-452b-883d-c6ad04541c87.png)

_Originally posted by @ktavabi in https://github.com/mne-tools/mne-python/issues/10467#issuecomment-1203342652_


Discussion:
My bandwidth for this stuff is very limited right now, if this isn't pressing, assign it to me, give me a deadline and I'll get it done.

Resolving patch:
diff --git a/mne/viz/evoked.py b/mne/viz/evoked.py
--- a/mne/viz/evoked.py
+++ b/mne/viz/evoked.py
@@ -1897,10 +1897,20 @@ def _draw_legend_pce(legend, split_legend, styles, linestyles, colors, cmap,
         legend_params.update(loc='lower right', bbox_to_anchor=(1, 1))
     # draw the legend
     if any([draw_styles, draw_colors, draw_linestyles]):
-        labels = [line.get_label() for line in lines]
+        labels = [_abbreviate_label(line.get_label()) for line in lines]
         ax.legend(lines, labels, **legend_params)
 
 
+_LABEL_LIMIT = 40
+
+
+# don't let labels be excessively long
+def _abbreviate_label(label):
+    if len(label) > _LABEL_LIMIT:
+        label = label[:_LABEL_LIMIT] + ' …'
+    return label
+
+
 def _draw_axes_pce(ax, ymin, ymax, truncate_yaxis, truncate_xaxis, invert_y,
                    vlines, tmin, tmax, unit, skip_axlabel=True, time_unit='s'):
     """Position, draw, and truncate axes for plot_compare_evokeds."""


Test patch:
diff --git a/mne/viz/tests/test_evoked.py b/mne/viz/tests/test_evoked.py
--- a/mne/viz/tests/test_evoked.py
+++ b/mne/viz/tests/test_evoked.py
@@ -364,6 +364,7 @@ def test_plot_compare_evokeds():
         assert fig[0].axes[0].get_title().endswith(_t)
     # test passing more than one evoked
     red, blue = evoked.copy(), evoked.copy()
+    red.comment = red.comment + '*' * 100
     red.data *= 1.5
     blue.data /= 1.5
     evoked_dict = {'aud/l': blue, 'aud/r': red, 'vis': evoked}
@@ -475,8 +476,11 @@ def ci_func(array):
     plot_compare_evokeds(csd, picks='csd', axes='topo')
     # old tests
     red.info['chs'][0]['loc'][:2] = 0  # test plotting channel at zero
-    plot_compare_evokeds([red, blue], picks=[0],
-                         ci=lambda x: [x.std(axis=0), -x.std(axis=0)])
+    fig, = plot_compare_evokeds(
+        [red, blue], picks=[0], ci=lambda x: [x.std(axis=0), -x.std(axis=0)])
+    # reasonable legend lengths
+    leg_texts = [t.get_text() for t in fig.axes[0].get_legend().get_texts()]
+    assert all(len(lt) < 50 for lt in leg_texts)
     plot_compare_evokeds([list(evoked_dict.values())], picks=[0],
                          ci=_parametric_ci)
     # smoke test for tmin >= 0 (from mailing list)