Skip to content

Commit 9fb1cec

Browse files
core-manweiji14seismanmichaelgrund
authored
Wrap wiggle (#1145)
Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Michael Grund <[email protected]>
1 parent b2fd391 commit 9fb1cec

File tree

7 files changed

+171
-0
lines changed

7 files changed

+171
-0
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Plotting data and laying out the map:
4242
Figure.solar
4343
Figure.subplot
4444
Figure.text
45+
Figure.wiggle
4546

4647
Color palette table generation:
4748

examples/gallery/lines/wiggle.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Wiggle along tracks
3+
-------------------
4+
5+
The :meth:`pygmt.Figure.wiggle` method can plot z = f(x,y) anomalies along
6+
tracks. ``x``, ``y``, ``z`` can be specified as 1d arrays or within a specified
7+
file. The ``scale`` parameter can be used to set the scale of the anomaly in
8+
data/distance units. The positive and/or negative areas can be filled with
9+
color by setting the ``color`` parameter.
10+
"""
11+
12+
import numpy as np
13+
import pygmt
14+
15+
# Create (x, y, z) triplets
16+
x = np.arange(-7, 7, 0.1)
17+
y = np.zeros(x.size)
18+
z = 50 * np.exp(-((x / 3) ** 2)) * np.cos(2 * np.pi * x)
19+
20+
fig = pygmt.Figure()
21+
fig.basemap(region=[-8, 12, -1, 1], projection="X10c", frame=["Snlr", "xa2f1"])
22+
fig.wiggle(
23+
x=x,
24+
y=y,
25+
z=z,
26+
# Set anomaly scale to "20c"
27+
scale="20c",
28+
# Fill positive and negative areas red and gray, respectively
29+
color=["red+p", "gray+n"],
30+
# Set the outline width to "1.0p"
31+
pen="1.0p",
32+
# Draw a blue track with a width of 0.5 points
33+
track="0.5p,blue",
34+
# Plot a vertical scale bar at the right middle. The bar length is 100 in
35+
# data (z) units. Set the z unit label to "nT".
36+
position="jRM+w100+lnT",
37+
)
38+
fig.show()

pygmt/figure.py

+1
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ def _repr_html_(self):
423423
solar,
424424
subplot,
425425
text,
426+
wiggle,
426427
)
427428

428429

pygmt/src/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
from pygmt.src.surface import surface
3333
from pygmt.src.text import text_ as text # "text" is an argument within "text_"
3434
from pygmt.src.which import which
35+
from pygmt.src.wiggle import wiggle
3536
from pygmt.src.x2sys_cross import x2sys_cross
3637
from pygmt.src.x2sys_init import x2sys_init

pygmt/src/wiggle.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
wiggle - Plot z=f(x,y) anomalies along tracks.
3+
"""
4+
from pygmt.clib import Session
5+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
6+
7+
8+
@fmt_docstring
9+
@use_alias(
10+
B="frame",
11+
D="position",
12+
G="color",
13+
J="projection",
14+
R="region",
15+
T="track",
16+
U="timestamp",
17+
V="verbose",
18+
W="pen",
19+
X="xshift",
20+
Y="yshift",
21+
Z="scale",
22+
c="panel",
23+
i="columns",
24+
p="perspective",
25+
)
26+
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
27+
def wiggle(self, x=None, y=None, z=None, data=None, **kwargs):
28+
r"""
29+
Plot z=f(x,y) anomalies along tracks.
30+
31+
Takes a matrix, (x,y,z) triplets, or a file name as input and plots z as a
32+
function of distance along track.
33+
34+
Must provide either ``data`` or ``x``/``y``/``z``.
35+
36+
Full parameter list at :gmt-docs:`wiggle.html`
37+
38+
{aliases}
39+
40+
Parameters
41+
----------
42+
x/y/z : 1d arrays
43+
The arrays of x and y coordinates and z data points.
44+
data : str or 2d array
45+
Either a data file name or a 2d numpy array with the tabular data.
46+
Use parameter ``columns`` to choose which columns are x, y, z,
47+
respectively.
48+
{J}
49+
{R}
50+
scale : str or float
51+
Gives anomaly scale in data-units/distance-unit. Append **c**, **i**,
52+
or **p** to indicate the distance unit (cm, inch, or point); if no unit
53+
is given we use the default unit that is controlled by
54+
:gmt-term:`PROJ_LENGTH_UNIT`.
55+
{B}
56+
position : str
57+
[**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\
58+
**+w**\ *length*\ [**+j**\ *justify*]\ [**+al**\ |\ **r**]\
59+
[**+o**\ *dx*\ [/*dy*]][**+l**\ [*label*]].
60+
Defines the reference point on the map for the vertical scale bar.
61+
color : str
62+
Set fill shade, color or pattern for positive and/or negative wiggles
63+
[Default is no fill]. Optionally, append **+p** to fill positive areas
64+
(this is the default behavior). Append **+n** to fill negative areas.
65+
Append **+n+p** to fill both positive and negative areas with the same
66+
fill. Note: You will need to repeat the color parameter to select
67+
different fills for the positive and negative wiggles.
68+
69+
track : str
70+
Draw track [Default is no track]. Append pen attributes to use
71+
[Default is **0.25p,black,solid**].
72+
{U}
73+
{V}
74+
pen : str
75+
Specify outline pen attributes [Default is no outline].
76+
{XY}
77+
{c}
78+
columns : str or 1d array
79+
Choose which columns are x, y, and z, respectively if input is provided
80+
via *data*. E.g. ``columns = [0, 1, 2]`` or ``columns = "0,1,2"`` if
81+
the *x* values are stored in the first column, *y* values in the second
82+
one and *z* values in the third one. Note: zero-based indexing is used.
83+
{p}
84+
"""
85+
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
86+
87+
with Session() as lib:
88+
# Choose how data will be passed in to the module
89+
file_context = lib.virtualfile_from_data(
90+
check_kind="vector", data=data, x=x, y=y, z=z
91+
)
92+
93+
with file_context as fname:
94+
arg_str = " ".join([fname, build_arg_string(kwargs)])
95+
lib.call_module("wiggle", arg_str)
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 5ac12f7e91127dae1fa70cec77b84c0b
3+
size: 9144
4+
path: test_wiggle.png

pygmt/tests/test_wiggle.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Tests wiggle.
3+
"""
4+
import numpy as np
5+
import pytest
6+
from pygmt import Figure
7+
8+
9+
@pytest.mark.mpl_image_compare
10+
def test_wiggle():
11+
"""
12+
Plot the z=f(x,y) anomalies along tracks.
13+
"""
14+
x = np.arange(-2, 2, 0.02)
15+
y = np.zeros(x.size)
16+
z = np.cos(2 * np.pi * x)
17+
18+
fig = Figure()
19+
fig.wiggle(
20+
region=[-4, 4, -1, 1],
21+
projection="X8c",
22+
x=x,
23+
y=y,
24+
z=z,
25+
scale="0.5c",
26+
color=["red+p", "gray+n"],
27+
pen="1.0p",
28+
track="0.5p",
29+
position="jRM+w2+lnT",
30+
)
31+
return fig

0 commit comments

Comments
 (0)