FIX: Lock axes bounds in plot_joint to prevent shrinking on cursor motion (closes #14317) - #14319
AkhilG-exe wants to merge 2 commits into
Conversation
…tion (mne-tools#14317) The ConnectionPatch lines between the time series and topomaps are not clipped to the timeseries axes, so matplotlib constrained layout counts their extent in the axes margins on every draw. The measured margin grows monotonically, shrinking the axes to zero on each redraw (which cursor motion triggers). Mark the connection lines as out-of-layout (set_in_layout(False)) so the layout ignores them.
CarinaFo
left a comment
There was a problem hiding this comment.
Thanks for the quick bugfix, looks good. I verified the bug and the fix on Windows 11.
drammock
left a comment
There was a problem hiding this comment.
Thanks for submitting a fix. change looks good, test could be cleaner.
| ) | ||
| # regression test for gh-14317: repeated redraws (e.g. when moving the cursor | ||
| # over the interactive figure) must not shrink the axes | ||
| figs = fig if isinstance(fig, list) else [fig] |
There was a problem hiding this comment.
this line should be unnecessary; in a test like this the input data is known so the output of plot_joint (figure or list) is also known. Moreover: tests should be fast, so if the input data is generating multiple figures you should change the test so it only generates one (there's nothing about mag vs grad vs eeg that is expected to behave differently here, so testing with just one sensor type should be adequate).
There was a problem hiding this comment.
Thanks for the feedback, @drammock! I've updated the test to pass picks='grad' directly into plot_joint to isolate a single sensor type. This makes the test run significantly faster and allowed me to drop the redundant list-checking normalization entirely. Ready for another look!
Why
evoked.plot_joint()figures shrink to zero once the cursor moves (or hovers) over them (closes #14317).The joint plot is a
layout="constrained"figure. The connection lines between the time series and the topomaps are drawn asConnectionPatch(..., clip_on=False)artists added to the butterfly (ts_ax) axes. Matplotlib's constrained layout computes each axes' margins fromAxes.get_tightbbox(for_layout_only=True), which includes every artist not fully clipped to the axes. Because these lines poke outsidets_axand never move with it, every redraw (which cursor motion triggers through the interactive hover/_BlitManagerpath) measures a larger margin than the last, and the solver turns that growth back into a smaller axes — a feedback loop collapsing all axes to zero.Reproduces with a plain sequence of
fig.canvas.draw()calls:What
mne/viz/evoked.py: callcon.set_in_layout(False)on the connection lines so constrained layout ignores them. This is the matplotlib-idiomatic mechanism for artists that intentionally leave their axes; rendering is unchanged and the initial layout is essentially the same.mne/viz/tests/test_topo.py: regression assertions added to the existingtest_plot_joint— the largest axes' position must be unchanged after repeated redraws.doc/changes/dev/14319.bugfix.rst: changelog fragment.doc/changes/names.inc: new contributor entry.Test plan
pytest mne/viz/tests/test_topo.py mne/viz/tests/test_evoked.py→ 25 passedtest_plot_jointfails without the fix (regression) and passes with itTFR joint-plot tests (
test_tfr_plot_joint,test_tfr_plot_joint_doesnt_modify) → 16 passed (TFR path was already safe: itsConnectionPatchis attached to the figure, not an axes)ruff check/ruff format --checkclean on modified filesBefore/after visual verification: pre-fix renders drift by 122,985 pixels over 9 redraws; post-fix renders are bit-identical (0 pixels).
Disclosure per the AI-assistance policy: this change was developed with assistance from an AI coding agent (opencode), which diagnosed the constrained-layout feedback loop, while I implemented the one-line fix, and drafted the regression test. I reviewed and verified all changes. as well.