From 1f0c2d28417ae0bfda85ba4cf46e066e81e53352 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Fri, 20 Oct 2023 10:16:51 +0200 Subject: [PATCH 01/28] Update and Expand text tutorial --- examples/tutorials/basics/text.py | 170 ++++++++++++++++++------------ 1 file changed, 100 insertions(+), 70 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 54a88eebcdb..f6b18a03a21 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -2,8 +2,8 @@ Plotting text ============= -It is often useful to add annotations to a plot. This is handled by -:meth:`pygmt.Figure.text`. +It is often useful to add annotations to a plot. This is handled by the +:meth:`pygmt.Figure.text` method of the :class:`pygmt.Figure` class. """ # %% @@ -15,12 +15,9 @@ # Basic map annotation # -------------------- # -# Text annotations can be added to a map using the :meth:`pygmt.Figure.text` -# method of the :class:`pygmt.Figure` class. -# -# Here we create a simple map and add an annotation using the ``text``, ``x``, -# and ``y`` parameters to specify the annotation text and position in the -# projection frame. ``text`` accepts *str* types, while ``x`` and ``y`` +# Here we create a simple map and add annotation using the ``text``, ``x``, +# and ``y`` parameters to specify the annotation text and position within +# the projection frame. ``text`` accepts *str* types, while ``x`` and ``y`` # accept either *int* or *float* numbers, or a list/array of numbers. fig = pygmt.Figure() @@ -28,7 +25,7 @@ fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") fig.coast(land="black", water="skyblue") -# Plot text annotations using single arguments +# Plot text annotation using single arguments fig.text(text="SOUTH CHINA SEA", x=112, y=6) # Plot text annotations using lists of arguments @@ -38,8 +35,37 @@ # %% -# Changing font style -# ------------------- +# The ``position`` and ``justify`` parameters +# ------------------------------------------- +# +# Instead of using ``x`` and ``y``, the ``position`` parameter can be +# specified to set the reference point for the text on the plot. In both +# cases, ``justify`` is used to define the anchor point for the bounding +# box for the text. +# +# Both ``position`` and ``justify`` are specified by a two-letter (order +# independent) code, chosen from: +# +# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) +# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) + +fig = pygmt.Figure() +fig.basemap(region=[-1, 1, -1, 1], projection="X10c", frame="af0.5g") + +for position in ("TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"): + fig.text( + text=position, + position=position, + font="28p,Helvetica-Bold,black", + justify=position, + ) + +fig.show() + + +# %% +# The ``font`` parameter +# ---------------------- # # The size, family/weight, and color of an annotation can be specified using # the ``font`` parameter. @@ -49,20 +75,76 @@ # including details of how to use non-default fonts. fig = pygmt.Figure() -with pygmt.config(MAP_FRAME_TYPE="plain"): - fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") -fig.coast(land="black", water="skyblue") +fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rltb") # Customize the font style -fig.text(text="BORNEO", x=114.0, y=0.5, font="22p,Helvetica-Bold,white") +fig.text( + x=0, + y=0, + text="blue text in Helvetica-Bold with size 6p", + font="6p,Helvetica-Bold,blue", +) + +fig.show() + + +# %% +# The ``angle`` parameter +# ----------------------- +# +# ``angle`` is an optional parameter used to specify the counter-clockwise +# rotation in degrees of the text from the horizontal. + +fig = pygmt.Figure() +fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rltb") + +for i in range(0, 360, 30): + fig.text(text=f"` {i}@.", x=0, y=0, justify="LM", angle=i) fig.show() # %% -# Plotting from a text file -# ------------------------- +# The ``fill`` and ``pen`` parameters +# ----------------------------------- # +# ``fill`` is used to set the fill color of the area surrounding the text. +# Add an outline to the text box via the ``pen`` parameter. + +fig = pygmt.Figure() +fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rltb") + +fig.text(text="Green", x=0, y=0, fill="green", pen="0.5p,darkgreen") + +fig.show() + + +# %% +# Plotting text with different configurations +# ------------------------------------------- +# +# To add text with different ``font``, ``justify``, and ``angle`` on can +# provide lists with the specific arguments. + +fig = pygmt.Figure() +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) + +fig.text( + x=[0, 0, 0], + y=[3, 2, -2], + angle=[0, 0, 30], + font=["5p,Helvetica,black", "5p,Helvetica,blue", "6p,Courier-Bold,red"], + justify=["CM", "LT", "CM"], + text=[ + "black text with justify='CM'", + "blue text with justify='LT'", + "red text with angle=30", + ], +) + +fig.show() + +# %% # It is also possible to add annotations from a file containing ``x``, ``y``, # and ``text`` columns. Here we give a complete example. @@ -93,58 +175,6 @@ fig.show() -# %% -# ``justify`` parameter -# --------------------- -# -# ``justify`` is used to define the anchor point for the bounding box for text -# being added to a plot. The following code segment demonstrates the -# positioning of the anchor point relative to the text. -# -# The anchor point is specified with a two-letter (order independent) code, -# chosen from: -# -# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) -# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) - -fig = pygmt.Figure() -fig.basemap(region=[0, 3, 0, 3], projection="X10c", frame=["WSne", "af0.5g"]) -for position in ("TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"): - fig.text( - text=position, - position=position, - font="28p,Helvetica-Bold,black", - justify=position, - ) -fig.show() - - -# %% -# ``angle`` parameter -# ------------------- -# -# ``angle`` is an optional parameter used to specify the counter-clockwise -# rotation in degrees of the text from the horizontal. - -fig = pygmt.Figure() -fig.basemap(region=[0, 4, 0, 4], projection="X5c", frame="WSen") -for i in range(0, 360, 30): - fig.text(text=f"` {i}@.", x=2, y=2, justify="LM", angle=i) -fig.show() - - -# %% -# ``fill`` parameter -# ------------------ -# -# ``fill`` is used to set the fill color of the area surrounding the text. - -fig = pygmt.Figure() -fig.basemap(region=[0, 1, 0, 1], projection="X5c", frame="WSen") -fig.text(text="Green", x=0.5, y=0.5, fill="green") -fig.show() - - # %% # Advanced configuration # ---------------------- @@ -154,4 +184,4 @@ # at :gmt-docs:`text.html` and also the cookbook at # :gmt-docs:`cookbook/features.html#placement-of-text`. Good luck! -# sphinx_gallery_thumbnail_number = 3 +# sphinx_gallery_thumbnail_number = 7 From 64996b44c7e59208a2128fdc40cf71db39dd7be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Sun, 29 Oct 2023 17:48:50 +0100 Subject: [PATCH 02/28] Add darft of new structure --- examples/tutorials/basics/text.py | 234 ++++++++++++++++-------------- 1 file changed, 129 insertions(+), 105 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index f6b18a03a21..bdad7769356 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -6,125 +6,86 @@ :meth:`pygmt.Figure.text` method of the :class:`pygmt.Figure` class. """ -# %% import os +import numpy as np import pygmt # %% -# Basic map annotation -# -------------------- -# -# Here we create a simple map and add annotation using the ``text``, ``x``, -# and ``y`` parameters to specify the annotation text and position within -# the projection frame. ``text`` accepts *str* types, while ``x`` and ``y`` -# accept either *int* or *float* numbers, or a list/array of numbers. - -fig = pygmt.Figure() -with pygmt.config(MAP_FRAME_TYPE="plain"): - fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") -fig.coast(land="black", water="skyblue") - -# Plot text annotation using single arguments -fig.text(text="SOUTH CHINA SEA", x=112, y=6) - -# Plot text annotations using lists of arguments -fig.text(text=["CELEBES SEA", "JAVA SEA"], x=[119, 112], y=[3.25, -4.6]) - -fig.show() - - -# %% -# The ``position`` and ``justify`` parameters -# ------------------------------------------- -# -# Instead of using ``x`` and ``y``, the ``position`` parameter can be -# specified to set the reference point for the text on the plot. In both -# cases, ``justify`` is used to define the anchor point for the bounding -# box for the text. -# -# Both ``position`` and ``justify`` are specified by a two-letter (order -# independent) code, chosen from: +# Add a single text label +# ----------------------- # -# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) -# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) +# To add a single text label to a plot or map the ``text``, ``x``, and ``y`` +# parameters to specify the text and position within the plot frame. fig = pygmt.Figure() -fig.basemap(region=[-1, 1, -1, 1], projection="X10c", frame="af0.5g") +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="af") -for position in ("TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"): - fig.text( - text=position, - position=position, - font="28p,Helvetica-Bold,black", - justify=position, - ) +fig.text(text="My text", x=0, y=0) fig.show() # %% -# The ``font`` parameter -# ---------------------- +# Adjust text labels +# ------------------ # # The size, family/weight, and color of an annotation can be specified using -# the ``font`` parameter. -# -# A list of all recognized fonts can be found at +# the ``font`` parameter. A list of all recognized fonts can be found at # :gmt-docs:`PostScript Fonts Used by GMT `, # including details of how to use non-default fonts. - -fig = pygmt.Figure() -fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rltb") - -# Customize the font style -fig.text( - x=0, - y=0, - text="blue text in Helvetica-Bold with size 6p", - font="6p,Helvetica-Bold,blue", -) - -fig.show() - - -# %% -# The ``angle`` parameter -# ----------------------- +# The ``angle`` parameter is used to specify the counter-clockwise rotation in +# degrees of the text from the horizontal. +# The ``justify`` parameter is used to define the anchor point for the bounding +# box for the text. it is specified by a two-letter (order independent) code, +# chosen from: # -# ``angle`` is an optional parameter used to specify the counter-clockwise -# rotation in degrees of the text from the horizontal. - -fig = pygmt.Figure() -fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rltb") - -for i in range(0, 360, 30): - fig.text(text=f"` {i}@.", x=0, y=0, justify="LM", angle=i) - -fig.show() - - -# %% -# The ``fill`` and ``pen`` parameters -# ----------------------------------- +# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) +# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # -# ``fill`` is used to set the fill color of the area surrounding the text. -# Add an outline to the text box via the ``pen`` parameter. +# The ``offset`` parameter can be used to shift the text label relative to the +# reference point. This can be used full when adding labels to data points. +# +# The ``fill`` parameter is used to set the fill color of the area surrounding +# the text. Add an outline to the text box via the ``pen`` parameter. The add +# a margin between the text and the box as well as to force rounded corner the +# ``clearance`` parameter can be used. fig = pygmt.Figure() -fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rltb") - -fig.text(text="Green", x=0, y=0, fill="green", pen="0.5p,darkgreen") +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["WSte", "af"]) + +# Change font size, family/weight, color +fig.text(x=0, y=2, text="My Text", font="12p,Helvetica-Bold,blue") +# Rotate text by 30 degrees counter-clockwise from the horizontal +fig.text(x=0, y=0, text="My Text", angle=30) +# Set the anchor point to TopLeft +fig.text(x=0, y=-2, text="My Text", justify="TL") +# Offset text by 0.2 centimeters in x and y-directions +fig.text(x=0, y=-4, text="My Text", offset="1c/-0.2c") + +# ----------------------------------------------------------------------------- +fig.shift_origin(xshift="+w0.5c") + +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["wStr", "af"]) + +# Add box with green fill +fig.text(x=0, y=2, text="My text", fill="green") +# Add box with an darkgreen, 0.5 points thick outline +fig.text(x=0, y=0, text="My text", pen="0.5p,darkgreen") +# Add box with a margin in x and y directions +fig.text(x=0, y=-2, text="My text", pen="0.5p,darkgreen", clearance="0.2c/0.2c") +# Add outline with roundet corners +fig.text(x=0, y=-4, text="My text", pen="0.5p,darkgreen", clearance="+tO") fig.show() # %% -# Plotting text with different configurations -# ------------------------------------------- +# Plotting text with individual configurations +# -------------------------------------------- # -# To add text with different ``font``, ``justify``, and ``angle`` on can -# provide lists with the specific arguments. +# To add text with individual ``font``, ``angle``, and ``justify`` one can +# provide lists with the corresponding arguments. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) @@ -132,8 +93,8 @@ fig.text( x=[0, 0, 0], y=[3, 2, -2], - angle=[0, 0, 30], font=["5p,Helvetica,black", "5p,Helvetica,blue", "6p,Courier-Bold,red"], + angle=[0, 0, 30], justify=["CM", "LT", "CM"], text=[ "black text with justify='CM'", @@ -144,28 +105,33 @@ fig.show() + # %% -# It is also possible to add annotations from a file containing ``x``, ``y``, -# and ``text`` columns. Here we give a complete example. +# Use an external txt file +# ------------------------ +# +# It is also possible to add text labels via an external text file containing +# ``x``, ``y``, and ``text`` columns. Addionaly, columns to set the ``angle``, +# ``front``, and ``justify`` parameters can be provided. Here we give a +# complete example. fig = pygmt.Figure() -with pygmt.config(MAP_FRAME_TYPE="plain"): - fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") -fig.coast(land="black", water="skyblue") +fig.basemap(region=[108, 121, -5, 8], projection="M10c", frame="a2f1") +fig.coast(land="darkgray", water="steelblue", shorelines="1/0.1p,gray30") # Create space-delimited file with open("examples.txt", "w") as f: - f.write("114 0.5 0 22p,Helvetica-Bold,white CM BORNEO\n") - f.write("119 3.25 0 12p,Helvetica-Bold,black CM CELEBES SEA\n") - f.write("112 -4.6 0 12p,Helvetica-Bold,black CM JAVA SEA\n") - f.write("112 6 40 12p,Helvetica-Bold,black CM SOUTH CHINA SEA\n") - f.write("119.12 7.25 -40 12p,Helvetica-Bold,black CM SULU SEA\n") - f.write("118.4 -1 65 12p,Helvetica-Bold,black CM MAKASSAR STRAIT\n") + f.write("114.00 0.50 0 15p,Helvetica-Bold,white CM BORNEO\n") + f.write("119.00 3.25 0 8p,Helvetica-Bold,black CM CELEBES SEA\n") + f.write("112.00 -4.60 0 8p,Helvetica-Bold,black CM JAVA SEA\n") + f.write("112.00 6.00 40 8p,Helvetica-Bold,black CM SOUTH CHINA SEA\n") + f.write("119.12 7.25 -40 8p,Helvetica-Bold,black CM SULU SEA\n") + f.write("118.40 -1.00 65 8p,Helvetica-Bold,black CM MAKASSAR STRAIT\n") # Plot region names / sea names from a text file, where # the longitude (x) and latitude (y) coordinates are in the first two columns. # Setting angle/font/justify to True will indicate that those columns are -# present in the text file too (Note: must be in that order!). +# present in the text file too (Please note: must be in that order). # Finally, the text to be printed will be in the last column fig.text(textfiles="examples.txt", angle=True, font=True, justify=True) @@ -175,6 +141,64 @@ fig.show() +# %% +# The position parameter +# ---------------------- +# +# Instead of using ``x`` and ``y``, the ``position`` parameter can be +# specified to set the reference point for the text on the plot. +# As for the ``justify`` parameter, the ``position`` parameter is specified +# by a two-letter (order independent) code, chosen from: +# +# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) +# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) +# +# This can be helpful to add text labels to subplot or text labels out of +# the map frame, e.g., for depth slices. + +fig = pygmt.Figure() +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["WStr", "af"]) + +fig.text( + text="(a)", + position="TL", + offset="0.1c/-0.1c", + justify="TL", +) + +# ----------------------------------------------------------------------------- +fig.shift_origin(xshift="+w1c") + +# Define region limits +lon_min = -30 +lon_max = 30 +lat_min = 10 +lat_max = 60 +# Determine projection center +lon0 = np.mean([lon_min, lon_max]) +lat0 = np.mean([lat_min, lat_max]) +# Determine the two standard parallels (only these two distortion-free) +lat1 = (lat_min + lat_max) / 3 +lat2 = (lat_min + lat_max) / 3 * 2 +# Set up arguments for the region and projection parameters +region_use = [lon_min, lon_max, lat_min, lat_max] +projection_use = ( + "L" + str(lon0) + "/" + str(lat0) + "/" + str(lat1) + "/" + str(lat2) + "/5c" +) + +fig.basemap(region=region_use, projection=projection_use, frame=["lStE", "af"]) + +fig.text( + text="@@100 km", + position="TC", + justify="MC", + offset="0c/0.2c", + no_clip=True, +) + +fig.show() + + # %% # Advanced configuration # ---------------------- @@ -184,4 +208,4 @@ # at :gmt-docs:`text.html` and also the cookbook at # :gmt-docs:`cookbook/features.html#placement-of-text`. Good luck! -# sphinx_gallery_thumbnail_number = 7 +# sphinx_gallery_thumbnail_number = 4 From 441d861739e8465c6bc2834e77d608e6bb25bead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Sun, 29 Oct 2023 20:09:53 +0100 Subject: [PATCH 03/28] Use bullet points to state paramters for adjusting text label --- examples/tutorials/basics/text.py | 42 +++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index bdad7769356..3e56578d0ac 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -30,26 +30,26 @@ # Adjust text labels # ------------------ # -# The size, family/weight, and color of an annotation can be specified using -# the ``font`` parameter. A list of all recognized fonts can be found at -# :gmt-docs:`PostScript Fonts Used by GMT `, -# including details of how to use non-default fonts. -# The ``angle`` parameter is used to specify the counter-clockwise rotation in -# degrees of the text from the horizontal. -# The ``justify`` parameter is used to define the anchor point for the bounding -# box for the text. it is specified by a two-letter (order independent) code, -# chosen from: +# There are several optional parameters to adjust a text label: # -# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) -# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) +# * ``font``: Sets the size, family/weight, and color of the text. +# A list of all recognized fonts can be found at +# :gmt-docs:`PostScript Fonts Used by GMT `, +# including details of how to use non-default fonts. +# * ``angle``: Specifies the counter-clockwise rotation from the horizontal in +# degrees of the text. +# * ``justify``: Defines the anchor point for the bounding box for the text. +# It is specified by a two-letter (order independent) code, chosen from: # -# The ``offset`` parameter can be used to shift the text label relative to the -# reference point. This can be used full when adding labels to data points. +# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) +# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # -# The ``fill`` parameter is used to set the fill color of the area surrounding -# the text. Add an outline to the text box via the ``pen`` parameter. The add -# a margin between the text and the box as well as to force rounded corner the -# ``clearance`` parameter can be used. +# * ``offset``: Shifts the text label relative to the reference point. +# This can be usedful when adding labels to data points. +# * ``fill``: Sets the fill color of the text box. +# * ``pen``: Adds an outline to the text box. Give thickness,color,style. +# * ``clearance``: Adds a margin between the text and the box. Can be used to +# force a text box with rounded corners. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["WSte", "af"]) @@ -70,12 +70,12 @@ # Add box with green fill fig.text(x=0, y=2, text="My text", fill="green") -# Add box with an darkgreen, 0.5 points thick outline -fig.text(x=0, y=0, text="My text", pen="0.5p,darkgreen") +# Add box with an seagreen, 0.5 points thick, solid outline +fig.text(x=0, y=0, text="My text", pen="0.5p,seagreen,solid") # Add box with a margin in x and y directions -fig.text(x=0, y=-2, text="My text", pen="0.5p,darkgreen", clearance="0.2c/0.2c") +fig.text(x=0, y=-2, text="My text", pen="0.5p,seagreen,solid", clearance="0.2c/0.2c") # Add outline with roundet corners -fig.text(x=0, y=-4, text="My text", pen="0.5p,darkgreen", clearance="+tO") +fig.text(x=0, y=-4, text="My text", pen="0.5p,seagreen,solid", clearance="+tO") fig.show() From fcdd028c6607b82c89daaa436d467b93bfec62c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Sun, 29 Oct 2023 20:34:31 +0100 Subject: [PATCH 04/28] Fix typo --- examples/tutorials/basics/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 3e56578d0ac..6c8b7a229c9 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -45,7 +45,7 @@ # * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # # * ``offset``: Shifts the text label relative to the reference point. -# This can be usedful when adding labels to data points. +# This can be useful when adding a label to a data point. # * ``fill``: Sets the fill color of the text box. # * ``pen``: Adds an outline to the text box. Give thickness,color,style. # * ``clearance``: Adds a margin between the text and the box. Can be used to From 4b792a7cd8bb6573ae9087ecebe6cd913602002c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Wed, 1 Nov 2023 12:30:22 +0100 Subject: [PATCH 05/28] Update code example for optional parameters to adjust text --- examples/tutorials/basics/text.py | 70 ++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 6c8b7a229c9..0465ed160a2 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -19,7 +19,7 @@ # parameters to specify the text and position within the plot frame. fig = pygmt.Figure() -fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="af") +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) fig.text(text="My text", x=0, y=0) @@ -32,50 +32,72 @@ # # There are several optional parameters to adjust a text label: # -# * ``font``: Sets the size, family/weight, and color of the text. -# A list of all recognized fonts can be found at +# * ``font``: Sets size, family/weight, and color of the font for the text. # :gmt-docs:`PostScript Fonts Used by GMT `, # including details of how to use non-default fonts. -# * ``angle``: Specifies the counter-clockwise rotation from the horizontal in -# degrees of the text. -# * ``justify``: Defines the anchor point for the bounding box for the text. +# * ``angle``: Specifies the rotation of the text. It is measured counter- +# clockwise from the horizontal in degrees. +# * ``justify``: Defines the anchor point of the bounding box for the text. # It is specified by a two-letter (order independent) code, chosen from: # # * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) # * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # -# * ``offset``: Shifts the text label relative to the reference point. +# * ``offset``: Shifts the text relatively to the reference point. # This can be useful when adding a label to a data point. -# * ``fill``: Sets the fill color of the text box. -# * ``pen``: Adds an outline to the text box. Give thickness,color,style. -# * ``clearance``: Adds a margin between the text and the box. Can be used to +# * ``fill``: Adds a fill color to the text box. +# * ``pen``: Adds an outline to the text box. +# * ``clearance``: Defines the margin between text and box. Can be used to # force a text box with rounded corners. fig = pygmt.Figure() -fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["WSte", "af"]) +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) # Change font size, family/weight, color -fig.text(x=0, y=2, text="My Text", font="12p,Helvetica-Bold,blue") +fig.text(x=0, y=3, text="My text", font="12p,Helvetica-Bold,blue") # Rotate text by 30 degrees counter-clockwise from the horizontal -fig.text(x=0, y=0, text="My Text", angle=30) -# Set the anchor point to TopLeft -fig.text(x=0, y=-2, text="My Text", justify="TL") -# Offset text by 0.2 centimeters in x and y-directions -fig.text(x=0, y=-4, text="My Text", offset="1c/-0.2c") +fig.text(x=0, y=0, text="My text", angle=30) +# Shift text relatively to the x and y positions by 0.7 centimeters to the +# right (position x directions) and 0.2 centimeters down (negative y direction) +fig.text(x=0, y=-3, text="My text", offset="0.7c/-0.2c") + +# ----------------------------------------------------------------------------- +fig.shift_origin(xshift="+w0.5c") + +fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rtlb") + +fig.plot( + x=[-0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5], + y=[0.5, 0.5, 0.5, 0, 0, 0, -0.5, -0.5, -0.5], + style="s0.2c", + fill="darkred", + pen="0.7p,pink", +) + +# Plot text labels with different justifications +fig.text(x=-0.5, y=0.5, text="TL", justify="TL") # TopLeft +fig.text(x=0, y=0.5, text="TM", justify="TC") # TopCenter +fig.text(x=0.5, y=0.5, text="TR", justify="TR") # TopRight +fig.text(x=-0.5, y=0, text="ML", justify="ML") # MiddleLeft +fig.text(x=0, y=0, text="MC", justify="MC") # MiddleCenter +fig.text(x=0.5, y=0, text="MR", justify="MR") # MiddleRight +fig.text(x=-0.5, y=-0.5, text="BL", justify="BL") # BottomLeft +fig.text(x=0, y=-0.5, text="BC", justify="BC") # BottomCenter +fig.text(x=0.5, y=-0.5, text="BR", justify="BR") # BottomRight # ----------------------------------------------------------------------------- fig.shift_origin(xshift="+w0.5c") -fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["wStr", "af"]) +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="rtlb") # Add box with green fill -fig.text(x=0, y=2, text="My text", fill="green") +fig.text(x=0, y=3, text="My text", fill="green") # Add box with an seagreen, 0.5 points thick, solid outline -fig.text(x=0, y=0, text="My text", pen="0.5p,seagreen,solid") -# Add box with a margin in x and y directions -fig.text(x=0, y=-2, text="My text", pen="0.5p,seagreen,solid", clearance="0.2c/0.2c") -# Add outline with roundet corners -fig.text(x=0, y=-4, text="My text", pen="0.5p,seagreen,solid", clearance="+tO") +fig.text(x=0, y=1, text="My text", pen="0.5p,seagreen,solid") +# Add box with a margin in x and y directions of 0.2 centimeters +fig.text(x=0, y=-1, text="My text", pen="0.5p,seagreen,solid", clearance="0.2c/0.2c") +# Add outline with rounded corners +fig.text(x=0, y=-3, text="My text", pen="0.5p,seagreen,solid", clearance="+tO") fig.show() From c37aac602f59facbfe33c0a5f2da91c6a0bfb86a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Wed, 1 Nov 2023 14:05:02 +0100 Subject: [PATCH 06/28] Improve comments --- examples/tutorials/basics/text.py | 48 +++++++++++++++++++------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 0465ed160a2..5f290fce0ee 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -32,7 +32,7 @@ # # There are several optional parameters to adjust a text label: # -# * ``font``: Sets size, family/weight, and color of the font for the text. +# * ``font``: Sets the size, family/weight, and color of the font for the text. # :gmt-docs:`PostScript Fonts Used by GMT `, # including details of how to use non-default fonts. # * ``angle``: Specifies the rotation of the text. It is measured counter- @@ -44,28 +44,33 @@ # * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # # * ``offset``: Shifts the text relatively to the reference point. -# This can be useful when adding a label to a data point. -# * ``fill``: Adds a fill color to the text box. -# * ``pen``: Adds an outline to the text box. -# * ``clearance``: Defines the margin between text and box. Can be used to -# force a text box with rounded corners. +# * ``fill``: Fills the text box with a color. +# * ``pen``: Outlines the text box. +# * ``clearance``: Defines the margins in x and y directions between the text +# and the borders of the text box. Can be used to force a text box with +# rounded corners. fig = pygmt.Figure() + +# ----------------------------------------------------------------------------- +# Left: "font", "angle", and "offset" parameters fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) -# Change font size, family/weight, color +# Change the font size, family/weight, color of the text fig.text(x=0, y=3, text="My text", font="12p,Helvetica-Bold,blue") -# Rotate text by 30 degrees counter-clockwise from the horizontal +# Rotate the text by 30 degrees counter-clockwise from the horizontal fig.text(x=0, y=0, text="My text", angle=30) -# Shift text relatively to the x and y positions by 0.7 centimeters to the -# right (position x directions) and 0.2 centimeters down (negative y direction) +# Shift the text relatively to the x and y positions by 0.7 centimeters to the +# right (positive x direction) and 0.2 centimeters down (negative y direction) fig.text(x=0, y=-3, text="My text", offset="0.7c/-0.2c") -# ----------------------------------------------------------------------------- fig.shift_origin(xshift="+w0.5c") +# ----------------------------------------------------------------------------- +# Middle: "justify" parameter fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rtlb") +# Plot markers fig.plot( x=[-0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5], y=[0.5, 0.5, 0.5, 0, 0, 0, -0.5, -0.5, -0.5], @@ -74,7 +79,8 @@ pen="0.7p,pink", ) -# Plot text labels with different justifications +# Plot text labels at the same position as the markers but +# with different justifications fig.text(x=-0.5, y=0.5, text="TL", justify="TL") # TopLeft fig.text(x=0, y=0.5, text="TM", justify="TC") # TopCenter fig.text(x=0.5, y=0.5, text="TR", justify="TR") # TopRight @@ -85,9 +91,10 @@ fig.text(x=0, y=-0.5, text="BC", justify="BC") # BottomCenter fig.text(x=0.5, y=-0.5, text="BR", justify="BR") # BottomRight -# ----------------------------------------------------------------------------- fig.shift_origin(xshift="+w0.5c") +# ----------------------------------------------------------------------------- +# Right: "fill", "pen", and "clearance" parameters fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="rtlb") # Add box with green fill @@ -175,10 +182,13 @@ # * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) # * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # -# This can be helpful to add text labels to subplot or text labels out of -# the map frame, e.g., for depth slices. +# This can be helpful to add a tag to a subplot or text labels out of +# the plot or map frame, e.g., for depth slices. fig = pygmt.Figure() + +# ----------------------------------------------------------------------------- +# Left: Add a tag to a subplot fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=["WStr", "af"]) fig.text( @@ -188,9 +198,11 @@ justify="TL", ) -# ----------------------------------------------------------------------------- fig.shift_origin(xshift="+w1c") +# ----------------------------------------------------------------------------- +# Right: Add a text label out of the plot or map frame + # Define region limits lon_min = -30 lon_max = 30 @@ -211,11 +223,11 @@ fig.basemap(region=region_use, projection=projection_use, frame=["lStE", "af"]) fig.text( - text="@@100 km", + text="@@100 km", # "@@" gives "@" in GMT or PyGMT position="TC", justify="MC", offset="0c/0.2c", - no_clip=True, + no_clip=True, # Allow plotting out of map or plot frame ) fig.show() From be3996b9147a037477fc0742ea72e1055397b986 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Wed, 1 Nov 2023 19:45:28 +0100 Subject: [PATCH 07/28] Improve code example for optional parameters to adjust text label --- examples/tutorials/basics/text.py | 45 ++++++++++++++++++------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 5f290fce0ee..e40dd2e69ef 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -46,9 +46,8 @@ # * ``offset``: Shifts the text relatively to the reference point. # * ``fill``: Fills the text box with a color. # * ``pen``: Outlines the text box. -# * ``clearance``: Defines the margins in x and y directions between the text -# and the borders of the text box. Can be used to force a text box with -# rounded corners. +# * ``clearance``: Adds a margin in x and y directions between the text and the +# borders of the text box. Can be used to get a text box with rounded edges. fig = pygmt.Figure() @@ -57,12 +56,16 @@ fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) # Change the font size, family/weight, color of the text -fig.text(x=0, y=3, text="My text", font="12p,Helvetica-Bold,blue") +fig.text(x=0, y=3, text="my text", font="12p,Helvetica-Bold,blue") + # Rotate the text by 30 degrees counter-clockwise from the horizontal -fig.text(x=0, y=0, text="My text", angle=30) -# Shift the text relatively to the x and y positions by 0.7 centimeters to the -# right (positive x direction) and 0.2 centimeters down (negative y direction) -fig.text(x=0, y=-3, text="My text", offset="0.7c/-0.2c") +fig.text(x=0, y=0, text="my text", angle=30) + +# Shift the text relatively to the x and y positions by 1 centimeter to the +# right (positive x direction) and 0.5 centimeters down (negative y direction) +fig.plot(x=0, y=-3, style="s0.2c", fill="darkorange", pen="0.7p,darkgray") +fig.text(x=0, y=-3, text="my text") +fig.text(x=0, y=-3, text="my text", offset="1c/-0.5c") fig.shift_origin(xshift="+w0.5c") @@ -75,12 +78,12 @@ x=[-0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5], y=[0.5, 0.5, 0.5, 0, 0, 0, -0.5, -0.5, -0.5], style="s0.2c", - fill="darkred", - pen="0.7p,pink", + fill="darkorange", + pen="0.7p,darkgray", ) -# Plot text labels at the same position as the markers but -# with different justifications +# Plot text at the same x and y positions as the markers +# but with varying justifications fig.text(x=-0.5, y=0.5, text="TL", justify="TL") # TopLeft fig.text(x=0, y=0.5, text="TM", justify="TC") # TopCenter fig.text(x=0.5, y=0.5, text="TR", justify="TR") # TopRight @@ -97,14 +100,18 @@ # Right: "fill", "pen", and "clearance" parameters fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="rtlb") -# Add box with green fill +# Add a box with a fill in green color fig.text(x=0, y=3, text="My text", fill="green") -# Add box with an seagreen, 0.5 points thick, solid outline -fig.text(x=0, y=1, text="My text", pen="0.5p,seagreen,solid") -# Add box with a margin in x and y directions of 0.2 centimeters -fig.text(x=0, y=-1, text="My text", pen="0.5p,seagreen,solid", clearance="0.2c/0.2c") -# Add outline with rounded corners -fig.text(x=0, y=-3, text="My text", pen="0.5p,seagreen,solid", clearance="+tO") + +# Add box with a seagreen, 1 point thick, solid outline +fig.text(x=0, y=1, text="My text", pen="1p,seagreen,solid") + +# Add a margin between the text and border of the text box of 0.1 centimeters +# in x direction and 0.2 centimeters in y direction +fig.text(x=0, y=-1, text="My text", pen="1p,seagreen,dashed", clearance="0.2c/0.2c") + +# Get rounded edges by appending "+tO" to the "clearance" parameter +fig.text(x=0, y=-3, text="My text", pen="1p,seagreen,solid", clearance="0.2c/0.2c+tO") fig.show() From 6dd12cd9c4e3f2152bb20834572c23239e43b23b Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Fri, 10 Nov 2023 11:39:28 +0100 Subject: [PATCH 08/28] Improve docs --- examples/tutorials/basics/text.py | 69 ++++++++++++++++--------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index e40dd2e69ef..0f4bcd6ff0a 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -2,8 +2,8 @@ Plotting text ============= -It is often useful to add annotations to a plot. This is handled by the -:meth:`pygmt.Figure.text` method of the :class:`pygmt.Figure` class. +It is often useful to add text annotations to a plot or map. This is handled +by the :meth:`pygmt.Figure.text` method of the :class:`pygmt.Figure` class. """ import os @@ -12,25 +12,25 @@ import pygmt # %% -# Add a single text label -# ----------------------- +# Adding a single text label +# -------------------------- # -# To add a single text label to a plot or map the ``text``, ``x``, and ``y`` -# parameters to specify the text and position within the plot frame. +# To add a single text label to a plot the ``text`` and ``x`` and ``y`` +# parameters to specify the text and position. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) -fig.text(text="My text", x=0, y=0) +fig.text(x=0, y=0, text="My text") fig.show() # %% -# Adjust text labels -# ------------------ +# Adjusting the text label +# ------------------------ # -# There are several optional parameters to adjust a text label: +# There are several optional parameters to adjust the text label: # # * ``font``: Sets the size, family/weight, and color of the font for the text. # :gmt-docs:`PostScript Fonts Used by GMT `, @@ -46,8 +46,8 @@ # * ``offset``: Shifts the text relatively to the reference point. # * ``fill``: Fills the text box with a color. # * ``pen``: Outlines the text box. -# * ``clearance``: Adds a margin in x and y directions between the text and the -# borders of the text box. Can be used to get a text box with rounded edges. +# * ``clearance``: Adds margins in x and y directions between the text and the +# outline of the text box. Can be used to get a text box with rounded edges. fig = pygmt.Figure() @@ -55,14 +55,15 @@ # Left: "font", "angle", and "offset" parameters fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) -# Change the font size, family/weight, color of the text +# Change font size, family/weight, color of the text fig.text(x=0, y=3, text="my text", font="12p,Helvetica-Bold,blue") # Rotate the text by 30 degrees counter-clockwise from the horizontal fig.text(x=0, y=0, text="my text", angle=30) -# Shift the text relatively to the x and y positions by 1 centimeter to the -# right (positive x direction) and 0.5 centimeters down (negative y direction) +# Shift the text relatively to the position given via the x and y parameters +# by 1 centimeter to the right (positive x direction) and 0.5 centimeters down +# (negative y direction) fig.plot(x=0, y=-3, style="s0.2c", fill="darkorange", pen="0.7p,darkgray") fig.text(x=0, y=-3, text="my text") fig.text(x=0, y=-3, text="my text", offset="1c/-0.5c") @@ -82,8 +83,8 @@ pen="0.7p,darkgray", ) -# Plot text at the same x and y positions as the markers -# but with varying justifications +# Plot text labels at the x and y positions of the markers +# while using varying justifications fig.text(x=-0.5, y=0.5, text="TL", justify="TL") # TopLeft fig.text(x=0, y=0.5, text="TM", justify="TC") # TopCenter fig.text(x=0.5, y=0.5, text="TR", justify="TR") # TopRight @@ -103,25 +104,25 @@ # Add a box with a fill in green color fig.text(x=0, y=3, text="My text", fill="green") -# Add box with a seagreen, 1 point thick, solid outline +# Add box with a seagreen, 1-point thick, solid outline fig.text(x=0, y=1, text="My text", pen="1p,seagreen,solid") -# Add a margin between the text and border of the text box of 0.1 centimeters -# in x direction and 0.2 centimeters in y direction -fig.text(x=0, y=-1, text="My text", pen="1p,seagreen,dashed", clearance="0.2c/0.2c") +# Add margins between the text and the outline of the text box of 0.1 +# centimeters in x direction and 0.2 centimeters in y direction +fig.text(x=0, y=-1, text="My text", pen="1p,seagreen,dashed", clearance="0.1c/0.2c") -# Get rounded edges by appending "+tO" to the "clearance" parameter +# Get rounded edges by passing "+tO" to the "clearance" parameter fig.text(x=0, y=-3, text="My text", pen="1p,seagreen,solid", clearance="0.2c/0.2c+tO") fig.show() # %% -# Plotting text with individual configurations -# -------------------------------------------- +# Adding multiple text labels with individual configurations +# ---------------------------------------------------------- # -# To add text with individual ``font``, ``angle``, and ``justify`` one can -# provide lists with the corresponding arguments. +# To add mulitple text labels with individual ``font``, ``angle``, and ``justify`` +# one can provide lists with the corresponding arguments. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) @@ -143,12 +144,12 @@ # %% -# Use an external txt file -# ------------------------ +# Using an external input file +# ---------------------------- # -# It is also possible to add text labels via an external text file containing +# It is also possible to add text labels via an external input file containing # ``x``, ``y``, and ``text`` columns. Addionaly, columns to set the ``angle``, -# ``front``, and ``justify`` parameters can be provided. Here we give a +# ``front``, and ``justify`` parameters can be provided. Here, we give a # complete example. fig = pygmt.Figure() @@ -178,8 +179,8 @@ # %% -# The position parameter -# ---------------------- +# Using the position parameter +# ---------------------------- # # Instead of using ``x`` and ``y``, the ``position`` parameter can be # specified to set the reference point for the text on the plot. @@ -208,7 +209,7 @@ fig.shift_origin(xshift="+w1c") # ----------------------------------------------------------------------------- -# Right: Add a text label out of the plot or map frame +# Right: Add a text label outside of the plot or map frame # Define region limits lon_min = -30 @@ -234,7 +235,7 @@ position="TC", justify="MC", offset="0c/0.2c", - no_clip=True, # Allow plotting out of map or plot frame + no_clip=True, # Allow plotting outside of the map or plot frame ) fig.show() From 2e35c04c395609dda755db2b8bc0565b407c3d93 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Fri, 10 Nov 2023 11:43:40 +0100 Subject: [PATCH 09/28] Fix typo, Fix line length --- examples/tutorials/basics/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 0f4bcd6ff0a..f06ae486db3 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -121,8 +121,8 @@ # Adding multiple text labels with individual configurations # ---------------------------------------------------------- # -# To add mulitple text labels with individual ``font``, ``angle``, and ``justify`` -# one can provide lists with the corresponding arguments. +# To add multiple text labels with individual ``font``, ``angle``, and +# ``justify`` one can provide lists with the corresponding arguments. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) From add8af1d49636f8ec744d68ae3f9f453856a538c Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Sat, 11 Nov 2023 11:29:00 +0100 Subject: [PATCH 10/28] Make a seperate section for 'Adding a text box' --- examples/tutorials/basics/text.py | 39 +++++++++++++++++++------------ 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index f06ae486db3..ffcabf6064e 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -44,16 +44,12 @@ # * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) # # * ``offset``: Shifts the text relatively to the reference point. -# * ``fill``: Fills the text box with a color. -# * ``pen``: Outlines the text box. -# * ``clearance``: Adds margins in x and y directions between the text and the -# outline of the text box. Can be used to get a text box with rounded edges. fig = pygmt.Figure() # ----------------------------------------------------------------------------- # Left: "font", "angle", and "offset" parameters -fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) +fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="rtlb") # Change font size, family/weight, color of the text fig.text(x=0, y=3, text="my text", font="12p,Helvetica-Bold,blue") @@ -61,20 +57,21 @@ # Rotate the text by 30 degrees counter-clockwise from the horizontal fig.text(x=0, y=0, text="my text", angle=30) -# Shift the text relatively to the position given via the x and y parameters -# by 1 centimeter to the right (positive x direction) and 0.5 centimeters down -# (negative y direction) +# Plot marker and text label for reference fig.plot(x=0, y=-3, style="s0.2c", fill="darkorange", pen="0.7p,darkgray") fig.text(x=0, y=-3, text="my text") +# Shift the text label relatively to the position given via the x and y +# parameters by 1 centimeter to the right (positive x direction) and 0.5 +# centimeters down (negative y direction) fig.text(x=0, y=-3, text="my text", offset="1c/-0.5c") fig.shift_origin(xshift="+w0.5c") # ----------------------------------------------------------------------------- -# Middle: "justify" parameter +# Right: "justify" parameter fig.basemap(region=[-1, 1, -1, 1], projection="X5c", frame="rtlb") -# Plot markers +# Plot markers for reference fig.plot( x=[-0.5, 0, 0.5, -0.5, 0, 0.5, -0.5, 0, 0.5], y=[0.5, 0.5, 0.5, 0, 0, 0, -0.5, -0.5, -0.5], @@ -83,8 +80,8 @@ pen="0.7p,darkgray", ) -# Plot text labels at the x and y positions of the markers -# while using varying justifications +# Plot text labels at the x and y positions of the markers while varying the +# anchor point via the justify parameter fig.text(x=-0.5, y=0.5, text="TL", justify="TL") # TopLeft fig.text(x=0, y=0.5, text="TM", justify="TC") # TopCenter fig.text(x=0.5, y=0.5, text="TR", justify="TR") # TopRight @@ -95,10 +92,22 @@ fig.text(x=0, y=-0.5, text="BC", justify="BC") # BottomCenter fig.text(x=0.5, y=-0.5, text="BR", justify="BR") # BottomRight -fig.shift_origin(xshift="+w0.5c") +fig.show() + + +# %% +# Adding a text box +# ----------------- +# +# There are different optional parameters to add and customize a text box: +# +# * ``fill``: Fills the text box with a color. +# * ``pen``: Outlines the text box. +# * ``clearance``: Adds margins in x and y directions between the text and the +# outline of the text box. Can be used to get a text box with rounded edges. + +fig = pygmt.Figure() -# ----------------------------------------------------------------------------- -# Right: "fill", "pen", and "clearance" parameters fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame="rtlb") # Add a box with a fill in green color From 706fb8390e9a4a8dcfc296fb99588df92f8f53f5 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Thu, 21 Dec 2023 23:17:39 +0100 Subject: [PATCH 11/28] Shorten code --- examples/tutorials/basics/text.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index ffcabf6064e..409bf171bff 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -219,26 +219,10 @@ # ----------------------------------------------------------------------------- # Right: Add a text label outside of the plot or map frame - -# Define region limits -lon_min = -30 -lon_max = 30 -lat_min = 10 -lat_max = 60 -# Determine projection center -lon0 = np.mean([lon_min, lon_max]) -lat0 = np.mean([lat_min, lat_max]) -# Determine the two standard parallels (only these two distortion-free) -lat1 = (lat_min + lat_max) / 3 -lat2 = (lat_min + lat_max) / 3 * 2 -# Set up arguments for the region and projection parameters -region_use = [lon_min, lon_max, lat_min, lat_max] -projection_use = ( - "L" + str(lon0) + "/" + str(lat0) + "/" + str(lat1) + "/" + str(lat2) + "/5c" +fig.basemap( + region=[-30, 30, 10, 60], projection="L0/35/23/47/5c", frame=["lStE", "af"] ) -fig.basemap(region=region_use, projection=projection_use, frame=["lStE", "af"]) - fig.text( text="@@100 km", # "@@" gives "@" in GMT or PyGMT position="TC", From 2dcbb547c4487e8880804feabb6c0fc99ecc753c Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Thu, 21 Dec 2023 23:21:16 +0100 Subject: [PATCH 12/28] Remove un-needed import --- examples/tutorials/basics/text.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 409bf171bff..eab4aa19f9e 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -8,7 +8,6 @@ import os -import numpy as np import pygmt # %% From 87ba5742fcdb04efa290b865d86d2f25eb6b8c3c Mon Sep 17 00:00:00 2001 From: actions-bot <58130806+actions-bot@users.noreply.github.com> Date: Thu, 21 Dec 2023 22:26:45 +0000 Subject: [PATCH 13/28] [format-command] fixes --- examples/tutorials/basics/text.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index eab4aa19f9e..5dcdde51d3a 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -218,9 +218,7 @@ # ----------------------------------------------------------------------------- # Right: Add a text label outside of the plot or map frame -fig.basemap( - region=[-30, 30, 10, 60], projection="L0/35/23/47/5c", frame=["lStE", "af"] -) +fig.basemap(region=[-30, 30, 10, 60], projection="L0/35/23/47/5c", frame=["lStE", "af"]) fig.text( text="@@100 km", # "@@" gives "@" in GMT or PyGMT From 6b5a02f72239888ebc8c2d89de48ad282a33bc79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Fri, 22 Dec 2023 11:59:02 +0100 Subject: [PATCH 14/28] Try to remove white space --- examples/tutorials/basics/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 5dcdde51d3a..4c89c5da476 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -34,8 +34,8 @@ # * ``font``: Sets the size, family/weight, and color of the font for the text. # :gmt-docs:`PostScript Fonts Used by GMT `, # including details of how to use non-default fonts. -# * ``angle``: Specifies the rotation of the text. It is measured counter- -# clockwise from the horizontal in degrees. +# * ``angle``: Specifies the rotation of the text. It is measured +# counter-clockwise from the horizontal in degrees. # * ``justify``: Defines the anchor point of the bounding box for the text. # It is specified by a two-letter (order independent) code, chosen from: # From c2c83f6414660e2f9961fc9824cb876b307caaeb Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Tue, 26 Dec 2023 22:21:39 +0100 Subject: [PATCH 15/28] Improve argument passed to 'xshift' --- examples/tutorials/basics/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 4c89c5da476..861607bcf73 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -64,7 +64,7 @@ # centimeters down (negative y direction) fig.text(x=0, y=-3, text="my text", offset="1c/-0.5c") -fig.shift_origin(xshift="+w0.5c") +fig.shift_origin(xshift="w+0.5c") # ----------------------------------------------------------------------------- # Right: "justify" parameter @@ -214,7 +214,7 @@ justify="TL", ) -fig.shift_origin(xshift="+w1c") +fig.shift_origin(xshift="w+1c") # ----------------------------------------------------------------------------- # Right: Add a text label outside of the plot or map frame From 1e8d070e3131713c5a8d7a62c2c142c57d08d700 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Tue, 16 Jan 2024 12:05:30 +0100 Subject: [PATCH 16/28] Wrap to 88 characters --- examples/tutorials/basics/text.py | 78 +++++++++++++++---------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 861607bcf73..e11498ff372 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -2,8 +2,8 @@ Plotting text ============= -It is often useful to add text annotations to a plot or map. This is handled -by the :meth:`pygmt.Figure.text` method of the :class:`pygmt.Figure` class. +It is often useful to add text annotations to a plot or map. This is handled by the +:meth:`pygmt.Figure.text` method of the :class:`pygmt.Figure` class. """ import os @@ -14,8 +14,8 @@ # Adding a single text label # -------------------------- # -# To add a single text label to a plot the ``text`` and ``x`` and ``y`` -# parameters to specify the text and position. +# To add a single text label to a plot the ``text`` and ``x`` and ``y`` parameters to +# specify the text and position. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) @@ -34,13 +34,13 @@ # * ``font``: Sets the size, family/weight, and color of the font for the text. # :gmt-docs:`PostScript Fonts Used by GMT `, # including details of how to use non-default fonts. -# * ``angle``: Specifies the rotation of the text. It is measured -# counter-clockwise from the horizontal in degrees. -# * ``justify``: Defines the anchor point of the bounding box for the text. -# It is specified by a two-letter (order independent) code, chosen from: +# * ``angle``: Specifies the rotation of the text. It is measured counter-clockwise +# from the horizontal in degrees. +# * ``justify``: Defines the anchor point of the bounding box for the text. It is +# specified by a two-letter (order independent) code, chosen from: # -# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) -# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) +# * Vertical: **T**\(op), **M**\(iddle), **B**\(ottom) +# * Horizontal: **L**\(eft), **C**\(entre), **R**\(ight) # # * ``offset``: Shifts the text relatively to the reference point. @@ -59,9 +59,9 @@ # Plot marker and text label for reference fig.plot(x=0, y=-3, style="s0.2c", fill="darkorange", pen="0.7p,darkgray") fig.text(x=0, y=-3, text="my text") -# Shift the text label relatively to the position given via the x and y -# parameters by 1 centimeter to the right (positive x direction) and 0.5 -# centimeters down (negative y direction) +# Shift the text label relatively to the position given via the x and y parameters +# by 1 centimeter to the right (positive x direction) and 0.5 centimeters down +# (negative y direction) fig.text(x=0, y=-3, text="my text", offset="1c/-0.5c") fig.shift_origin(xshift="w+0.5c") @@ -79,8 +79,8 @@ pen="0.7p,darkgray", ) -# Plot text labels at the x and y positions of the markers while varying the -# anchor point via the justify parameter +# Plot text labels at the x and y positions of the markers while varying the anchor +# point via the justify parameter fig.text(x=-0.5, y=0.5, text="TL", justify="TL") # TopLeft fig.text(x=0, y=0.5, text="TM", justify="TC") # TopCenter fig.text(x=0.5, y=0.5, text="TR", justify="TR") # TopRight @@ -102,8 +102,8 @@ # # * ``fill``: Fills the text box with a color. # * ``pen``: Outlines the text box. -# * ``clearance``: Adds margins in x and y directions between the text and the -# outline of the text box. Can be used to get a text box with rounded edges. +# * ``clearance``: Adds margins in x and y directions between the text and the outline +# of the text box. Can be used to get a text box with rounded edges. fig = pygmt.Figure() @@ -129,8 +129,8 @@ # Adding multiple text labels with individual configurations # ---------------------------------------------------------- # -# To add multiple text labels with individual ``font``, ``angle``, and -# ``justify`` one can provide lists with the corresponding arguments. +# To add multiple text labels with individual ``font``, ``angle``, and ``justify``, +# one can provide lists with the corresponding arguments. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) @@ -155,10 +155,9 @@ # Using an external input file # ---------------------------- # -# It is also possible to add text labels via an external input file containing -# ``x``, ``y``, and ``text`` columns. Addionaly, columns to set the ``angle``, -# ``front``, and ``justify`` parameters can be provided. Here, we give a -# complete example. +# It is also possible to add text labels via an external input file containing ``x``, +# ``y``, and ``text`` columns. Addionaly, columns to set the ``angle``, ``front``, +# and ``justify`` parameters can be provided. Here, we give a complete example. fig = pygmt.Figure() fig.basemap(region=[108, 121, -5, 8], projection="M10c", frame="a2f1") @@ -173,11 +172,10 @@ f.write("119.12 7.25 -40 8p,Helvetica-Bold,black CM SULU SEA\n") f.write("118.40 -1.00 65 8p,Helvetica-Bold,black CM MAKASSAR STRAIT\n") -# Plot region names / sea names from a text file, where -# the longitude (x) and latitude (y) coordinates are in the first two columns. -# Setting angle/font/justify to True will indicate that those columns are -# present in the text file too (Please note: must be in that order). -# Finally, the text to be printed will be in the last column +# Plot region names / sea names from a text file, where the longitude (x) and latitude +# (y) coordinates are in the first two columns. Setting angle/font/justify to True +# will indicate that those columns are present in the text file too (Please note: must +# be in that order). Finally, the text to be printed will be in the last column fig.text(textfiles="examples.txt", angle=True, font=True, justify=True) # Cleanups @@ -190,16 +188,16 @@ # Using the position parameter # ---------------------------- # -# Instead of using ``x`` and ``y``, the ``position`` parameter can be -# specified to set the reference point for the text on the plot. -# As for the ``justify`` parameter, the ``position`` parameter is specified -# by a two-letter (order independent) code, chosen from: +# Instead of using ``x`` and ``y``, the ``position`` parameter can be specified to set +# the reference point for the text on the plot. As for the ``justify`` parameter, the +# ``position`` parameter is specified by a two-letter (order independent) code, chosen +# from: # -# * Vertical anchor: **T**\(op), **M**\(iddle), **B**\(ottom) -# * Horizontal anchor: **L**\(eft), **C**\(entre), **R**\(ight) +# * Vertical: **T**\(op), **M**\(iddle), **B**\(ottom) +# * Horizontal: **L**\(eft), **C**\(entre), **R**\(ight) # -# This can be helpful to add a tag to a subplot or text labels out of -# the plot or map frame, e.g., for depth slices. +# This can be helpful to add a tag to a subplot or text labels out of the plot or map +# frame, e.g., for depth slices. fig = pygmt.Figure() @@ -235,9 +233,9 @@ # Advanced configuration # ---------------------- # -# For crafting more advanced styles, including using special symbols and -# other character sets, be sure to check out the GMT documentation -# at :gmt-docs:`text.html` and also the cookbook at -# :gmt-docs:`cookbook/features.html#placement-of-text`. Good luck! +# For crafting more advanced styles, including using special symbols and other character +# sets, be sure to check out the GMT documentation at :gmt-docs:`text.html` and also the +# Technical References at :gmt-docs:`reference/features.html#placement-of-text`. Good +# luck! # sphinx_gallery_thumbnail_number = 4 From 2c6ef529b77c2f1ad62ce4573296a682c3d6058c Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Wed, 31 Jan 2024 21:46:32 +0100 Subject: [PATCH 17/28] TEST: Remove choropleth example --- examples/gallery/maps/choropleth_map.py | 57 ------------------------- 1 file changed, 57 deletions(-) delete mode 100644 examples/gallery/maps/choropleth_map.py diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py deleted file mode 100644 index 6c43d24d3dd..00000000000 --- a/examples/gallery/maps/choropleth_map.py +++ /dev/null @@ -1,57 +0,0 @@ -""" -Choropleth map -============== - -The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such -as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use -:func:`geopandas.read_file` to load data from any supported OGR format such as -a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also -use a full URL pointing to your desired data source. Then, pass the -:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of -:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. -To fill the polygons based on a corresponding column you need to set -``fill="+z"`` as well as select the appropriate column using the ``aspatial`` -parameter as shown in the example below. -""" - -# %% -import geopandas as gpd -import pygmt - -# Read polygon data using geopandas -gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip") - -fig = pygmt.Figure() - -fig.basemap( - region=gdf.total_bounds[[0, 2, 1, 3]], - projection="M6c", - frame="+tPopulation of Chicago", -) - -# The dataset contains different attributes, here we select -# the "population" column to plot. - -# First, we define the colormap to fill the polygons based on -# the "population" column. -pygmt.makecpt( - cmap="acton", - series=[gdf["population"].min(), gdf["population"].max(), 10], - continuous=True, - reverse=True, -) - -# Next, we plot the polygons and fill them using the defined colormap. -# The target column is defined by the aspatial parameter. -fig.plot( - data=gdf, - pen="0.3p,gray10", - fill="+z", - cmap=True, - aspatial="Z=population", -) - -# Add colorbar legend -fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c") - -fig.show() From ba521f1ea5244b334747bc180be262287bc930a0 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Wed, 31 Jan 2024 22:04:55 +0100 Subject: [PATCH 18/28] TEST: Remove roads example --- examples/gallery/lines/roads.py | 46 --------------------------------- 1 file changed, 46 deletions(-) delete mode 100644 examples/gallery/lines/roads.py diff --git a/examples/gallery/lines/roads.py b/examples/gallery/lines/roads.py deleted file mode 100644 index 7c1e75ef943..00000000000 --- a/examples/gallery/lines/roads.py +++ /dev/null @@ -1,46 +0,0 @@ -# ruff: noqa: RUF003 -""" -Roads -===== - -The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such -as lines which are stored in a :class:`geopandas.GeoDataFrame` object. Use -:func:`geopandas.read_file` to load data from any supported OGR format such as -a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. Then, pass the -:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of -:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. -""" - -# %% -import geopandas as gpd -import pygmt - -# Read shapefile data using geopandas -gdf = gpd.read_file( - "http://www2.census.gov/geo/tiger/TIGER2015/PRISECROADS/tl_2015_15_prisecroads.zip" -) -# The dataset contains different road types listed in the RTTYP column, -# here we select the following ones to plot: -roads_common = gdf[gdf.RTTYP == "M"] # Common name roads -roads_state = gdf[gdf.RTTYP == "S"] # State recognized roads -roads_interstate = gdf[gdf.RTTYP == "I"] # Interstate roads - -fig = pygmt.Figure() - -# Define target region around Oʻahu (Hawaiʻi) -region = [-158.3, -157.6, 21.2, 21.75] # xmin, xmax, ymin, ymax - -title = "Main roads of O`ahu (Hawai`i)" # Approximating the Okina letter ʻ with ` -fig.basemap(region=region, projection="M12c", frame=["af", f"WSne+t{title}"]) -fig.coast(land="gray", water="dodgerblue4", shorelines="1p,black") - -# Plot the individual road types with different pen settings and assign labels -# which are displayed in the legend -fig.plot(data=roads_common, pen="5p,dodgerblue", label="CommonName") -fig.plot(data=roads_state, pen="2p,gold", label="StateRecognized") -fig.plot(data=roads_interstate, pen="2p,red", label="Interstate") - -# Add legend -fig.legend() - -fig.show() From 9c58e0ec0665c3940548067df088da7585d23724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Thu, 1 Feb 2024 17:20:55 +0100 Subject: [PATCH 19/28] Revert "TEST: Remove choropleth example" This reverts commit 2c6ef529b77c2f1ad62ce4573296a682c3d6058c. --- examples/gallery/maps/choropleth_map.py | 57 +++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/gallery/maps/choropleth_map.py diff --git a/examples/gallery/maps/choropleth_map.py b/examples/gallery/maps/choropleth_map.py new file mode 100644 index 00000000000..6c43d24d3dd --- /dev/null +++ b/examples/gallery/maps/choropleth_map.py @@ -0,0 +1,57 @@ +""" +Choropleth map +============== + +The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such +as polygons which are stored in a :class:`geopandas.GeoDataFrame` object. Use +:func:`geopandas.read_file` to load data from any supported OGR format such as +a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. You can also +use a full URL pointing to your desired data source. Then, pass the +:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of +:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. +To fill the polygons based on a corresponding column you need to set +``fill="+z"`` as well as select the appropriate column using the ``aspatial`` +parameter as shown in the example below. +""" + +# %% +import geopandas as gpd +import pygmt + +# Read polygon data using geopandas +gdf = gpd.read_file("https://geodacenter.github.io/data-and-lab/data/airbnb.zip") + +fig = pygmt.Figure() + +fig.basemap( + region=gdf.total_bounds[[0, 2, 1, 3]], + projection="M6c", + frame="+tPopulation of Chicago", +) + +# The dataset contains different attributes, here we select +# the "population" column to plot. + +# First, we define the colormap to fill the polygons based on +# the "population" column. +pygmt.makecpt( + cmap="acton", + series=[gdf["population"].min(), gdf["population"].max(), 10], + continuous=True, + reverse=True, +) + +# Next, we plot the polygons and fill them using the defined colormap. +# The target column is defined by the aspatial parameter. +fig.plot( + data=gdf, + pen="0.3p,gray10", + fill="+z", + cmap=True, + aspatial="Z=population", +) + +# Add colorbar legend +fig.colorbar(frame="x+lPopulation", position="jML+o-0.5c+w3.5c/0.2c") + +fig.show() From 3fa7763bdfb1202709befb356507fcbcd11fda29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= Date: Thu, 1 Feb 2024 17:21:22 +0100 Subject: [PATCH 20/28] Revert "TEST: Remove roads example" This reverts commit ba521f1ea5244b334747bc180be262287bc930a0. --- examples/gallery/lines/roads.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 examples/gallery/lines/roads.py diff --git a/examples/gallery/lines/roads.py b/examples/gallery/lines/roads.py new file mode 100644 index 00000000000..7c1e75ef943 --- /dev/null +++ b/examples/gallery/lines/roads.py @@ -0,0 +1,46 @@ +# ruff: noqa: RUF003 +""" +Roads +===== + +The :meth:`pygmt.Figure.plot` method allows us to plot geographical data such +as lines which are stored in a :class:`geopandas.GeoDataFrame` object. Use +:func:`geopandas.read_file` to load data from any supported OGR format such as +a shapefile (.shp), GeoJSON (.geojson), geopackage (.gpkg), etc. Then, pass the +:class:`geopandas.GeoDataFrame` as an argument to the ``data`` parameter of +:meth:`pygmt.Figure.plot`, and style the geometry using the ``pen`` parameter. +""" + +# %% +import geopandas as gpd +import pygmt + +# Read shapefile data using geopandas +gdf = gpd.read_file( + "http://www2.census.gov/geo/tiger/TIGER2015/PRISECROADS/tl_2015_15_prisecroads.zip" +) +# The dataset contains different road types listed in the RTTYP column, +# here we select the following ones to plot: +roads_common = gdf[gdf.RTTYP == "M"] # Common name roads +roads_state = gdf[gdf.RTTYP == "S"] # State recognized roads +roads_interstate = gdf[gdf.RTTYP == "I"] # Interstate roads + +fig = pygmt.Figure() + +# Define target region around Oʻahu (Hawaiʻi) +region = [-158.3, -157.6, 21.2, 21.75] # xmin, xmax, ymin, ymax + +title = "Main roads of O`ahu (Hawai`i)" # Approximating the Okina letter ʻ with ` +fig.basemap(region=region, projection="M12c", frame=["af", f"WSne+t{title}"]) +fig.coast(land="gray", water="dodgerblue4", shorelines="1p,black") + +# Plot the individual road types with different pen settings and assign labels +# which are displayed in the legend +fig.plot(data=roads_common, pen="5p,dodgerblue", label="CommonName") +fig.plot(data=roads_state, pen="2p,gold", label="StateRecognized") +fig.plot(data=roads_interstate, pen="2p,red", label="Interstate") + +# Add legend +fig.legend() + +fig.show() From 0ce220416c7b22751775ab4f4ec43acacb735c19 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Mon, 5 Feb 2024 08:43:08 +0100 Subject: [PATCH 21/28] TEST: date_time_charts - Change symbol --- examples/tutorials/advanced/date_time_charts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/advanced/date_time_charts.py b/examples/tutorials/advanced/date_time_charts.py index 69622088675..3b1a1065ff7 100644 --- a/examples/tutorials/advanced/date_time_charts.py +++ b/examples/tutorials/advanced/date_time_charts.py @@ -47,7 +47,7 @@ frame=["WSen", "afg"], x=x, y=y, - style="x0.3c", + style="c0.3c", pen="1p", ) fig.show() From ec295ebf55def3c1b54ff290aa252b301c4fa6f4 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Fri, 5 Apr 2024 09:56:59 +0200 Subject: [PATCH 22/28] Follow PTH123 --- examples/tutorials/basics/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 873809c12a6..b69f7c1a071 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -166,7 +166,7 @@ fig.coast(land="darkgray", water="steelblue", shorelines="1/0.1p,gray30") # Create space-delimited file -with open("examples.txt", "w") as f: +with Path.open("examples.txt", "w") as f: f.write("114.00 0.50 0 15p,Helvetica-Bold,white CM BORNEO\n") f.write("119.00 3.25 0 8p,Helvetica-Bold,black CM CELEBES SEA\n") f.write("112.00 -4.60 0 8p,Helvetica-Bold,black CM JAVA SEA\n") From 2ece2a8230fd238a65befcb6fa3dc706a74d7340 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Thu, 11 Apr 2024 20:32:15 +0200 Subject: [PATCH 23/28] Improve documentation and comments --- examples/tutorials/basics/text.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index b69f7c1a071..64852969f14 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -165,7 +165,10 @@ fig.basemap(region=[108, 121, -5, 8], projection="M10c", frame="a2f1") fig.coast(land="darkgray", water="steelblue", shorelines="1/0.1p,gray30") -# Create space-delimited file +# Create space-delimited file with region / sea names: +# - longitude (x) and latitude (y) coordinates are in the first two columns +# - angle, font, and justify muss be present in this order in the next three columns +# - the text to be printed is given in the last column with Path.open("examples.txt", "w") as f: f.write("114.00 0.50 0 15p,Helvetica-Bold,white CM BORNEO\n") f.write("119.00 3.25 0 8p,Helvetica-Bold,black CM CELEBES SEA\n") @@ -174,10 +177,8 @@ f.write("119.12 7.25 -40 8p,Helvetica-Bold,black CM SULU SEA\n") f.write("118.40 -1.00 65 8p,Helvetica-Bold,black CM MAKASSAR STRAIT\n") -# Plot region names / sea names from a text file, where the longitude (x) and latitude -# (y) coordinates are in the first two columns. Setting angle/font/justify to True -# will indicate that those columns are present in the text file too (Please note: must -# be in that order). Finally, the text to be printed will be in the last column +# Setting the angle, font, and justify parameters to True indicates that those columns +# are present in the text file fig.text(textfiles="examples.txt", angle=True, font=True, justify=True) # Cleanups @@ -190,10 +191,10 @@ # Using the position parameter # ---------------------------- # -# Instead of using ``x`` and ``y``, the ``position`` parameter can be specified to set -# the reference point for the text on the plot. As for the ``justify`` parameter, the -# ``position`` parameter is specified by a two-letter (order independent) code, chosen -# from: +# Instead of using the ``x`` and ``y`` parameters, the ``position`` parameter can be +# specified to set the reference point for the text on the plot. As for the ``justify`` +# parameter, the ``position`` parameter is specified by a two-letter (order independent) +# code, chosen from: # # * Vertical: **T**\(op), **M**\(iddle), **B**\(ottom) # * Horizontal: **L**\(eft), **C**\(entre), **R**\(ight) @@ -209,21 +210,21 @@ fig.text( text="(a)", - position="TL", + position="TL", # Top Left + justify="TL", # Top Left offset="0.1c/-0.1c", - justify="TL", ) fig.shift_origin(xshift="w+1c") # ----------------------------------------------------------------------------- # Right: Add a text label outside of the plot or map frame -fig.basemap(region=[-30, 30, 10, 60], projection="L0/35/23/47/5c", frame=["lStE", "af"]) +fig.basemap(region=[-30, 30, 10, 60], projection="L0/35/23/47/5c", frame=["wSnE", "af"]) fig.text( text="@@100 km", # "@@" gives "@" in GMT or PyGMT - position="TC", - justify="MC", + position="TC", # Top Center + justify="MC", # Middle Center offset="0c/0.2c", no_clip=True, # Allow plotting outside of the map or plot frame ) From 47002ab4cb1fc313d69407254681ab59264656b1 Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Thu, 11 Apr 2024 20:34:45 +0200 Subject: [PATCH 24/28] Remove white space --- examples/tutorials/basics/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 64852969f14..40fef4e60c3 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -224,7 +224,7 @@ fig.text( text="@@100 km", # "@@" gives "@" in GMT or PyGMT position="TC", # Top Center - justify="MC", # Middle Center + justify="MC", # Middle Center offset="0c/0.2c", no_clip=True, # Allow plotting outside of the map or plot frame ) From 3bc7d05225bdc82a180186d4e89f59135976e8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= <94163266+yvonnefroehlich@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:01:28 +0200 Subject: [PATCH 25/28] Add missing verb Co-authored-by: Dongdong Tian --- examples/tutorials/basics/text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 40fef4e60c3..8cad68105e8 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -15,7 +15,7 @@ # Adding a single text label # -------------------------- # -# To add a single text label to a plot the ``text`` and ``x`` and ``y`` parameters to +# To add a single text label to a plot, use the ``text`` and ``x`` and ``y`` parameters to # specify the text and position. fig = pygmt.Figure() From a405fd3b8862153b8b825ad85ea514d0ff9c6f94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= <94163266+yvonnefroehlich@users.noreply.github.com> Date: Fri, 19 Apr 2024 10:10:08 +0200 Subject: [PATCH 26/28] Remove blank line Co-authored-by: Dongdong Tian --- examples/tutorials/basics/text.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 8cad68105e8..9bd7aaa7e48 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -20,7 +20,6 @@ fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) - fig.text(x=0, y=0, text="My text") fig.show() From 50d196f1a61d6aa8efc92dc97bc4ac7a7217811d Mon Sep 17 00:00:00 2001 From: yvonnefroelich Date: Fri, 19 Apr 2024 10:11:47 +0200 Subject: [PATCH 27/28] Fix line length --- examples/tutorials/basics/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 9bd7aaa7e48..18c87656fb3 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -15,8 +15,8 @@ # Adding a single text label # -------------------------- # -# To add a single text label to a plot, use the ``text`` and ``x`` and ``y`` parameters to -# specify the text and position. +# To add a single text label to a plot, use the ``text`` and ``x`` and ``y`` parameters +# to specify the text and position. fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) From 33c0d7d939e5ed56b3c1597c932636c2e29c5b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yvonne=20Fr=C3=B6hlich?= <94163266+yvonnefroehlich@users.noreply.github.com> Date: Sat, 20 Apr 2024 16:56:12 +0200 Subject: [PATCH 28/28] Remove blank line Co-authored-by: Dongdong Tian --- examples/tutorials/basics/text.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/tutorials/basics/text.py b/examples/tutorials/basics/text.py index 18c87656fb3..35744456e40 100644 --- a/examples/tutorials/basics/text.py +++ b/examples/tutorials/basics/text.py @@ -21,7 +21,6 @@ fig = pygmt.Figure() fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) fig.text(x=0, y=0, text="My text") - fig.show()