-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Legend guide addition #24707
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
Legend guide addition #24707
Conversation
…otlib into legend-guide-addition
Ah, shoot. I didn't check indentation. Sorry about that. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for opening your first PR into Matplotlib!
If you have not heard from us in a while, please feel free to ping @matplotlib/developers
or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.
You can also join us on gitter for real-time discussion.
For details on testing, writing docs, and our review process, please see the developer guide
We strive to be a welcoming and open project. Please follow our Code of Conduct.
Hi @matthewelmer, thanks for the proposal. This is a neat solution for getting the legend nicely placed with current Matplotlib capability. However, you may be interested in the proposed new functionality at #19743. I think this will allow users to get the desired layout more simply: import numpy as np
import matplotlib.pyplot as plt
fig, axes = plt.subplots(1, 3, layout="constrained")
eta = np.linspace(0, 1)
cap_lambda_vals = np.array([-24, -12, 0, 12, 24])
for cap_lambda in cap_lambda_vals:
# Some data
f = 2 * eta - 2 * eta**3 + eta**4 + cap_lambda * eta / 6 * (1 - eta)**3
df_deta = 2 - 6 * eta**2 + 4 * eta**3 + cap_lambda / \
6 * (1 - 6 * eta + 9 * eta**2 - 4 * eta**3)
d2f_deta2 = -12 * eta + 12 * eta**2 + \
cap_lambda / 6 * (-6 + 18 * eta - 12 * eta**2)
# Plot data
axes[0].plot(f, eta, label=f"$\Lambda = {cap_lambda}$")
axes[1].plot(df_deta, eta, label=f"$\Lambda = {cap_lambda}$")
axes[2].plot(d2f_deta2, eta, label=f"$\Lambda = {cap_lambda}$")
# Add a legend to the figure, using the handles from the plot you want to
# create the legend for.
fig.legend(loc="outside upper center", ncol=len(cap_lambda_vals),
handles=axes[0].get_legend_handles_labels()[0],
mode="expand")
plt.show() |
That's great! Thank you for bringing this to my attention. I'm happy to hear that a feature I have wanted for so long is being implemented! |
PR Summary
This pull request adds an example to the legend guide. While the legend guide already has a nice way of putting a legend above a subplot, it didn't have an example showing how to do a common legend above multiple subplots. I found a pretty good way to do it, and I wanted to share!
PR Checklist
Documentation and Tests
pytest
passes)Release Notes
.. versionadded::
directive in the docstring and documented indoc/users/next_whats_new/
.. versionchanged::
directive in the docstring and documented indoc/api/next_api_changes/
next_whats_new/README.rst
ornext_api_changes/README.rst