Skip to content

Commit d0ca06a

Browse files
yvonnefroehlichmichaelgrundseisman
authored
Add gallery example showing how to build an envelope around a curve (#2587)
Co-authored-by: Michael Grund <[email protected]> Co-authored-by: Dongdong Tian <[email protected]>
1 parent b32e6d7 commit d0ca06a

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

examples/gallery/lines/envelope.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
"""
2+
Envelope
3+
--------
4+
The ``close`` parameter of the :meth:`pygmt.Figure.plot` method can be
5+
used to build a symmetrical or an asymmetrical envelope. The user can
6+
give either the deviations or the bounds in y-direction. For the first
7+
case append ``"+d"`` or ``"+D"`` and for the latter case ``"+b"``.
8+
"""
9+
10+
11+
import pandas as pd
12+
import pygmt
13+
14+
# Define a pandas DataFrame with columns for x and y as well as the
15+
# lower and upper deviations
16+
df_devi = pd.DataFrame(
17+
data={
18+
"x": [1, 3, 5, 7, 9],
19+
"y": [0.5, -0.7, 0.8, -0.3, 0.1],
20+
"y_deviation_low": [0.2, 0.2, 0.3, 0.4, 0.2],
21+
"y_deviation_upp": [0.1, 0.3, 0.2, 0.4, 0.1],
22+
}
23+
)
24+
25+
# Define the same pandas DataFrame but with lower and upper bounds
26+
df_bound = pd.DataFrame(
27+
data={
28+
"x": [1, 3, 5, 7, 9],
29+
"y": [0.5, -0.7, 0.8, -0.3, 0.1],
30+
"y_bound_low": [0.3, -0.9, 0.5, -0.7, -0.1],
31+
"y_bound_upp": [0.6, -0.4, 1.1, 0.1, 0.2],
32+
}
33+
)
34+
35+
36+
# Create Figure instance
37+
fig = pygmt.Figure()
38+
39+
# -----------------------------------------------------------------------------
40+
# Left
41+
fig.basemap(
42+
region=[0, 10, -1.5, 1.5],
43+
projection="X10c",
44+
frame=["WSne+tsymmetric deviations +d", "xa2f1", "ya1f0.1"],
45+
)
46+
47+
# Plot a symmetrical envelope based on the deviations ("+d")
48+
fig.plot(
49+
data=df_devi,
50+
close="+d",
51+
# Fill the envelope in gray color with a transparency of 50 %
52+
fill="gray@50",
53+
pen="1p,gray30",
54+
)
55+
56+
# Plot the data points on top
57+
fig.plot(
58+
data=df_devi,
59+
style="c0.2c", # Use circles with a diameter of 0.2 centimeters
60+
pen="1p,gray30",
61+
fill="darkgray",
62+
)
63+
64+
# Shift plot origin 11 centimeters in x direction
65+
fig.shift_origin(xshift="11c")
66+
67+
# -----------------------------------------------------------------------------
68+
# Middle
69+
fig.basemap(
70+
region=[0, 10, -1.5, 1.5],
71+
projection="X10c",
72+
frame=["WSne+tasymmetric deviations +D", "xa2f1", "yf0.1"],
73+
)
74+
75+
# Plot an asymmetrical envelope based on the deviations ("+D")
76+
fig.plot(
77+
data=df_devi,
78+
fill="gray@50",
79+
# Add an outline around the envelope
80+
# Here, a dashed pen ("+p") with 0.5-points thickness and
81+
# "gray30" color is used
82+
close="+D+p0.5p,gray30,dashed",
83+
pen="1p,gray30",
84+
)
85+
86+
# Plot the data points on top
87+
fig.plot(data=df_devi, style="c0.2c", pen="1p,gray30", fill="darkgray")
88+
89+
# Shift plot origin 11 centimeters in x-direction
90+
fig.shift_origin(xshift="11c")
91+
92+
# -----------------------------------------------------------------------------
93+
# Right
94+
fig.basemap(
95+
region=[0, 10, -1.5, 1.5],
96+
projection="X10c",
97+
# Use "\\053" to handle "+b" as a string not as a modifier
98+
frame=["wSnE+tbounds \\053b", "xa2f1", "ya1f0.1"],
99+
)
100+
101+
# Plot an envelope based on the bounds ("+b")
102+
fig.plot(data=df_bound, close="+b+p0.5p,gray30,dashed", pen="1p,gray30")
103+
104+
# Plot the data points on top
105+
fig.plot(data=df_bound, style="c0.2c", pen="1p,gray30", fill="darkgray")
106+
107+
fig.show()

0 commit comments

Comments
 (0)