Skip to content

Commit 294aee8

Browse files
liamtoneyseisman
andauthored
Add gallery example for grdview (#502)
* Add gallery example for grdview * Add second line before function definition * Fix incorrect method syntax * Use "meth" instead of "func" * import xarray as xr Co-authored-by: Dongdong Tian <seisman.info@gmail.com> * Finish updating to use xr Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
1 parent cda1fe8 commit 294aee8

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""
2+
Plotting a surface
3+
------------------
4+
5+
The :meth:`pygmt.Figure.grdview()` method can plot 3-D surfaces with ``surftype="s"``. Here,
6+
we supply the data as an :class:`xarray.DataArray` with the coordinate vectors ``x`` and
7+
``y`` defined. Note that the ``perspective`` argument here controls the azimuth and
8+
elevation angle of the view. We provide a list of two arguments to ``frame`` — the
9+
second argument, prepended with ``"z"``, specifies the :math:`z`-axis frame attributes.
10+
Specifying the same scale for the ``projection`` and ``zcale`` arguments ensures equal
11+
axis scaling.
12+
"""
13+
14+
import pygmt
15+
import numpy as np
16+
import xarray as xr
17+
18+
19+
# Define an interesting function of two variables, see:
20+
# https://en.wikipedia.org/wiki/Ackley_function
21+
def ackley(x, y):
22+
return (
23+
-20 * np.exp(-0.2 * np.sqrt(0.5 * (x ** 2 + y ** 2)))
24+
- np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y)))
25+
+ np.exp(1)
26+
+ 20
27+
)
28+
29+
30+
# Create gridded data
31+
INC = 0.05
32+
x = np.arange(-5, 5 + INC, INC)
33+
y = np.arange(-5, 5 + INC, INC)
34+
data = xr.DataArray(ackley(*np.meshgrid(x, y)), coords=(x, y))
35+
36+
fig = pygmt.Figure()
37+
38+
# Plot grid as a 3-D surface
39+
SCALE = 0.2 # [inches]
40+
fig.grdview(
41+
data,
42+
frame=["a5f1", "za5f1"],
43+
projection=f"x{SCALE}i",
44+
zscale=f"{SCALE}i",
45+
surftype="s",
46+
cmap="roma",
47+
perspective="135/30",
48+
)
49+
50+
fig.show()

0 commit comments

Comments
 (0)