FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

DOC: Revising the Figure Legend Demo Example by star1327p · Pull Request #29666 · matplotlib/matplotlib · GitHub

Repository navigation

DOC: Revising the Figure Legend Demo Example - #29666

Merged
timhoffm merged 5 commits into
matplotlib:mainfrom
star1327p:figure-legend-demo
Feb 25, 2025
Merged

timhoffm merged 5 commits into
matplotlib:mainfrom
star1327p:figure-legend-demo

Conversation

Copy link
Copy Markdown
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 Bot added the Documentation: examples files in galleries/examples label Feb 22, 2025

timhoffm commented Feb 24, 2025 •
edited
Loading

Copy link
Copy Markdown
Member

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()

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.

Copy link
Copy Markdown
Contributor Author

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

timhoffm added this to the v3.10.0-doc milestone Feb 25, 2025
timhoffm merged commit dd44beb into matplotlib:main Feb 25, 2025

Copy link
Copy Markdown
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 mentioned this pull request Mar 3, 2025
5 tasks
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
wavebyrd pushed a commit to wavebyrd/matplotlib that referenced this pull request Mar 13, 2026
…t-of-pr-29666-on-v3.10.0-doc

Backport PR matplotlib#29666 on branch v3.10.0-doc (DOC: Revising the Figure Legend Demo Example)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL