Skip to content

Commit 53e24fc

Browse files
committed
Figure.wiggle: Refactor using the new alias system
1 parent b4a94d7 commit 53e24fc

File tree

2 files changed

+105
-13
lines changed

2 files changed

+105
-13
lines changed

pygmt/src/wiggle.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
from pygmt._typing import PathLike, TableLike
99
from pygmt.alias import Alias, AliasSystem
1010
from pygmt.clib import Session
11+
from pygmt.exceptions import GMTInvalidInput
1112
from pygmt.helpers import build_arg_list, deprecate_parameter, fmt_docstring, use_alias
13+
from pygmt.params import Position
1214

1315

1416
def _parse_fills(positive_fill, negative_fill):
@@ -46,7 +48,6 @@ def _parse_fills(positive_fill, negative_fill):
4648
"fillnegative", "negative_fill", "v0.18.0", remove_version="v0.20.0"
4749
)
4850
@use_alias(
49-
D="position",
5051
T="track",
5152
W="pen",
5253
Z="scale",
@@ -64,6 +65,10 @@ def wiggle( # noqa: PLR0913
6465
x=None,
6566
y=None,
6667
z=None,
68+
position: Position | None = None,
69+
length: float | str | None = None,
70+
label: str | None = None,
71+
label_alignment: Literal["left", "right"] | None = None,
6772
positive_fill=None,
6873
negative_fill=None,
6974
projection: str | None = None,
@@ -107,29 +112,34 @@ def wiggle( # noqa: PLR0913
107112
$table_classes.
108113
Use parameter ``incols`` to choose which columns are x, y, z,
109114
respectively.
110-
$projection
111-
$region
115+
position
116+
Specify the position of the vertical scale bar on the plot. See
117+
:class:`pygmt.params.Position` for more details.
118+
length
119+
Length of the vertical scale bar in data (z) units.
120+
label
121+
Set the z unit label that is used in the scale label [Default is no unit].
122+
label_alignment
123+
Set the alignment of the scale label. Choose from ``"left"`` or ``"right"``
124+
[Default is ``"left"``].
112125
scale : str or float
113126
Give anomaly scale in data-units/distance-unit. Append **c**, **i**,
114127
or **p** to indicate the distance unit (centimeters, inches, or
115128
points); if no unit is given we use the default unit that is
116129
controlled by :gmt-term:`PROJ_LENGTH_UNIT`.
117-
$frame
118-
position : str
119-
[**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\
120-
**+w**\ *length*\ [**+j**\ *justify*]\ [**+al**\|\ **r**]\
121-
[**+o**\ *dx*\ [/*dy*]][**+l**\ [*label*]].
122-
Define the reference point on the map for the vertical scale bar.
123130
positive_fill : str
124131
Set color or pattern for filling positive wiggles [Default is no fill].
125132
negative_fill : str
126133
Set color or pattern for filling negative wiggles [Default is no fill].
127134
track : str
128135
Draw track [Default is no track]. Append pen attributes to use
129136
[Default is ``"0.25p,black,solid"``].
130-
$verbose
131137
pen : str
132138
Specify outline pen attributes [Default is no outline].
139+
$projection
140+
$region
141+
$frame
142+
$verbose
133143
$binary
134144
$panel
135145
$nodata
@@ -144,9 +154,29 @@ def wiggle( # noqa: PLR0913
144154
"""
145155
self._activate_figure()
146156

157+
if isinstance(position, str) and any(
158+
v is not None for v in (length, label, label_alignment)
159+
):
160+
msg = (
161+
"Parameter 'position' is given with a raw GMT command string, and conflicts "
162+
"with parameters 'length', 'label', and 'label_alignment'."
163+
)
164+
raise GMTInvalidInput(msg)
165+
147166
_fills = _parse_fills(positive_fill, negative_fill)
148167

149168
aliasdict = AliasSystem(
169+
D=[
170+
Alias(position, name="position"),
171+
Alias(length, name="length", prefix="+w"),
172+
Alias(
173+
label_alignment,
174+
name="label_alignment",
175+
prefix="+a",
176+
mapping={"left": "l", "right": "r"},
177+
),
178+
Alias(label, name="label", prefix="+l"),
179+
],
150180
G=Alias(_fills, name="positive_fill/negative_fill"),
151181
).add_common(
152182
B=frame,

pygmt/tests/test_wiggle.py

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import numpy as np
66
import pytest
77
from pygmt import Figure
8+
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.params import Position
810

911

1012
@pytest.mark.mpl_image_compare
@@ -28,7 +30,35 @@ def test_wiggle():
2830
negative_fill="gray",
2931
pen="1.0p",
3032
track="0.5p",
31-
position="jRM+w2+lnT",
33+
position=Position("MR"),
34+
length=2,
35+
label="nT",
36+
)
37+
return fig
38+
39+
40+
@pytest.mark.mpl_image_compare(filename="test_wiggle.png")
41+
def test_wiggle_deprecated_position_syntax():
42+
"""
43+
Test the deprecated position syntax for wiggle.
44+
"""
45+
x = np.arange(-2, 2, 0.02)
46+
y = np.zeros(x.size)
47+
z = np.cos(2 * np.pi * x)
48+
49+
fig = Figure()
50+
fig.wiggle(
51+
region=[-4, 4, -1, 1],
52+
projection="X8c",
53+
x=x,
54+
y=y,
55+
z=z,
56+
scale="0.5c",
57+
positive_fill="red",
58+
negative_fill="gray",
59+
pen="1.0p",
60+
track="0.5p",
61+
position="jMR+w2+lnT",
3262
)
3363
return fig
3464

@@ -39,7 +69,6 @@ def test_wiggle_data_incols():
3969
"""
4070
Make sure that incols parameter works with input data array.
4171
"""
42-
4372
# put data into numpy array and swap x and y columns
4473
# as the use of the 'incols' parameter will reverse this action
4574
x = np.arange(-2, 2, 0.02)
@@ -58,6 +87,39 @@ def test_wiggle_data_incols():
5887
negative_fill="gray",
5988
pen="1.0p",
6089
track="0.5p",
61-
position="jRM+w2+lnT",
90+
position=Position("MR"),
91+
length=2,
92+
label="nT",
6293
)
6394
return fig
95+
96+
97+
def test_wiggle_mixed_syntax():
98+
"""
99+
Test that an error is raised when mixing new and deprecated syntax in 'position'.
100+
"""
101+
fig = Figure()
102+
x = np.arange(-2, 2, 0.02)
103+
y = np.zeros(x.size)
104+
z = np.cos(2 * np.pi * x)
105+
106+
kwargs = {
107+
"region": [-4, 4, -1, 1],
108+
"projection": "X8c",
109+
"x": x,
110+
"y": y,
111+
"z": z,
112+
"scale": "0.5c",
113+
"positive_fill": "red",
114+
"negative_fill": "gray",
115+
"pen": "1.0p",
116+
"track": "0.5p",
117+
}
118+
with pytest.raises(GMTInvalidInput):
119+
fig.wiggle(position="jMR+w2+lnT", length=2, **kwargs)
120+
121+
with pytest.raises(GMTInvalidInput):
122+
fig.wiggle(position="jMR+w2+lnT", label="nT", **kwargs)
123+
124+
with pytest.raises(GMTInvalidInput):
125+
fig.wiggle(position="jMR+w2+lnT", length_alignment="left", **kwargs)

0 commit comments

Comments
 (0)