[GUI] Preserve legend settings when regenerating in figure options - #32285
[GUI] Preserve legend settings when regenerating in figure options#32285yushuosun wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The new regression test’s fedit mock returns data in a shape that does not match what apply_callback expects for curve settings, which will fail when the Axes has labeled lines.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR updates the Qt figure options editor so that when users click “(Re-)Generate automatic legend”, it re-creates the legend while preserving key legend configuration (location/anchor, styling, spacing, frame, and font settings), and adds a regression test to ensure those settings are retained.
Changes:
- Preserve existing legend parameters (loc, bbox anchor/transform, spacing, frame styling, fonts, alignment, etc.) when regenerating the legend in
figureoptions.figure_edit. - Add a Qt backend regression test that exercises legend regeneration and asserts key properties remain unchanged.
File summaries
| File | Description |
|---|---|
| lib/matplotlib/backends/qt_editor/figureoptions.py | Copies legend settings from the existing legend into the regenerated legend. |
| lib/matplotlib/tests/test_backend_qt.py | Adds a regression test for preserving legend settings across regeneration. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 2
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| def fake_fedit(datalist, **kwargs): | ||
| general = list(datalist[0][0]) | ||
| general[-1] = True | ||
| payload = [general] | ||
| if len(datalist) > 1: | ||
| payload.append(list(datalist[1])) | ||
| if len(datalist) > 2: | ||
| payload.append(list(datalist[2])) | ||
| kwargs["apply"](payload) |
| edgecolor=old_legend.legendPatch.get_edgecolor(), | ||
| facecolor=old_legend.legendPatch.get_facecolor(), | ||
| linewidth=old_legend.legendPatch.get_linewidth(), | ||
| alignment=old_legend._alignment, | ||
| prop=old_legend.prop, |

Summary
When users click "(Re-)Generate automatic legend" in the Qt figure options editor, only
ncolswas preserved, so custom legend settings like location, bounding-box anchor, and style were silently lost.Changes
figureoptions.figure_edit:numpoints,markerscale,scatterpoints, paddings, etc.)fancybox,frameon,framealpha,shadow, edge/face colors)propand title font propertiestest_figureoptions_preserves_legend_settingsensuring regenerated legend keeps those settings.Notes
No behavioral changes when
"(Re-)Generate automatic legend"is unchecked.