From b09e46b66443e11b4b4770e7572cd51100eaa97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Fri, 20 Oct 2023 22:39:36 +0200 Subject: [PATCH 01/12] Correct docs and add code regarding multi-column legends --- examples/gallery/embellishments/legend.py | 43 ++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index f2f80128b9e..a0ba61ba13b 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -3,13 +3,17 @@ ====== The :meth:`pygmt.Figure.legend` method can automatically create a legend for -symbols plotted using :meth:`pygmt.Figure.plot`. Legend entries are only -created when the ``label`` parameter is used. For more complicated legends, -including legends with multiple columns, users have to write an ASCII file -with instructions for the layout of the legend items and pass it -to the ``spec`` parameter of :meth:`pygmt.Figure.legend`. For details on -how to set up such a file, please see the GMT documentation at -:gmt-docs:`legend.html#legend-codes`. +symbols plotted using :meth:`pygmt.Figure.plot`. A legend entry is only added +when the ``label`` parameter is used to state the desired text. Optionally, +to adjust the legend, users can append different modifiers. A list of all +available modifiers can be found at :gmt-docs:`gmt.html#l-full`. To create a +multiple-column legend **+N** is used with the desired number of columns. +For more complicated legends, users may want to write an ASCII file with +instructions for the layout of the legend items and pass it to the ``spec`` +parameter of :meth:`pygmt.Figure.legend`. For details on how to set up such a +file, please see the GMT documentation at :gmt-docs:`legend.html#legend-codes`. +In this example the same plot is created twice, first with a vertical (default) +and then with a horizontal legend. """ # %% @@ -17,7 +21,9 @@ fig = pygmt.Figure() -fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=True) +# ----------------------------------------------------------------------------- +# Left: Vertical legend (one column, default) +fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=["WSne", "af"]) fig.plot( data="@Table_5_11.txt", @@ -31,4 +37,25 @@ fig.legend(position="JTR+jTR+o0.2c", box=True) +fig.shift_origin(xshift="+w1c") + +# ----------------------------------------------------------------------------- +# Right: Horizontal legend (here three columns) +fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=["wSne", "af"]) + +fig.plot( + data="@Table_5_11.txt", + style="c0.40c", + fill="lightgreen", + pen="faint", + # +N sets the number of columns corresponding to the given number + label="Apples+N3", +) +fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines") +fig.plot(data="@Table_5_11.txt", style="t0.40c", fill="orange", label="Oranges") + +# For multi-column legends users have to provide the width via +w, here it is +# set to 6.5 centimeters +fig.legend(position="JTR+jTR+o0.2c+w6.5c", box=True) + fig.show() From a5dd75fb2e74197c69426853cad556d614a7b226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Mon, 23 Oct 2023 15:35:45 +0200 Subject: [PATCH 02/12] Use sine and cosine curves as data --- examples/gallery/embellishments/legend.py | 52 +++++++++++------------ 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index a0ba61ba13b..f724cbe106b 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -17,45 +17,45 @@ """ # %% +import numpy as np import pygmt +# Set up some test data +x = np.arange(-10, 10, 0.1) +y1 = np.sin(x) +y2 = np.cos(x) + + +# Create new Figure() object fig = pygmt.Figure() # ----------------------------------------------------------------------------- # Left: Vertical legend (one column, default) -fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=["WSne", "af"]) - -fig.plot( - data="@Table_5_11.txt", - style="c0.40c", - fill="lightgreen", - pen="faint", - label="Apples", -) -fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines") -fig.plot(data="@Table_5_11.txt", style="t0.40c", fill="orange", label="Oranges") +fig.basemap(projection="X10c", region=[-10, 10, -1.5, 1.5], frame=True) + +# Use the label parameter to state to text label for the legend entery +fig.plot(x=x, y=y1, pen="1p,green3", label="Sine") +fig.plot(x=x, y=y2, style="c0.1c", fill="dodgerblue", label="Cosine") + +# Add a legend to the plot; place it within the plot bounding box at position +# ("J") TopRight with a anchor point ("j") TopRight and an offset of 0.2 +# centimeters in x and y directions; sourond the legend with a box fig.legend(position="JTR+jTR+o0.2c", box=True) fig.shift_origin(xshift="+w1c") # ----------------------------------------------------------------------------- -# Right: Horizontal legend (here three columns) -fig.basemap(projection="x2c", region=[0, 7, 3, 7], frame=["wSne", "af"]) - -fig.plot( - data="@Table_5_11.txt", - style="c0.40c", - fill="lightgreen", - pen="faint", - # +N sets the number of columns corresponding to the given number - label="Apples+N3", -) -fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label="My lines") -fig.plot(data="@Table_5_11.txt", style="t0.40c", fill="orange", label="Oranges") +# Right: Horizontal legend (here two columns) +fig.basemap(projection="X10c", region=[-10, 10, -1.5, 1.5], frame=["wStr", "af"]) + +# +N sets the number of columns corresponding to the given number, here two +fig.plot(x=x, y=y1, pen="1p,green3", label="Sine+N2") + +fig.plot(x=x, y=y2, style="c0.1c", fill="dodgerblue", label="Cosine") # For multi-column legends users have to provide the width via +w, here it is -# set to 6.5 centimeters -fig.legend(position="JTR+jTR+o0.2c+w6.5c", box=True) +# set to 4.5 centimeters +fig.legend(position="JTR+jTR+o0.2c+w4.5c", box=True) fig.show() From 13c3c7b86129512eaede10763387f94746105dbb Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Mon, 23 Oct 2023 20:59:39 +0200 Subject: [PATCH 03/12] Fix typos --- examples/gallery/embellishments/legend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index f724cbe106b..e25b23145dd 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -33,14 +33,14 @@ # Left: Vertical legend (one column, default) fig.basemap(projection="X10c", region=[-10, 10, -1.5, 1.5], frame=True) -# Use the label parameter to state to text label for the legend entery +# Use the label parameter to state to text label for the legend entry fig.plot(x=x, y=y1, pen="1p,green3", label="Sine") fig.plot(x=x, y=y2, style="c0.1c", fill="dodgerblue", label="Cosine") # Add a legend to the plot; place it within the plot bounding box at position # ("J") TopRight with a anchor point ("j") TopRight and an offset of 0.2 -# centimeters in x and y directions; sourond the legend with a box +# centimeters in x and y directions; surround the legend with a box fig.legend(position="JTR+jTR+o0.2c", box=True) fig.shift_origin(xshift="+w1c") From 1b860a0354104ee1c6fe36c4715f45d25df165f0 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Tue, 24 Oct 2023 06:32:26 +0200 Subject: [PATCH 04/12] Combinde to one figure --- examples/gallery/embellishments/legend.py | 38 +++++++++++++---------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index e25b23145dd..8ab075e8fc3 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -12,8 +12,6 @@ instructions for the layout of the legend items and pass it to the ``spec`` parameter of :meth:`pygmt.Figure.legend`. For details on how to set up such a file, please see the GMT documentation at :gmt-docs:`legend.html#legend-codes`. -In this example the same plot is created twice, first with a vertical (default) -and then with a horizontal legend. """ # %% @@ -21,41 +19,47 @@ import pygmt # Set up some test data -x = np.arange(-10, 10, 0.1) -y1 = np.sin(x) -y2 = np.cos(x) +x = np.arange(-10, 10, 0.2) +y1 = np.sin(x) + 1.1 +y2 = np.cos(x) + 1.1 +y3 = np.sin(x/2) - 1.1 +y4 = np.cos(x/2) - 1.1 # Create new Figure() object fig = pygmt.Figure() +fig.basemap( + projection="X10c/7c", + region=[-10, 10, -3.5, 3.5], + frame=["WSne", "xaf+lx", "ya1f0.5+ly"], +) + # ----------------------------------------------------------------------------- -# Left: Vertical legend (one column, default) -fig.basemap(projection="X10c", region=[-10, 10, -1.5, 1.5], frame=True) +# Top: Vertical legend (one column, default) # Use the label parameter to state to text label for the legend entry -fig.plot(x=x, y=y1, pen="1p,green3", label="Sine") +fig.plot(x=x, y=y1, pen="1p,green3", label="Sine(x)") -fig.plot(x=x, y=y2, style="c0.1c", fill="dodgerblue", label="Cosine") +fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="Cosine(x)") # Add a legend to the plot; place it within the plot bounding box at position # ("J") TopRight with a anchor point ("j") TopRight and an offset of 0.2 # centimeters in x and y directions; surround the legend with a box fig.legend(position="JTR+jTR+o0.2c", box=True) -fig.shift_origin(xshift="+w1c") # ----------------------------------------------------------------------------- -# Right: Horizontal legend (here two columns) -fig.basemap(projection="X10c", region=[-10, 10, -1.5, 1.5], frame=["wStr", "af"]) +# Bottom: Horizontal legend (here two columns) # +N sets the number of columns corresponding to the given number, here two -fig.plot(x=x, y=y1, pen="1p,green3", label="Sine+N2") +fig.plot(x=x, y=y3, pen="1p,darkred,-", label="Sine(x/2)+N2") + +fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="Cosine(x/2)") -fig.plot(x=x, y=y2, style="c0.1c", fill="dodgerblue", label="Cosine") +# For multi-column legends, users have to provide the width via +w, here it is +# set to 5 centimeters; position and anchor point are set to BottoRight +fig.legend(position="JBR+jBR+o0.2c+w5c", box=True) -# For multi-column legends users have to provide the width via +w, here it is -# set to 4.5 centimeters -fig.legend(position="JTR+jTR+o0.2c+w4.5c", box=True) fig.show() From d9134856bdf9b3d42ca9bd4e473ec70c27da1f6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Tue, 24 Oct 2023 06:35:52 +0200 Subject: [PATCH 05/12] Add white space within calculations --- examples/gallery/embellishments/legend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index 8ab075e8fc3..e5853559446 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -22,8 +22,8 @@ x = np.arange(-10, 10, 0.2) y1 = np.sin(x) + 1.1 y2 = np.cos(x) + 1.1 -y3 = np.sin(x/2) - 1.1 -y4 = np.cos(x/2) - 1.1 +y3 = np.sin(x / 2) - 1.1 +y4 = np.cos(x / 2) - 1.1 # Create new Figure() object From f43ad0f97be20be5d6b4b6887176f8e599c6a920 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Tue, 24 Oct 2023 07:11:15 +0200 Subject: [PATCH 06/12] Improve comment, fix typo --- examples/gallery/embellishments/legend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index e5853559446..fd8f6c47122 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -57,8 +57,8 @@ fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="Cosine(x/2)") -# For multi-column legends, users have to provide the width via +w, here it is -# set to 5 centimeters; position and anchor point are set to BottoRight +# For a multi-column legend, users have to provide the width via +w, here it +# is set to 5 centimeters; position and anchor point are set to BottomRight fig.legend(position="JBR+jBR+o0.2c+w5c", box=True) From 64929aa1dab2e5c0dbd4d11f05703f58755710f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Tue, 24 Oct 2023 10:10:45 +0200 Subject: [PATCH 07/12] Improve formulations --- examples/gallery/embellishments/legend.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index fd8f6c47122..5cad29539c8 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -43,22 +43,22 @@ fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="Cosine(x)") -# Add a legend to the plot; place it within the plot bounding box at position -# ("J") TopRight with a anchor point ("j") TopRight and an offset of 0.2 -# centimeters in x and y directions; surround the legend with a box +# Add a legend to the plot; place it within the plot bounding box with both +# reference ("J") and anchor ("+j") points being TopRight and with an offset +# of 0.2 centimeters in x and y directions; surround the legend with a box fig.legend(position="JTR+jTR+o0.2c", box=True) # ----------------------------------------------------------------------------- # Bottom: Horizontal legend (here two columns) -# +N sets the number of columns corresponding to the given number, here two +# +N sets the number of columns corresponding to the given number, here 2 fig.plot(x=x, y=y3, pen="1p,darkred,-", label="Sine(x/2)+N2") fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="Cosine(x/2)") -# For a multi-column legend, users have to provide the width via +w, here it -# is set to 5 centimeters; position and anchor point are set to BottomRight +# For a multi-column legend, users have to provide the width via "+w", here it +# is set to 5 centimeters; reference and anchor points are set to BottomRight fig.legend(position="JBR+jBR+o0.2c+w5c", box=True) From 56144f1179c38e557c91635125f8a759e2c04376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Tue, 24 Oct 2023 10:49:36 +0200 Subject: [PATCH 08/12] Consider exclusive upper limit of np.arange --- examples/gallery/embellishments/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index 5cad29539c8..51774064c5b 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -19,7 +19,7 @@ import pygmt # Set up some test data -x = np.arange(-10, 10, 0.2) +x = np.arange(-10, 10.2, 0.2) y1 = np.sin(x) + 1.1 y2 = np.cos(x) + 1.1 y3 = np.sin(x / 2) - 1.1 From 11a2e658896f69cb00f982472d6110f277f0873b Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Tue, 24 Oct 2023 12:36:49 +0200 Subject: [PATCH 09/12] State complete sine and cosine calculation in legend --- examples/gallery/embellishments/legend.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index 51774064c5b..bbd3c074ea3 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -39,9 +39,9 @@ # Top: Vertical legend (one column, default) # Use the label parameter to state to text label for the legend entry -fig.plot(x=x, y=y1, pen="1p,green3", label="Sine(x)") +fig.plot(x=x, y=y1, pen="1p,green3", label="Sine(x)+1.1") -fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="Cosine(x)") +fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="Cosine(x)+1.1") # Add a legend to the plot; place it within the plot bounding box with both # reference ("J") and anchor ("+j") points being TopRight and with an offset @@ -53,13 +53,13 @@ # Bottom: Horizontal legend (here two columns) # +N sets the number of columns corresponding to the given number, here 2 -fig.plot(x=x, y=y3, pen="1p,darkred,-", label="Sine(x/2)+N2") +fig.plot(x=x, y=y3, pen="1p,darkred,-", label="Sine(x/2)-1.1+N2") -fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="Cosine(x/2)") +fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="Cosine(x/2)-1.1") # For a multi-column legend, users have to provide the width via "+w", here it -# is set to 5 centimeters; reference and anchor points are set to BottomRight -fig.legend(position="JBR+jBR+o0.2c+w5c", box=True) +# is set to 6 centimeters; reference and anchor points are set to BottomRight +fig.legend(position="JBR+jBR+o0.2c+w6c", box=True) fig.show() From 5e5db26a3bb67675f9b5c69a7b98e989eff8d697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= <94163266+yvonnefroehlich@users.noreply.github.com> Date: Wed, 25 Oct 2023 12:28:27 +0200 Subject: [PATCH 10/12] Fix typo Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com> --- examples/gallery/embellishments/legend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index bbd3c074ea3..067f9a4d241 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -38,7 +38,7 @@ # ----------------------------------------------------------------------------- # Top: Vertical legend (one column, default) -# Use the label parameter to state to text label for the legend entry +# Use the label parameter to state the text label for the legend entry fig.plot(x=x, y=y1, pen="1p,green3", label="Sine(x)+1.1") fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="Cosine(x)+1.1") From 5986cc4c6e3391fc900d99875985891206d4fc11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= <94163266+yvonnefroehlich@users.noreply.github.com> Date: Wed, 25 Oct 2023 17:34:16 +0200 Subject: [PATCH 11/12] Use short forms 'sin' and 'cos' for legend text Co-authored-by: Dongdong Tian --- examples/gallery/embellishments/legend.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index 067f9a4d241..5e8633ac38b 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -39,9 +39,9 @@ # Top: Vertical legend (one column, default) # Use the label parameter to state the text label for the legend entry -fig.plot(x=x, y=y1, pen="1p,green3", label="Sine(x)+1.1") +fig.plot(x=x, y=y1, pen="1p,green3", label="sin(x)+1.1") -fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="Cosine(x)+1.1") +fig.plot(x=x, y=y2, style="c0.07c", fill="dodgerblue", label="cos(x)+1.1") # Add a legend to the plot; place it within the plot bounding box with both # reference ("J") and anchor ("+j") points being TopRight and with an offset @@ -53,9 +53,9 @@ # Bottom: Horizontal legend (here two columns) # +N sets the number of columns corresponding to the given number, here 2 -fig.plot(x=x, y=y3, pen="1p,darkred,-", label="Sine(x/2)-1.1+N2") +fig.plot(x=x, y=y3, pen="1p,darkred,-", label="sin(x/2)-1.1+N2") -fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="Cosine(x/2)-1.1") +fig.plot(x=x, y=y4, style="s0.07c", fill="orange", label="cos(x/2)-1.1") # For a multi-column legend, users have to provide the width via "+w", here it # is set to 6 centimeters; reference and anchor points are set to BottomRight From 75598d07ee7c22c4fd2653578055f4bbfdbdec8e Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Wed, 25 Oct 2023 17:42:28 +0200 Subject: [PATCH 12/12] Remove blank lines --- examples/gallery/embellishments/legend.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/examples/gallery/embellishments/legend.py b/examples/gallery/embellishments/legend.py index 5e8633ac38b..9da1f95e73f 100644 --- a/examples/gallery/embellishments/legend.py +++ b/examples/gallery/embellishments/legend.py @@ -25,7 +25,6 @@ y3 = np.sin(x / 2) - 1.1 y4 = np.cos(x / 2) - 1.1 - # Create new Figure() object fig = pygmt.Figure() @@ -48,7 +47,6 @@ # of 0.2 centimeters in x and y directions; surround the legend with a box fig.legend(position="JTR+jTR+o0.2c", box=True) - # ----------------------------------------------------------------------------- # Bottom: Horizontal legend (here two columns) @@ -61,5 +59,4 @@ # is set to 6 centimeters; reference and anchor points are set to BottomRight fig.legend(position="JBR+jBR+o0.2c+w6c", box=True) - fig.show()