From 9d1ae9185a8ffac58c94db9df96df7c8a5ebd4eb Mon Sep 17 00:00:00 2001 From: Matt Elmer Date: Mon, 12 Dec 2022 12:48:00 -0600 Subject: [PATCH 1/3] Add example to legend guide --- tutorials/intermediate/legend_guide.py | 44 ++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tutorials/intermediate/legend_guide.py b/tutorials/intermediate/legend_guide.py index 596d6df93b55..c5bd824d2579 100644 --- a/tutorials/intermediate/legend_guide.py +++ b/tutorials/intermediate/legend_guide.py @@ -137,6 +137,50 @@ 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 # ================================= From f7be69c44eeb4da37b7fd8f19059e45c4e5789da Mon Sep 17 00:00:00 2001 From: Matt Elmer Date: Mon, 12 Dec 2022 12:48:00 -0600 Subject: [PATCH 2/3] Add example to legend guide --- tutorials/intermediate/legend_guide.py | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tutorials/intermediate/legend_guide.py b/tutorials/intermediate/legend_guide.py index 596d6df93b55..14dbcbc55ff3 100644 --- a/tutorials/intermediate/legend_guide.py +++ b/tutorials/intermediate/legend_guide.py @@ -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 # ================================= From f1ddbf10d83c37af048b836c6059f919083adf29 Mon Sep 17 00:00:00 2001 From: matthewelmer Date: Mon, 12 Dec 2022 13:19:05 -0600 Subject: [PATCH 3/3] Added a line --- tutorials/intermediate/legend_guide.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tutorials/intermediate/legend_guide.py b/tutorials/intermediate/legend_guide.py index c5bd824d2579..14dbcbc55ff3 100644 --- a/tutorials/intermediate/legend_guide.py +++ b/tutorials/intermediate/legend_guide.py @@ -140,6 +140,7 @@ ############################################################################### # 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 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy