Skip to content
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f58c83e
Add example for non-co-registered curves
yvonnefroehlich Jun 24, 2026
78fa90f
Add fig.show
yvonnefroehlich Jun 24, 2026
a856cd2
Merge branch 'main' into add-gallery-between-curve-non-co-res
yvonnefroehlich Jun 24, 2026
22cf960
Fix input data
yvonnefroehlich Jun 24, 2026
71ff233
Adjust input data
yvonnefroehlich Jun 24, 2026
01217f3
Fix code style - maybe better exception here
yvonnefroehlich Jun 24, 2026
d249e77
Merge branch 'main' into add-gallery-between-curve-non-co-res
yvonnefroehlich Jun 24, 2026
4b4a5f1
Fix typo in code
yvonnefroehlich Jun 24, 2026
545fcde
Adjust region, color and y2 curve
yvonnefroehlich Jun 24, 2026
ed0d978
Include 'legend-pen'
yvonnefroehlich Jun 24, 2026
7b12e51
Exclude line length limit locally
yvonnefroehlich Jun 24, 2026
4e6510f
Try using random numbers
yvonnefroehlich Jun 24, 2026
cb6b6fc
Change back to manual list array for y2
yvonnefroehlich Jun 24, 2026
b566876
Use complete plotting x region
yvonnefroehlich Jun 24, 2026
1ea695d
Use consistent curves
yvonnefroehlich Jun 24, 2026
208df32
Use 'legend_pen'
yvonnefroehlich Jun 25, 2026
38a2f84
Restructure example
yvonnefroehlich Jun 25, 2026
41d4ba6
Improve intro text
yvonnefroehlich Jun 25, 2026
18de8be
Fix random numbers
yvonnefroehlich Jun 25, 2026
edd0b60
Merge branch 'main' into add-gallery-between-curve-non-co-res
yvonnefroehlich Jun 25, 2026
47a1d2e
Use only one decimal digit for x3
yvonnefroehlich Jun 25, 2026
398f356
Shift and increase step of the curve for other sampling points
yvonnefroehlich Jun 25, 2026
3a60533
Increase sampling
yvonnefroehlich Jun 25, 2026
6306da1
Fix typo
yvonnefroehlich Jun 25, 2026
7a17ab0
Merge branch 'main' into add-gallery-between-curve-non-co-res
yvonnefroehlich Jun 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 52 additions & 29 deletions examples/gallery/lines/fill_areas_between_curves.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,92 @@
========================
The :meth:`pygmt.Figure.fill_between` method fills the area between two curves y1 and
y2. Different fills (colors or patterns) can be used for the areas y1 > y2 and
y1 < y2. Optionally, the curves can be drawn.
To plot an anomaly along a track use :meth:`pygmt.Figure.wiggle` and see the gallery
example :doc:`Wiggle along tracks </gallery/lines/wiggle>`.
y1 < y2. The two curves can be co-registered or have different x-coordinates. To plot
an anomaly along a track use :meth:`pygmt.Figure.wiggle` and see the gallery example
:doc:`Wiggle along tracks </gallery/lines/wiggle>`.

For filling the areas between the two curves use the ``fill`` and ``fill2`` parameters
to set different fills for the areas with y1 > y2 and y1 < y2, respectively. Use the
``label`` and ``label2`` parameters to set the corresponding legend entries. In addition
to filling the areas, we can draw the outline curves. Use the ``pen`` and ``pen2``
parameters to set different lines for the two curves y1 and y2, respectively.

"""

# %%
import numpy as np
import pygmt

# Generate some test data
x = np.arange(-10, 10.2, 0.1)
y1 = np.sin(x * 3)
x = np.arange(-10, 10.2, 0.15)
y1 = np.sin(3 * x)
y2 = np.sin(x / 2)

x3 = np.arange(-10, 10.4, 0.5) - 0.13
y3 = np.sin(x3 / 2)


# %%
# Fill the areas between the two curves. Use the ``fill`` and ``fill2`` parameters to
# set different fills for areas with y1 > y2 and y1 < y2, respectively. Use the
# ``label`` and ``label2`` parameters to set the corresponding legend entries.
# To compare a curve y1(x) to a horizontal line, pass the desired y-level to ``y2``.

fig = pygmt.Figure()
fig.basemap(region=[-10, 10, -5, 5], projection="X15c/5c", frame=True)
fig.basemap(region=[-10, 10, -3.5, 3.5], projection="X15c/5c", frame=True)

fig.fill_between(
x=x, y=y1, y2=y2, fill="orange", fill2="steelblue", label="y1(x)", label2="y2(x)"
x=x,
y=y1,
y2=0.42,
fill="p8",
fill2="p17",
pen="1p,black,solid",
pen2="1p,black,dashed",
)
fig.legend()
fig.show()


# %%
# In addition to filling the areas, we can draw the curves. Use the ``pen`` and
# ``pen2`` parameters to set different lines for the two curves y1 and y2, respectively.
# To compare two co-registered curves y1(x) and y2(x) pass a sequence with the same
# length as the inputs for ``x`` and ``y`` to ``y2``.

fig = pygmt.Figure()
fig.basemap(region=[-10, 10, -5, 5], projection="X15c/5c", frame=True)
fig.basemap(region=[-10, 10, -3.5, 3.5], projection="X15c/5c", frame=True)

fig.fill_between(
x=x,
y=y1,
y2=y2,
fill="p8",
fill2="p17",
pen="1p,black,solid",
pen2="1p,black,dashed",
x=x, y=y1, y2=y2, fill="orange", fill2="steelblue", label="y1(x)", label2="y2(x)"
)
fig.legend()
fig.show()


# %%
# To compare a curve y1 to a horizontal line, pass the desired y-level to ``y2``.
# Now, we use two non-co-registered curves, e.g., the two curves have different
# x-coordinates. For providing the x-coordinates for the second curve, use
# parameter ``x2``. Via the ``legend_pen`` parameter the appearance in the legend
# can be changed to draw the legend entries as colored lines (using the fill colors)
# instead of filled and outlined boxes.
Comment thread
yvonnefroehlich marked this conversation as resolved.

fig = pygmt.Figure()
fig.basemap(region=[-10, 10, -5, 5], projection="X15c/5c", frame=True)
fig.basemap(region=[-10, 10, -3.5, 3.5], projection="X15c/5c", frame=True)

fig.fill_between(
x=x,
y=y1,
y2=0.42,
fill="p8",
fill2="p17",
pen="1p,black,solid",
pen2="1p,black,dashed",
x2=x3,
y2=y3,
fill="orange",
fill2="steelblue",
pen="1p,darkred,solid",
pen2="1p,darkblue,solid",
label="y1(x)",
label2="y3(x3)",
legend_pen=True,
)

# Mark sampling points
fig.plot(x=x, y=y1, style="c0.1c", fill="pink")
fig.plot(x=x3, y=y3, style="c0.1c", fill="cyan")

fig.legend()
fig.show()

# sphinx_gallery_thumbnail_number = 1
# sphinx_gallery_thumbnail_number = 3
Loading