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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tutorials/intermediate/legend_guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,51 @@

plt.show()

###############################################################################
# If you have multiple plots side-by-side each with a common legend and wish to
# instead have one legend that represents all subplots, you can do this:

import numpy as np
import matplotlib.pyplot as plt

# Make a mosaic of subplots and use the top one for the legend.
fig, axes = plt.subplot_mosaic(
(("legend", "legend", "legend"), # Top row has one column.
("f", "df_deta", "d2f_deta2")), # Bottom row has three columns.
height_ratios=(0.1, 0.9), # Size the dummy plot appropriately.
num="distributions") # I like to name my figures. :)
fig.set_constrained_layout(True)

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)

for axis in axes:
# Make the dummy plot blank.
if axis == "legend":
axes[axis].axis("off")
continue

# Plot data
axes["f"].plot(f, eta, label=f"$\Lambda = {cap_lambda}$")
axes["df_deta"].plot(df_deta, eta, label=f"$\Lambda = {cap_lambda}$")
axes["d2f_deta2"].plot(d2f_deta2, eta, label=f"$\Lambda = {cap_lambda}$")

# Add a legend to the dummy plot, using the handles from the plot you want to
# create the legend for.
axes["legend"].legend(
handles=axes["f"].get_legend_handles_labels()[0],
loc="center", ncol=len(cap_lambda_vals),
mode="expand", borderaxespad=0)

plt.show() # Ta-daa! (They look better if you squish em down a bit)

###############################################################################
# Multiple legends on the same Axes
# =================================
Expand Down