|
| 1 | +""" |
| 2 | +Plotting text |
| 3 | +============= |
| 4 | +
|
| 5 | +It is often useful to add annotations to a map plot. This is handled by |
| 6 | +:meth:`pygmt.Figure.text`. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import pygmt |
| 11 | + |
| 12 | +############################################################################### |
| 13 | +# Basic map annotation |
| 14 | +# -------------------- |
| 15 | +# |
| 16 | +# Text annotations can be added to a map using the :meth:`pygmt.Figure.text` |
| 17 | +# method of the :class:`pygmt.Figure` class. |
| 18 | +# |
| 19 | +# Here we create a simple map and add an annotation using the ``text``, ``x``, |
| 20 | +# and ``y`` arguments to specify the annotation text and position in the |
| 21 | +# projection frame. ``text`` accepts 'str' types, while ``x``, and ``y`` |
| 22 | +# accepts either 'int'/'float' numbers, or a list/array of numbers. |
| 23 | + |
| 24 | +fig = pygmt.Figure() |
| 25 | +with pygmt.config(MAP_FRAME_TYPE="plain"): |
| 26 | + fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") |
| 27 | +fig.coast(land="black", water="skyblue") |
| 28 | + |
| 29 | +# Plotting text annotations using single elements |
| 30 | +fig.text(text="SOUTH CHINA SEA", x=112, y=6) |
| 31 | + |
| 32 | +# Plotting text annotations using lists of elements |
| 33 | +fig.text(text=["CELEBES SEA", "JAVA SEA"], x=[119, 112], y=[3.25, -4.6]) |
| 34 | + |
| 35 | +fig.show() |
| 36 | + |
| 37 | +############################################################################### |
| 38 | +# Changing font style |
| 39 | +# ------------------- |
| 40 | +# The size, family/weight, and color of an annotation can be specified using |
| 41 | +# the ``font`` argument. |
| 42 | +# |
| 43 | +# A list of all recognised fonts can be found at |
| 44 | +# :gmt-docs:`cookbook/postscript-fonts.html`, including details of how to use |
| 45 | +# non-default fonts. |
| 46 | + |
| 47 | +fig = pygmt.Figure() |
| 48 | +with pygmt.config(MAP_FRAME_TYPE="plain"): |
| 49 | + fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") |
| 50 | +fig.coast(land="black", water="skyblue") |
| 51 | + |
| 52 | +# Customising the font style |
| 53 | +fig.text(text="BORNEO", x=114.0, y=0.5, font="22p,Helvetica-Bold,white") |
| 54 | + |
| 55 | +fig.show() |
| 56 | + |
| 57 | +############################################################################### |
| 58 | +# Plotting from a text file |
| 59 | +# ------------------------- |
| 60 | +# |
| 61 | +# It is also possible to add annotations from a file containing `x`, `y`, and |
| 62 | +# `text` fields. Here we give a complete example. |
| 63 | + |
| 64 | +fig = pygmt.Figure() |
| 65 | +with pygmt.config(MAP_FRAME_TYPE="plain"): |
| 66 | + fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") |
| 67 | +fig.coast(land="black", water="skyblue") |
| 68 | + |
| 69 | +# Create space-delimited file |
| 70 | +with open("examples.txt", "w") as f: |
| 71 | + f.write("114 0.5 0 22p,Helvetica-Bold,white CM BORNEO\n") |
| 72 | + f.write("119 3.25 0 12p,Helvetica-Bold,black CM CELEBES SEA\n") |
| 73 | + f.write("112 -4.6 0 12p,Helvetica-Bold,black CM JAVA SEA\n") |
| 74 | + f.write("112 6 40 12p,Helvetica-Bold,black CM SOUTH CHINA SEA\n") |
| 75 | + f.write("119.12 7.25 -40 12p,Helvetica-Bold,black CM SULU SEA\n") |
| 76 | + f.write("118.4 -1 65 12p,Helvetica-Bold,black CM MAKASSAR STRAIT\n") |
| 77 | + |
| 78 | +# Plot region names / sea names from a text file, where |
| 79 | +# the longitude (x) and latitude (y) coordinates are in the first two columns. |
| 80 | +# Setting angle/font/justiry to True will indicate that those columns are |
| 81 | +# present in the text file too (Note: must be in that order!). |
| 82 | +# Finally, the text to be printed will be in the last column |
| 83 | +fig.text(textfiles="examples.txt", angle=True, font=True, justify=True) |
| 84 | + |
| 85 | +# Cleanups |
| 86 | +os.remove("examples.txt") |
| 87 | + |
| 88 | +fig.show() |
| 89 | + |
| 90 | +############################################################################### |
| 91 | +# ``justify`` argument |
| 92 | +# -------------------- |
| 93 | +# |
| 94 | +# ``justify`` is used to define the anchor point for the bounding box for text |
| 95 | +# being added to a plot. The following code segment demonstrates the |
| 96 | +# positioning of the anchor point relative to the text. |
| 97 | +# |
| 98 | +# The anchor is specified with a two letter (order independent) code, chosen |
| 99 | +# from: |
| 100 | +# * Vertical anchor: T(op), M(iddle), B(ottom) |
| 101 | +# * Horizontal anchor: L(eft), C(entre), R(ight) |
| 102 | + |
| 103 | +fig = pygmt.Figure() |
| 104 | +fig.basemap(region=[0, 3, 0, 3], projection="X10c", frame=["WSne", "af0.5g"]) |
| 105 | +for position in ("TL", "TC", "TR", "ML", "MC", "MR", "BL", "BC", "BR"): |
| 106 | + fig.text( |
| 107 | + text=position, |
| 108 | + position=position, |
| 109 | + font="28p,Helvetica-Bold,black", |
| 110 | + justify=position, |
| 111 | + ) |
| 112 | +fig.show() |
| 113 | + |
| 114 | +############################################################################### |
| 115 | +# ``angle`` argument |
| 116 | +# ------------------ |
| 117 | +# ``angle`` is an optional argument used to specify the clockwise rotation of |
| 118 | +# the text from the horizontal. |
| 119 | + |
| 120 | +fig = pygmt.Figure() |
| 121 | +fig.basemap(region=[0, 4, 0, 4], projection="X5c", frame="WSen") |
| 122 | +for i in range(0, 360, 30): |
| 123 | + fig.text(text=f"` {i}@.", x=2, y=2, justify="LM", angle=i) |
| 124 | +fig.show() |
| 125 | + |
| 126 | +############################################################################### |
| 127 | +# ``fill`` argument |
| 128 | +# ----------------- |
| 129 | +# |
| 130 | +# ``fill`` is used to set the fill color of the area surrounding the text. |
| 131 | + |
| 132 | +fig = pygmt.Figure() |
| 133 | +fig.basemap(region=[0, 1, 0, 1], projection="X5c", frame="WSen") |
| 134 | +fig.text(text="Green", x=0.5, y=0.5, fill="green") |
| 135 | +fig.show() |
| 136 | + |
| 137 | +############################################################################### |
| 138 | +# Advanced configuration |
| 139 | +# ---------------------- |
| 140 | +# |
| 141 | +# For crafting more advanced styles, be sure to check out the GMT documentation |
| 142 | +# at :gmt-docs:`text.html` and also the cookbook at |
| 143 | +# :gmt-docs:`cookbook/features.html#placement-of-text`. Good luck! |
0 commit comments