Skip to content

Fix legend settings not preserved when regenerating in Qt dialog#31934

Open
M-ndak wants to merge 1 commit into
matplotlib:mainfrom
M-ndak:fix/legend-options-dialog
Open

Fix legend settings not preserved when regenerating in Qt dialog#31934
M-ndak wants to merge 1 commit into
matplotlib:mainfrom
M-ndak:fix/legend-options-dialog

Conversation

@M-ndak

@M-ndak M-ndak commented Jun 22, 2026

Copy link
Copy Markdown

Clone ncols, shadow, fancybox, framealpha, title, loc, mode, borderaxespad and bbox_to_anchor from the existing legend before regenerating, so custom legend state is not discarded.

Closes #17775

PR summary

When the "(Re-)Generate automatic legend" checkbox is used in the Qt
figure options dialog, the existing code only preserved ncols and
draggable. All other custom legend settings (shadow, fancybox,
framealpha, title, loc, mode, borderaxespad, bbox_to_anchor) were
silently discarded.

This fix reads all relevant properties from the existing legend before
calling axes.legend() again, so the regenerated legend matches the
original state.

Minimum reproducible example:

import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.plot(range(5), label='a')
ax.plot(range(3)[::-1], label='b')
ax.legend(bbox_to_anchor=(0,1.02,1,0.2), loc="lower left", 
          mode="expand", borderaxespad=0, ncols=3)
plt.show()
# Open figure options, tick "(Re)-generate legend", click OK
# Legend settings are lost

AI Disclosure

Used Claude (Anthropic) to assist with identifying the correct
attributes to read from the Legend object and to verify the fix
via a test script.

PR checklist

Clone ncols, shadow, fancybox, framealpha, title, loc, mode,
borderaxespad and bbox_to_anchor from the existing legend before
regenerating, so custom legend state is not discarded.

Closes matplotlib#17775
@github-actions

Copy link
Copy Markdown

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process.

You can also join us on discourse chat for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide.
Please let us know if (and how) you use AI, it will help us give you better feedback on your PR.

We strive to be a welcoming and open project. Please follow our Code of Conduct.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clone more legend settings when regenerating in Qt figure options dialog

1 participant