Skip to content

Commit 2c14fbd

Browse files
hemmeligseismanweiji14
authored
Add tutorial for pygmt.config (#482)
Add tutorial detailing how the pygmt.config class can be used to override GMT default parameters, both permanently (globally) or temporarily (locally). Follows the style of the other tutorials available. Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 66a9f98 commit 2c14fbd

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
tutorials/coastlines.rst
3434
tutorials/plot.rst
3535
tutorials/text.rst
36+
tutorials/configuration.rst
3637

3738
.. toctree::
3839
:maxdepth: 2

examples/tutorials/configuration.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Configuring PyGMT defaults
3+
==========================
4+
5+
Default GMT parameters can be set globally or locally using :class:`pygmt.config`.
6+
"""
7+
8+
import pygmt
9+
10+
########################################################################################
11+
# Configuring default GMT parameters
12+
# ----------------------------------
13+
#
14+
# Users can override default parameters either temporarily (locally) or permanently
15+
# (globally) using :meth:`pygmt.config`. The full list of default parameters that can be
16+
# changed can be found at :gmt-docs:`gmt.conf.html`.
17+
#
18+
# We demonstrate the usage of :meth:`pygmt.config` by configuring a map plot.
19+
20+
# Start with a basic figure with the default style
21+
fig = pygmt.Figure()
22+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
23+
fig.coast(land="black", water="skyblue")
24+
25+
fig.show()
26+
27+
########################################################################################
28+
# Globally overriding defaults
29+
# ----------------------------
30+
#
31+
# The ``MAP_FRAME_TYPE`` parameter specifies the style of map frame to use, of which there
32+
# are 5 options: ``fancy`` (default, seen above), ``fancy+``, ``plain``, ``graph``
33+
# (which does not apply to geographical maps) and ``inside``.
34+
#
35+
# The ``FORMAT_GEO_MAP`` parameter controls the format of geographical tick annotations.
36+
# The default uses degrees and minutes. Here we specify the ticks to be a decimal number
37+
# of degrees.
38+
39+
fig = pygmt.Figure()
40+
41+
# Configuration for the 'current figure'.
42+
pygmt.config(MAP_FRAME_TYPE="plain")
43+
pygmt.config(FORMAT_GEO_MAP="ddd.xx")
44+
45+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
46+
fig.coast(land="black", water="skyblue")
47+
48+
fig.show()
49+
50+
########################################################################################
51+
# Locally overriding defaults
52+
# ---------------------------
53+
#
54+
# It is also possible to temporarily override the default parameters, which is very
55+
# useful for limiting the scope of changes to a particular plot. :class:`pygmt.config` is
56+
# implemented as a context manager, which handles the setup and teardown of a GMT
57+
# session. Python users are likely familiar with the ``with open(...) as file:`` snippet,
58+
# which returns a ``file`` context manager. In this way, it can be used to override a parameter
59+
# for a single command, or a sequence of commands. An application of :class:`pygmt.config`
60+
# as a context manager is shown below:
61+
62+
fig = pygmt.Figure()
63+
64+
# This will have a fancy+ frame
65+
with pygmt.config(MAP_FRAME_TYPE="fancy+"):
66+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
67+
fig.coast(land="black", water="skyblue")
68+
69+
# Shift plot origin down by 10cm to plot another map
70+
fig.shift_origin(yshift="-10c")
71+
72+
# This figure retains the default "fancy" frame
73+
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
74+
fig.coast(land="black", water="skyblue")
75+
76+
fig.show()

0 commit comments

Comments
 (0)