Skip to content

Commit 9b60f5c

Browse files
yohaimagenMeghan Joneswillschlitzer
authored
A gallery example for the contour method (#1387)
* Initial commit for contour gallery example * Explain the acceptable data formats, levels, and annotation parameters. * Add a bit more explanation on the implementation, adding a reference to numpy ndarray, adding units to pen. Co-authored-by: Meghan Jones <[email protected]> Co-authored-by: Will Schlitzer <[email protected]>
1 parent 9efe317 commit 9b60f5c

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Contours
3+
--------
4+
The :meth:`pygmt.Figure.contour` method can plot contour lines from a table of points by direct triangulation.
5+
The data for the triangulation can be provided using one of three methods:
6+
7+
#. ``x``, ``y``, ``z`` 1d :class:`numpy.ndarray` data columns.
8+
#. ``data`` 2d :class:`numpy.ndarray` data matrix with 3 columns corresponding
9+
to ``x``, ``y``, ``z``.
10+
#. ``data`` path string to a file containing the ``x``, ``y``, ``z`` in a
11+
tabular format.
12+
13+
The parameters ``levels`` and ``annotation`` set the intervals of the contours and the
14+
annotation on the contours respectively.
15+
16+
In this example we supply the data as 1d :class:`numpy.ndarray` with the ``x``, ``y``,
17+
and ``z`` parameters and draw the contours using a 0.5p pen with contours every 10 ``z`` values and
18+
annotations every 20 ``z`` values.
19+
"""
20+
21+
22+
import numpy as np
23+
import pygmt
24+
25+
# build the contours underlying data with the function z = x^2 + y^2
26+
X, Y = np.meshgrid(np.linspace(-10, 10, 50), np.linspace(-10, 10, 50))
27+
Z = X ** 2 + Y ** 2
28+
x, y, z = X.flatten(), Y.flatten(), Z.flatten()
29+
30+
31+
fig = pygmt.Figure()
32+
fig.contour(
33+
region=[-10, 10, -10, 10],
34+
projection="X10c/10c",
35+
frame="ag",
36+
pen="0.5p",
37+
# pass the data as 3 1d data columns
38+
x=x,
39+
y=y,
40+
z=z,
41+
# set the contours z values intervals to 10
42+
levels=10,
43+
# set the contours annotation intervals to 20
44+
annotation=20,
45+
)
46+
fig.show()

0 commit comments

Comments
 (0)