Skip to content

Commit a9630ce

Browse files
DOC: Add Technical Reference page for justification codes (#4028)
Co-authored-by: Dongdong Tian <[email protected]>
1 parent 4789f68 commit a9630ce

File tree

8 files changed

+151
-40
lines changed

8 files changed

+151
-40
lines changed

doc/techref/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ fonts.md
1414
text_formatting.md
1515
patterns.md
1616
encodings.md
17+
justification_codes.md
1718
environment_variables.md
1819
```

doc/techref/justification_codes.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
file_format: mystnb
3+
---
4+
5+
# Justification codes
6+
7+
To place plot embellishments, such as scalebars, directional roses, colorbars, legends,
8+
text, or images on a figure, two points have to be specified: a point somewhere on the
9+
figure (**reference point**) and a point on the feature (**anchor point**). For both,
10+
users can use a two-character code, a combination of a vertical code and a horizontal
11+
code (order-independent):
12+
13+
- Vertical: **T**(op), **M**(iddle), **B**(ottom)
14+
- Horizontal: **L**(eft), **C**(entre), **R**(ight)
15+
16+
For example, `"TL"` means **T**op **L**eft.
17+
18+
The possible nine justification codes are visualized in the sketch below:
19+
20+
```{code-cell}
21+
---
22+
tags: [remove-input]
23+
---
24+
"""
25+
Script showing the justification codes used in GMT / PyGMT.
26+
"""
27+
import pygmt
28+
29+
size = 5
30+
x1 = [-size, 0, size, size, size, 0, -size, -size, 0]
31+
y1 = [-size, -size, -size, 0, size, size, size, 0, 0]
32+
codes = ["BL", "BC", "BR", "MR", "TR", "TC", "TL", "ML", "MC"]
33+
34+
fig = pygmt.Figure()
35+
fig.basemap(projection="X10c/6c", region=[-size, size, -size, size], frame=0)
36+
37+
fig.text(
38+
font="15p,1,black",
39+
x=x1,
40+
y=y1,
41+
text=codes,
42+
justify=codes,
43+
offset="j0.5c/0.5c+v2p,gray30",
44+
)
45+
46+
fig.plot(x=x1, y=y1, style="c0.3c", fill="steelblue", no_clip=True)
47+
48+
fig.text(
49+
font="15p",
50+
offset="j0.5c/0.5c",
51+
no_clip=True,
52+
x=[size, size, size, -size, 0, size],
53+
y=[size, 0, -size, size, size, size],
54+
justify=["ML", "ML", "ML", "BC", "BC", "BC"],
55+
text=[
56+
"@%1%T@%%op",
57+
"@%1%M@%%iddle",
58+
"@%1%B@%%ottom",
59+
"@%1%L@%%eft",
60+
"@%1%C@%%enter",
61+
"@%1%R@%%ight",
62+
],
63+
)
64+
65+
fig.show(width=600)
66+
```
67+
68+
For a non-rectangular geographic basemap, the justification codes refer to the invisible,
69+
rectangular map bounding box:
70+
71+
```{code-cell}
72+
---
73+
tags: [remove-input]
74+
---
75+
"""
76+
Script showing justification codes for non-rectangular geographic basemaps.
77+
"""
78+
fig = pygmt.Figure()
79+
fig.basemap(projection="H10c", region="g", frame=0)
80+
81+
for code in codes:
82+
fig.text(
83+
font="10p,1,black",
84+
position=code,
85+
justify=code,
86+
text=code,
87+
offset="j0.5c/0.5c+v2p,gray30",
88+
)
89+
fig.text(font="10p,steelblue", position=code, justify="MC", text="●", no_clip=True)
90+
91+
fig.show(width=600)
92+
```
93+
94+
95+
Plot embellishments can be abstracted as rectangles. Here, the justification codes are
96+
shown exemplary for a colorbar.
97+
98+
```{code-cell}
99+
---
100+
tags: [remove-input]
101+
---
102+
"""
103+
Script showing justification codes for plot embellishments, e.g., a colorbar.
104+
"""
105+
fig = pygmt.Figure()
106+
fig.basemap(projection="X10c/2c", region=[-size, size, -size, size], frame=0)
107+
108+
fig.colorbar(cmap="buda", frame=0, position="jMC+w10c/2c+h")
109+
110+
for code in codes:
111+
fig.text(
112+
font="10p,1,black",
113+
position=code,
114+
justify=code,
115+
text=code,
116+
offset="j0.3c/0.15c+v1p,gray30",
117+
)
118+
fig.plot(x=x1, y=y1, style="c0.2c", fill="steelblue", no_clip=True)
119+
120+
fig.show(width=600)
121+
```

examples/gallery/embellishments/colorbar.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
followed by the desired interval. The placement of the colorbar is set
1212
via the ``position`` parameter. There are the following options:
1313
14-
- **j/J**: placed inside/outside the plot bounding box using any 2-character
15-
combination of vertical (**T**\ op, **M**\ iddle, **B**\ ottom) and
16-
horizontal (**L**\ eft, **C**\ enter, **R**\ ight) alignment codes, e.g.
14+
- **j/J**: placed inside/outside the plot bounding box using a
15+
:doc:`2-character justification code </techref/justification_codes>`, e.g.,
1716
``position="jTR"`` for Top Right.
1817
- **g**: using map coordinates, e.g. ``position="g170/-45"`` for longitude
1918
170° East, latitude 45° South.

examples/gallery/embellishments/scalebar.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010
of the reference point. Choose from
1111
1212
- **g**: Give map coordinates as *longitude*\/\ *latitude*.
13-
- **j**\|\ **J**: Specify a two-character (order independent) code.
14-
Choose from vertical **T**\(op), **M**\(iddle), or **B**\(ottom) and
15-
horizontal **L**\(eft), **C**\(entre), or **R**\(ight). Lower /
16-
uppercase **j** / **J** mean inside / outside of the map bounding
17-
box.
13+
- **j**\|\ **J**: Specify a
14+
:doc:`2-character justification code </techref/justification_codes>`.
15+
Lower / uppercase **j** / **J** mean inside / outside of the map
16+
bounding box.
1817
- **n**: Give normalized bounding box coordinates as *nx*\/\ *ny*.
1918
- **x**: Give plot coordinates as *x*\/\ *y*.
2019
@@ -26,9 +25,8 @@
2625
**+c** is appended the middle of the map is used. Note that *slon* is only
2726
optional for projections with constant scale along parallels, e.g.,
2827
Mercator projection.
29-
- justify: **+j**. Set the anchor point. Specify a two-character (order
30-
independent) code. Choose from vertical **T**\(op), **M**\(iddle), or
31-
**B**\(ottom) and horizontal **L**\(eft), **C**\(entre), or **R**\(ight).
28+
- justify: **+j**. Set the anchor point. Specify a
29+
:doc:`2-character justification code </techref/justification_codes>`.
3230
- offset: **+o**\ *offset* or **+o**\ *xoffset*/\ *yoffset*. Give either a
3331
common shift or individual shifts in x- (longitude) and y- (latitude)
3432
directions.

examples/tutorials/basics/text.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@
3737
# * ``angle``: Specifies the rotation of the text. It is measured counter-clockwise
3838
# from the horizontal in degrees.
3939
# * ``justify``: Defines the anchor point of the bounding box for the text. It is
40-
# specified by a two-letter (order independent) code, chosen from:
41-
#
42-
# * Vertical: **T**\(op), **M**\(iddle), **B**\(ottom)
43-
# * Horizontal: **L**\(eft), **C**\(entre), **R**\(ight)
44-
#
40+
# specified by a :doc:`2-character justification code </techref/justification_codes>`.
4541
# * ``offset``: Shifts the text relatively to the reference point.
4642

4743
fig = pygmt.Figure()
@@ -191,12 +187,8 @@
191187
#
192188
# Instead of using the ``x`` and ``y`` parameters, the ``position`` parameter can be
193189
# specified to set the reference point for the text on the plot. As for the ``justify``
194-
# parameter, the ``position`` parameter is specified by a two-letter (order independent)
195-
# code, chosen from:
196-
#
197-
# * Vertical: **T**\(op), **M**\(iddle), **B**\(ottom)
198-
# * Horizontal: **L**\(eft), **C**\(entre), **R**\(ight)
199-
#
190+
# parameter, the ``position`` parameter is specified by a
191+
# :doc:`2-character justification code </techref/justification_codes>`.
200192
# This can be helpful to add a tag to a subplot or text labels out of the plot or map
201193
# frame, e.g., for depth slices.
202194

pygmt/src/colorbar.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ def colorbar(
6868
[**+n**\ [*txt*]][**+o**\ *dx*\ [/*dy*]].
6969
Define the reference point on the map for the color scale using one of
7070
four coordinate systems: (1) Use **g** for map (user) coordinates, (2)
71-
use **j** or **J** for setting *refpoint* via a 2-character
72-
justification code that refers to the (invisible) map domain rectangle,
71+
use **j** or **J** for setting *refpoint* via a
72+
:doc:`2-character justification code </techref/justification_codes>`
73+
that refers to the (invisible) map domain rectangle,
7374
(3) use **n** for normalized (0-1) coordinates, or (4) use **x** for
7475
plot coordinates (inches, cm, etc.). All but **x** requires both
7576
``region`` and ``projection`` to be specified. Append **+w** followed
@@ -78,8 +79,9 @@ def colorbar(
7879
reverse the scale bar. Append **+h** to get a horizontal scale
7980
[Default is vertical (**+v**)]. By default, the anchor point on the
8081
scale is assumed to be the bottom left corner (**BL**), but this can
81-
be changed by appending **+j** followed by a 2-character
82-
justification code *justify*.
82+
be changed by appending **+j** followed by a
83+
:doc:`2-character justification code </techref/justification_codes>`
84+
*justify*.
8385
box : bool or str
8486
[**+c**\ *clearances*][**+g**\ *fill*][**+i**\ [[*gap*/]\ *pen*]]\
8587
[**+p**\ [*pen*]][**+r**\ [*radius*]][**+s**\ [[*dx*/*dy*/][*shade*]]].

pygmt/src/inset.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def inset(
5656
5757
Append **g**\ *lon*/*lat* for map (user) coordinates,
5858
**j**\ *code* or **J**\ *code* for setting the *refpoint* via a
59-
2-character justification code that refers to the (invisible)
59+
:doc:`2-character justification code </techref/justification_codes>`
60+
that refers to the (invisible)
6061
projected map bounding box, **n**\ *xn*/*yn* for normalized (0-1)
6162
bounding box coordinates, or **x**\ *x*/*y* for plot
6263
coordinates (inches, centimeters, points, append unit).
@@ -75,8 +76,9 @@ def inset(
7576
Append **+w**\ *width*\ [/*height*] of bounding rectangle or box
7677
in plot coordinates (inches, centimeters, etc.). By default, the
7778
anchor point on the scale is assumed to be the bottom left corner
78-
(**BL**), but this can be changed by appending **+j** followed by
79-
a 2-character justification code *justify*.
79+
(**BL**), but this can be changed by appending **+j** followed by a
80+
:doc:`2-character justification code </techref/justification_codes>`
81+
*justify*.
8082
**Note**: If **j** is used then *justify* defaults to the same
8183
as *refpoint*, if **J** is used then *justify* defaults to the
8284
mirror opposite of *refpoint*. Specify inset box attributes via

pygmt/src/text.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915
9090
* *y*: Y coordinate or latitude
9191
* *angle*: Angle in degrees counter-clockwise from horizontal
9292
* *font*: Text size, font, and color
93-
* *justify*: Two-character justification code
93+
* *justify*:
94+
:doc:`2-character justification code </techref/justification_codes>`
9495
* *text*: The text string to typeset
9596
9697
The *angle*, *font*, and *justify* columns are optional and can be set
@@ -104,12 +105,8 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915
104105
position
105106
Set reference point on the map for the text by using x, y
106107
coordinates extracted from ``region`` instead of providing them
107-
through ``x``/``y``. Specify with a two-letter (order independent)
108-
code, chosen from:
109-
110-
* Vertical: **T**\ (op), **M**\ (iddle), **B**\ (ottom)
111-
* Horizontal: **L**\ (eft), **C**\ (entre), **R**\ (ight)
112-
108+
through ``x``/``y``. Specify with a
109+
:doc:`2-character justification code </techref/justification_codes>`.
113110
For example, ``position="TL"`` plots the text at the Top Left corner
114111
of the map.
115112
text
@@ -129,10 +126,9 @@ def text_( # noqa: PLR0912, PLR0913, PLR0915
129126
columns.
130127
justify
131128
Set the alignment which refers to the part of the text string that
132-
will be mapped onto the (x, y) point. Choose a two-letter
133-
combination of **L**, **C**, **R** (for left, center, or right) and
134-
**T**, **M**, **B** (for top, middle, or bottom). E.g., **BL** for
135-
bottom left. If no justification is explicitly given
129+
will be mapped onto the (x, y) point. Choose a
130+
:doc:`2-character justification code </techref/justification_codes>`,
131+
e.g., **BL** for Bottom Left. If no justification is explicitly given
136132
(i.e. ``justify=True``), then the input to ``textfiles`` must have
137133
this as a column.
138134
{projection}

0 commit comments

Comments
 (0)