Skip to content

Commit 952e754

Browse files
willschlitzerseismanweiji14
authored
Add intro tutorial section for creating contour map (#2126)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 8fa1a97 commit 952e754

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

examples/get-started/contour-map.py

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
"""
2+
2. Create a contour map
3+
=======================
4+
5+
This tutorial page covers the basics of creating a figure of Earth relief,
6+
using a remote dataset hosted by GMT, using the method
7+
:meth:`pygmt.datasets.load_earth_relief`. It will use
8+
:meth:`pygmt.Figure.grdimage`, :meth:`pygmt.Figure.grdcontour`,
9+
:meth:`pygmt.Figure.colorbar`, and :meth:`pygmt.Figure.coast` methods for
10+
plotting.
11+
"""
12+
13+
# sphinx_gallery_thumbnail_number = 4
14+
import pygmt
15+
16+
###############################################################################
17+
# Loading the Earth relief dataset
18+
# --------------------------------
19+
#
20+
# The first step is to use :meth:`pygmt.datasets.load_earth_relief`.
21+
# The ``resolution`` parameter sets the resolution of the remote grid file,
22+
# which will affect the resolution of the plot made later in the tutorial.
23+
# The ``registration`` parameter determines the grid registration.
24+
#
25+
# This grid region covers the islands of Guam and Rota in the western Pacific
26+
# Ocean.
27+
28+
grid = pygmt.datasets.load_earth_relief(
29+
resolution="30s", region=[144.5, 145.5, 13, 14.5], registration="gridline"
30+
)
31+
32+
###############################################################################
33+
# Plotting Earth relief
34+
# ---------------------
35+
#
36+
# To plot Earth relief data, the method :meth:`pygmt.Figure.grdimage` can be
37+
# used to plot a color-coded figure to display the topography and bathymetry
38+
# in the grid file. The ``grid`` parameter accepts the input grid, which in
39+
# this case is the remote file downloaded in the previous step. If the
40+
# ``region`` parameter is not set, the region boundaries of the input grid are
41+
# used.
42+
#
43+
# The ``cmap`` parameter sets the color palette table (CPT) used for
44+
# portraying Earth relief. The :meth:`pygmt.Figure.grdimage` method uses the
45+
# input grid to apply Earth relief values to a specific color within the CPT.
46+
# In this case, the CPT used is "oleron"; a full list of CPTs can be found
47+
# at :gmt-docs:`cookbook/cpts.html`.
48+
49+
fig = pygmt.Figure()
50+
fig.grdimage(grid=grid, frame="a", projection="M10c", cmap="oleron")
51+
fig.show()
52+
53+
###############################################################################
54+
# Adding a colorbar
55+
# -----------------
56+
#
57+
# To show how the plotted colors relate to the Earth relief, a colorbar can be
58+
# added using the :meth:`pygmt.Figure.colorbar` method.
59+
#
60+
# To control the labels on the colorbar, a list is passed to the ``frame``
61+
# parameter. The value beginning with "a" sets the interval for annotation on
62+
# the colorbar, in this case every 1,000 meters. To set the label for an axis
63+
# on the colorbar, the value begins with either "x+l" (x-axis) or "y+l"
64+
# (y-axis), followed by the intended label.
65+
#
66+
# By default, the CPT for the colorbar is the same as the one set
67+
# in :meth:`pygmt.Figure.grdimage`.
68+
69+
fig = pygmt.Figure()
70+
fig.grdimage(grid=grid, frame="a", projection="M10c", cmap="oleron")
71+
fig.colorbar(frame=["a1000", "x+lElevation", "y+lm"])
72+
fig.show()
73+
74+
###############################################################################
75+
# Adding contour lines
76+
# --------------------
77+
#
78+
# To add contour lines to the color coded figured, the
79+
# :meth:`pygmt.Figure.grdcontour` method is used. The ``frame`` and
80+
# ``projection`` are already set using :meth:`pygmt.Figure.grdimage` and are
81+
# not needed again. However, the same input for ``grid`` (in this case, the
82+
# variable named "grid") must be input again. The ``interval`` parameter sets
83+
# the spacing between lines (in this case, 500 meters), and the ``annotation``
84+
# parameter draws darker lines that are annotated with the elevation or
85+
# bathymetry.
86+
87+
fig = pygmt.Figure()
88+
fig.grdimage(grid=grid, frame="a", projection="M10c", cmap="oleron")
89+
fig.grdcontour(grid=grid, interval=500, annotation=1000)
90+
fig.colorbar(frame=["a1000", "x+lElevation (m)"])
91+
fig.show()
92+
93+
###############################################################################
94+
# Color in land
95+
# -------------
96+
#
97+
# To make it clear where the islands are located, the
98+
# :meth:`pygmt.Figure.coast` method can be used to color in the land. The
99+
# ``land`` is colored in as "lightgray", and the ``shorelines`` parameters
100+
# draws a border around the islands.
101+
102+
fig = pygmt.Figure()
103+
fig.grdimage(grid=grid, frame="a", projection="M10c", cmap="oleron")
104+
fig.grdcontour(grid=grid, interval=500, annotation=1000)
105+
fig.coast(shorelines="2p", land="lightgray")
106+
fig.colorbar(frame=["a1000", "x+lElevation (m)"])
107+
fig.show()
108+
109+
###############################################################################
110+
# Additional exercises
111+
# --------------------
112+
#
113+
# This is the end of the second tutorial. Here are some additional exercises
114+
# for the concepts that were discussed:
115+
#
116+
# 1. Change the resolution of the grid file to either "01m" (1 arc-minute, a
117+
# lower resolution) or "15s" (15 arc-seconds, a higher resolution). Note
118+
# that higher resolution grids will have larger file sizes. Available
119+
# resolutions can be found `here
120+
# <https://www.generic-mapping-tools.org/
121+
# remote-datasets/earth-relief.html#usage>`_.
122+
#
123+
# 2. Create a contour map of the area around Mt. Rainier. A suggestion for the
124+
# ``region`` would be ``[-122, -121, 46.5, 47.5]``. Adjust the
125+
# :meth:`pygmt.Figure.grdcontour` and :meth:`pygmt.Figure.colorbar`
126+
# settings as needed to make the figure look good.
127+
#
128+
# 3. Create a contour map of São Miguel Island in the Azores; a suggested
129+
# ``region`` is ``[-26, -25, 37.5, 38]``. Instead of coloring in ``land``,
130+
# set ``water`` to "lightblue" to only display Earth relief information for
131+
# the land.
132+
#
133+
# 4. Try other CPTs, such as "SCM/fes" or "geo".

pygmt/helpers/testing.py

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def download_test_data():
169169
"@N35E135.earth_relief_03s_g.nc",
170170
"@N37W120.earth_relief_03s_g.nc",
171171
"@N00W090.earth_relief_03m_p.nc",
172+
"@N00E135.earth_relief_30s_g.nc",
172173
# Earth seafloor age grids
173174
"@earth_age_01d_g",
174175
"@S90W180.earth_age_05m_g.nc", # Specific grid for 05m test

0 commit comments

Comments
 (0)