Skip to content

Commit 97f015f

Browse files
committed
Add tests and improve docstrings
1 parent 539f66f commit 97f015f

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

pygmt/params/position.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,40 @@ class Position(BaseParam):
1717
The class for positioning GMT embellishments.
1818
"""
1919

20-
location: str | tuple[float | str, float | str]
20+
#: Specify the reference point on the plot. The method of defining the reference
21+
#: point is controlled by ``type``, and the exact location is set by ``position``.
22+
location: Sequence[float | str] | AnchorCode
23+
24+
#: Specify the type of coordinates used to define the reference point. It can be
25+
#: one of the following values:
26+
#:
27+
#: - ``"mapcoords"``: ``position`` is specified as (*longitude*, *latitude*) in map
28+
#: coordinates.
29+
#: - ``"boxcoords"``: ``position`` is specified as (*nx*, *ny*) in normalized
30+
#: coordinates, i.e., fractional values between 0 and 1 along the x- and y-axes.
31+
#: - ``"plotcoords"``: ``position`` is specified as (*x*, *y*) in plot coordinates,
32+
#: i.e., distances from the lower-left plot origin given in inches, centimeters,
33+
#: or points.
34+
#: - ``"inside"`` or ``"outside"``: ``position`` is one of the nine
35+
#: :doc:`two-character justification codes </techref/justification_codes>`,
36+
#: indicating a specific location relative to the plot bounding box.
37+
#:
2138
type: Literal["mapcoords", "inside", "outside", "boxcoords", "plotcoords"]
22-
anchor: AnchorCode
23-
offset: Sequence[float | str]
39+
40+
#: Specify the anchor point of the GMT logo, using one of the
41+
#: :doc:`2-character justification codes </techref/justification_codes>`. The
42+
#: default value depends on ``position_type``.
43+
#:
44+
#: - ``position_type="inside"``: ``anchor`` defaults to the same as ``position``.
45+
#: - ``position_type="outside"``: ``anchor`` defaults to the mirror opposite of
46+
#: ``position``.
47+
#: - Otherwise, ``anchor`` defaults to ``"MC"`` (middle center).
48+
anchor: AnchorCode | None = None
49+
50+
#: Specifies an offset for the anchor point as *offset* or (*offset_x*, *offset_y*).
51+
#: If a single value *offset* is given, both *offset_x* and *offset_y* are set to
52+
#: *offset*.
53+
offset: Sequence[float | str] | None = None
2454

2555
@property
2656
def _aliases(self):
@@ -37,6 +67,6 @@ def _aliases(self):
3767
},
3868
),
3969
Alias(self.location, name="location", sep="/", size=2),
40-
Alias(self.anchor, name="anchor"),
41-
Alias(self.offset, name="offset", sep="/", size=2),
70+
Alias(self.anchor, name="anchor", prefix="+j"),
71+
Alias(self.offset, name="offset", prefix="+o", sep="/", size=2),
4272
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Test the Position class.
3+
"""
4+
5+
from pygmt.params import Position
6+
7+
8+
def test_params_position_types():
9+
"""
10+
Test the Position class with different types of coordinate systems.
11+
"""
12+
assert str(Position(location=(10, 20), type="mapcoords")) == "g10/20"
13+
assert str(Position(location=(0.1, 0.2), type="boxcoords")) == "n0.1/0.2"
14+
assert str(Position(location=("5c", "3c"), type="plotcoords")) == "x5c/3c"
15+
assert str(Position(location="TL", type="inside")) == "jTL"
16+
assert str(Position(location="BR", type="outside")) == "JBR"
17+
18+
19+
def test_params_position_anchor_offset():
20+
"""
21+
Test the Position class with anchor and offset parameters.
22+
"""
23+
pos = Position(location=(10, 20), type="mapcoords", anchor="TL")
24+
assert str(pos) == "g10/20+jTL"
25+
26+
pos = Position(location=(10, 20), type="mapcoords", offset=(1, 2))
27+
assert str(pos) == "g10/20+o1/2"
28+
29+
pos = Position(location="TL", type="inside", anchor="MC", offset=("1c", "2c"))
30+
assert str(pos) == "jTL+jMC+o1c/2c"

0 commit comments

Comments
 (0)