Skip to content

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

Closed
wants to merge 4 commits into from
Closed

Legend guide addition #24707

wants to merge 4 commits into from

Conversation

matthewelmer
Copy link

@matthewelmer matthewelmer commented Dec 12, 2022

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

  • [N/A] Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • [N/A] New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@matthewelmer
Copy link
Author

Ah, shoot. I didn't check indentation. Sorry about that.

Copy link

@github-actions github-actions bot left a 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.

@rcomer
Copy link
Member

rcomer commented Dec 22, 2022

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

temp

@matthewelmer
Copy link
Author

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!

@matthewelmer matthewelmer deleted the legend-guide-addition branch January 2, 2023 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

2 participants