[GUI] Preserve legend settings when regenerating in figure options by yushuosun · Pull Request #32285 · matplotlib/matplotlib · GitHub
Skip to content

[GUI] Preserve legend settings when regenerating in figure options - #32285

Open
yushuosun wants to merge 1 commit into
matplotlib:mainfrom
yushuosun:fix/qt-figureoptions-legend-state
Open

[GUI] Preserve legend settings when regenerating in figure options#32285
yushuosun wants to merge 1 commit into
matplotlib:mainfrom
yushuosun:fix/qt-figureoptions-legend-state

Conversation

@yushuosun

Copy link
Copy Markdown

Summary

When users click "(Re-)Generate automatic legend" in the Qt figure options editor, only ncols was preserved, so custom legend settings like location, bounding-box anchor, and style were silently lost.

Changes

  • Copy key legend settings before regenerating in figureoptions.figure_edit:
    • location, bbox anchor/transform, column count, title
    • spacing/style fields (numpoints, markerscale, scatterpoints, paddings, etc.)
    • frame and alignment settings (fancybox, frameon, framealpha, shadow, edge/face colors)
    • marker/line/text style via prop and title font properties
  • Add regression test test_figureoptions_preserves_legend_settings ensuring regenerated legend keeps those settings.

Notes

No behavioral changes when "(Re-)Generate automatic legend" is unchecked.

Copilot AI lite review requested due to automatic review settings September 4, 2026 00:35
@github-actions

github-actions Bot commented Sep 4, 2026

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment on lines +252 to +260
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)
Comment on lines +288 to +292
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,
@iccir

iccir commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants