Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DOC: Revising the Figure Legend Demo Example #29666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 25, 2025

Conversation

star1327p
Copy link
Contributor

PR summary

Revising the Figure Legend Demo Example. Elaborated on the difference between outside left upper and outside upper right of legend locations.
https://matplotlib.org/devdocs/gallery/text_labels_and_annotations/figlegend_demo.html

PR checklist

@github-actions github-actions bot added the Documentation: examples files in galleries/examples label Feb 22, 2025
@timhoffm
Copy link
Member

timhoffm commented Feb 24, 2025

The example claims "Instead of plotting a legend on each axis, a legend for all the artists on all the sub-axes of a figure can be plotted instead." but it does not do this.

I think we can consolidate the whole example to

import matplotlib.pyplot as plt
import numpy as np
fig, axs = plt.subplots(1, 2, layout='constrained')

x = np.arange(0.0, 4*np.pi, 0.2)
axs[0].plot(x, np.sin(x), label='Line 1')
axs[0].plot(x, np.exp(-x/2), marker='o', label='Line 2')
axs[1].plot(x, np.sin(x), color='tab:green', label='Line 3')
axs[1].plot(x, np.exp(-x/4), color='tab:red', marker='^', label='Line 4')

fig.legend(loc='outside right upper')

plt.show()

image

fig.legend() will also auto-detect the labelled Artists across all axes, so the recommended way is to pass label=... to the plot functions and let the legend detect these. Explicitly passing handles and labels is only needed for very special cases where you need more control, and it's the same as for ax.legend() so does not need mention here.

Also, I believe 'outside right upper' is the most used position, so let's use that in the example.

The outside positioning is discussed in detail here: https://matplotlib.org/stable/users/explain/axes/legend_guide.html#figure-legends. I'd prefer a link instead of discussing this again in a second plot.

@star1327p
Copy link
Contributor Author

@timhoffm Thank you so much for consolidating the example! I have incorporated the new version into this PR.

@timhoffm timhoffm added this to the v3.10.0-doc milestone Feb 25, 2025
@timhoffm timhoffm merged commit dd44beb into matplotlib:main Feb 25, 2025
22 checks passed
@timhoffm
Copy link
Member

Thanks @star1327p!

meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Feb 25, 2025
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Feb 25, 2025
timhoffm added a commit that referenced this pull request Feb 25, 2025
…666-on-v3.10.x

Backport PR #29666 on branch v3.10.x (DOC: Revising the Figure Legend Demo Example)
timhoffm added a commit that referenced this pull request Feb 25, 2025
…666-on-v3.10.0-doc

Backport PR #29666 on branch v3.10.0-doc (DOC: Revising the Figure Legend Demo Example)
@ksunden ksunden mentioned this pull request Mar 3, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation: examples files in galleries/examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants