Skip to content

Commit 61590b0

Browse files
yvonnefroehlichmichaelgrundseismanweiji14
authored
Add gallery example "Cross-section along a transect" (#2515)
Co-authored-by: Michael Grund <[email protected]> Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 691f1d4 commit 61590b0

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
"""
2+
Cross-section along a transect
3+
==============================
4+
:func:`pygmt.project` and :func:`pygmt.grdtrack` can be used to focus on
5+
a quantity and its variation along a desired survey line.
6+
In this example, the elevation is extracted from a grid provided via
7+
:func:`pygmt.datasets.load_earth_relief`.
8+
The figure consists of two parts, a map of the elevation in the study
9+
area showing the survey line and a Cartesian plot showing the elevation
10+
along the survey line.
11+
12+
*This example is orientated on an example in the GMT/China documentation*:
13+
https://docs.gmt-china.org/latest/examples/ex026/
14+
"""
15+
16+
import pygmt
17+
18+
# Define region of study area
19+
# lon_min, lon_max, lat_min, lat_max in degrees East and North
20+
region_map = [122, 149, 30, 49]
21+
22+
# Create a new pygmt.Figure instance
23+
fig = pygmt.Figure()
24+
25+
# ----------------------------------------------------------------------------
26+
# Bottom: Map of elevation in study area
27+
28+
# Set up basic map
29+
fig.basemap(
30+
region=region_map,
31+
projection="M12c", # Mercator projection with a width of 12 centimeters
32+
frame="af",
33+
)
34+
35+
# Download grid for Earth relief with a resolution of 10 arc-minutes and
36+
# gridline registration [Default]
37+
grid_map = pygmt.datasets.load_earth_relief(
38+
resolution="10m",
39+
region=region_map,
40+
)
41+
42+
# Plot the downloaded grid with color-coding based on the elevation
43+
fig.grdimage(grid=grid_map, cmap="oleron")
44+
45+
# Add a colorbar for the elevation
46+
fig.colorbar(
47+
# Place the colorbar inside the plot (lower-case "j") with justification
48+
# Bottom Right and an offset ("+o") of 0.7 centimeters and
49+
# 0.3 centimeters in x or y directions, respectively
50+
# Move the x label above the horizontal colorbar ("+ml")
51+
position="jBR+o0.7c/0.8c+h+w5c/0.3c+ml",
52+
# Add a box around the colobar with a fill ("+g") in "white" color and
53+
# a transparency ("@") of 30 % and with a 0.8-points thick black
54+
# outline ("+p")
55+
box="+gwhite@30+p0.8p,black",
56+
# Add x and y labels ("+l")
57+
frame=["x+lElevation", "y+lm"],
58+
)
59+
60+
# Choose a survey line
61+
fig.plot(
62+
x=[126, 146], # Longitude in degrees East
63+
y=[42, 40], # Latitude in degrees North
64+
# Draw a 2-points thick red dashed line for the survey line
65+
pen="2p,red,dashed",
66+
)
67+
68+
# Add labels "A" and "B" for the start and end points of the survey line
69+
fig.text(
70+
x=[126, 146],
71+
y=[42, 40],
72+
text=["A", "B"],
73+
offset="0c/0.2c", # Move text 0.2 centimeters up (y direction)
74+
font="15p", # Use a font size of 15 points
75+
)
76+
77+
# ----------------------------------------------------------------------------
78+
# Top: Elevation along survey line
79+
80+
# Shift plot origin 12.5 centimeters to the top
81+
fig.shift_origin(yshift="12.5c")
82+
83+
fig.basemap(
84+
region=[0, 15, -8000, 6000], # x_min, x_max, y_min, y_max
85+
# Cartesian projection with a width of 12 centimeters and
86+
# a height of 3 centimeters
87+
projection="X12c/3c",
88+
# Add annotations ("a") and ticks ("f") as well as labels ("+l")
89+
# at the west or left and south or bottom sides ("WSrt")
90+
frame=["WSrt", "xa2f1+lDistance+u°", "ya4000+lElevation / m"],
91+
)
92+
93+
# Add labels "A" and "B" for the start and end points of the survey line
94+
fig.text(
95+
x=[0, 15],
96+
y=[7000, 7000],
97+
text=["A", "B"],
98+
no_clip=True, # Do not clip text that fall outside the plot bounds
99+
font="10p", # Use a font size of 10 points
100+
)
101+
102+
# Generate points along a great circle corresponding to the survey line
103+
# and store them in a pandas.DataFrame
104+
track_df = pygmt.project(
105+
center="126/42", # Start point of survey line (longitude/latitude)
106+
endpoint="146/40", # End point of survey line (longitude/latitude)
107+
generate="0.1", # Output data in steps of 0.1 degrees
108+
)
109+
110+
# Extract the elevation at the generated points from the downloaded grid
111+
# and add it as new column "elevation" to the pandas.DataFrame
112+
track_df = pygmt.grdtrack(
113+
grid=grid_map,
114+
points=track_df,
115+
newcolname="elevation",
116+
)
117+
118+
# Plot water masses
119+
fig.plot(
120+
x=[0, 15],
121+
y=[0, 0],
122+
fill="lightblue", # Fill the polygon in "lightblue"
123+
# Draw a 0.25-points thick black solid outline
124+
pen="0.25p,black,solid",
125+
close="+y-8000", # Force closed polygon
126+
)
127+
128+
# Plot elevation along the survey line
129+
fig.plot(
130+
x=track_df.p,
131+
y=track_df.elevation,
132+
fill="gray", # Fill the polygon in "gray"
133+
# Draw a 1-point thick black solid outline
134+
pen="1p,black,solid",
135+
close="+y-8000", # Force closed polygon
136+
)
137+
138+
fig.show()

0 commit comments

Comments
 (0)