From 39145ebf04f8bb912b7a55c41d6d831fa1ba3c07 Mon Sep 17 00:00:00 2001 From: "Christine P. Chai" Date: Sat, 22 Feb 2025 09:14:18 -0800 Subject: [PATCH 1/4] DOC: Revising the Figure Legend Demo Example --- .../figlegend_demo.py | 34 +++++++++++++++++-- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/galleries/examples/text_labels_and_annotations/figlegend_demo.py b/galleries/examples/text_labels_and_annotations/figlegend_demo.py index f6f74b837c10..a3c05edd19fe 100644 --- a/galleries/examples/text_labels_and_annotations/figlegend_demo.py +++ b/galleries/examples/text_labels_and_annotations/figlegend_demo.py @@ -31,8 +31,13 @@ # %% # Sometimes we do not want the legend to overlap the Axes. If you use -# *constrained layout* you can specify "outside right upper", and -# *constrained layout* will make room for the legend. +# *constrained layout*, you can specify "outside" at the beginning of +# the `loc` keyword argument. In this way, the legend is drawn outside +# the Axes on the (sub)figure. + +# %% +# This example puts the legends at the "outside upper left" and +# "outside upper right". fig, axs = plt.subplots(1, 2, layout='constrained') @@ -47,7 +52,30 @@ l3, = axs[1].plot(x, y3, color='tab:green') l4, = axs[1].plot(x, y4, color='tab:red', marker='^') -fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='upper left') +fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='outside upper left') +fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='outside upper right') + +plt.show() + +# %% Alternatively, we can put the legends at the "outside left +# upper" and "outside right upper". There is a slight difference +# in the exact location of the legends +# (see :ref:`legend_guide` for more details). + +fig, axs = plt.subplots(1, 2, layout='constrained') + +x = np.arange(0.0, 2.0, 0.02) +y1 = np.sin(2 * np.pi * x) +y2 = np.exp(-x) +l1, = axs[0].plot(x, y1) +l2, = axs[0].plot(x, y2, marker='o') + +y3 = np.sin(4 * np.pi * x) +y4 = np.exp(-2 * x) +l3, = axs[1].plot(x, y3, color='tab:green') +l4, = axs[1].plot(x, y4, color='tab:red', marker='^') + +fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='outside left upper') fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='outside right upper') plt.show() From d9763fdd47af1a34494647ac1586ecc1b740846d Mon Sep 17 00:00:00 2001 From: "Christine P. Chai" Date: Mon, 24 Feb 2025 18:46:52 -0800 Subject: [PATCH 2/4] DOC: Consolidate figlegend_demo.py to one graph --- .../figlegend_demo.py | 77 +++---------------- 1 file changed, 11 insertions(+), 66 deletions(-) diff --git a/galleries/examples/text_labels_and_annotations/figlegend_demo.py b/galleries/examples/text_labels_and_annotations/figlegend_demo.py index a3c05edd19fe..2667acc39de3 100644 --- a/galleries/examples/text_labels_and_annotations/figlegend_demo.py +++ b/galleries/examples/text_labels_and_annotations/figlegend_demo.py @@ -3,79 +3,24 @@ Figure legend demo ================== -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. +Rather than plotting a legend on each axis, a legend for all the artists +on all the sub-axes of a figure can be plotted instead. """ import matplotlib.pyplot as plt import numpy as np - -fig, axs = plt.subplots(1, 2) - -x = np.arange(0.0, 2.0, 0.02) -y1 = np.sin(2 * np.pi * x) -y2 = np.exp(-x) -l1, = axs[0].plot(x, y1) -l2, = axs[0].plot(x, y2, marker='o') - -y3 = np.sin(4 * np.pi * x) -y4 = np.exp(-2 * x) -l3, = axs[1].plot(x, y3, color='tab:green') -l4, = axs[1].plot(x, y4, color='tab:red', marker='^') - -fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='upper left') -fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='upper right') - -plt.tight_layout() -plt.show() - -# %% -# Sometimes we do not want the legend to overlap the Axes. If you use -# *constrained layout*, you can specify "outside" at the beginning of -# the `loc` keyword argument. In this way, the legend is drawn outside -# the Axes on the (sub)figure. - -# %% -# This example puts the legends at the "outside upper left" and -# "outside upper right". - fig, axs = plt.subplots(1, 2, layout='constrained') -x = np.arange(0.0, 2.0, 0.02) -y1 = np.sin(2 * np.pi * x) -y2 = np.exp(-x) -l1, = axs[0].plot(x, y1) -l2, = axs[0].plot(x, y2, marker='o') - -y3 = np.sin(4 * np.pi * x) -y4 = np.exp(-2 * x) -l3, = axs[1].plot(x, y3, color='tab:green') -l4, = axs[1].plot(x, y4, color='tab:red', marker='^') +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((l1, l2), ('Line 1', 'Line 2'), loc='outside upper left') -fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='outside upper right') +fig.legend(loc='outside right upper') plt.show() -# %% Alternatively, we can put the legends at the "outside left -# upper" and "outside right upper". There is a slight difference -# in the exact location of the legends -# (see :ref:`legend_guide` for more details). - -fig, axs = plt.subplots(1, 2, layout='constrained') - -x = np.arange(0.0, 2.0, 0.02) -y1 = np.sin(2 * np.pi * x) -y2 = np.exp(-x) -l1, = axs[0].plot(x, y1) -l2, = axs[0].plot(x, y2, marker='o') - -y3 = np.sin(4 * np.pi * x) -y4 = np.exp(-2 * x) -l3, = axs[1].plot(x, y3, color='tab:green') -l4, = axs[1].plot(x, y4, color='tab:red', marker='^') - -fig.legend((l1, l2), ('Line 1', 'Line 2'), loc='outside left upper') -fig.legend((l3, l4), ('Line 3', 'Line 4'), loc='outside right upper') - -plt.show() +# %% +# The outside positioning is discussed in detail here: +# https://matplotlib.org/stable/users/explain/axes/legend_guide.html#figure-legends From 4140e358d5a321a5263ab70c5b59c8755777d674 Mon Sep 17 00:00:00 2001 From: "Christine P. Chai" Date: Mon, 24 Feb 2025 18:49:35 -0800 Subject: [PATCH 3/4] DOC: Remove trailing white space --- .../examples/text_labels_and_annotations/figlegend_demo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/galleries/examples/text_labels_and_annotations/figlegend_demo.py b/galleries/examples/text_labels_and_annotations/figlegend_demo.py index 2667acc39de3..826e0b8d4a3a 100644 --- a/galleries/examples/text_labels_and_annotations/figlegend_demo.py +++ b/galleries/examples/text_labels_and_annotations/figlegend_demo.py @@ -3,7 +3,7 @@ Figure legend demo ================== -Rather than plotting a legend on each axis, a legend for all the artists +Rather than plotting a legend on each axis, a legend for all the artists on all the sub-axes of a figure can be plotted instead. """ From 7521ff8a2f28f6da9911ea79afef3b142d9824f9 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Tue, 25 Feb 2025 10:17:05 +0100 Subject: [PATCH 4/4] Update galleries/examples/text_labels_and_annotations/figlegend_demo.py --- galleries/examples/text_labels_and_annotations/figlegend_demo.py | 1 + 1 file changed, 1 insertion(+) diff --git a/galleries/examples/text_labels_and_annotations/figlegend_demo.py b/galleries/examples/text_labels_and_annotations/figlegend_demo.py index 826e0b8d4a3a..50b3eeabc085 100644 --- a/galleries/examples/text_labels_and_annotations/figlegend_demo.py +++ b/galleries/examples/text_labels_and_annotations/figlegend_demo.py @@ -9,6 +9,7 @@ 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)