Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tutorial for pygmt.config #482

Merged
merged 13 commits into from
Oct 29, 2020
1 change: 1 addition & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
projections/index.rst
tutorials/coastlines.rst
tutorials/plot.rst
tutorials/configuration.rst

.. toctree::
:maxdepth: 2
Expand Down
78 changes: 78 additions & 0 deletions examples/tutorials/configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""
Configuring PyGMT defaults
==========================

Default GMT parameters can be set globally or locally using :class:`pygmt.config`
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
"""

import pygmt

########################################################################################
# Configuring default GMT parameters
# ----------------------------------
#
# The user can override default parameters either temporarily (locally) or permanently
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
# (globally) using :meth:`pygmt.config`. The full list of default parameters that can be
# changed can be at :gmt-docs:`gmt.conf.html`.
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
#
# We demonstrate the usage of :meth:`pygmt.config` by configuring a map plot.

# Start with a basic figure
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
fig = pygmt.Figure()
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
fig.coast(land="black", water="skyblue")

fig.show()

########################################################################################
# Globally overriding defaults
# ----------------------------
#
# The ``MAP_FRAME_TYPE`` parameter specifies the style of map frame to use, of which there
# are 3 options: ``fancy`` (default, seen above), ``plain``, and ``inside``.
seisman marked this conversation as resolved.
Show resolved Hide resolved
#
# The ``FORMAT_GEO_MAP`` parameter controls the format of geographical tick annotations.
# The default uses degrees and minutes. Here we specify the ticks to be a decimal number
# of degrees.

fig = pygmt.Figure()

# Configuration for the 'current figure'.
pygmt.config(MAP_FRAME_TYPE="plain")
pygmt.config(FORMAT_GEO_MAP="ddd.xx")

fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
fig.coast(land="black", water="skyblue")

fig.show()

########################################################################################
# Locally overriding defaults
# ---------------------------
#
# It is also possible to temporarily override the default parameters, which is a very
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
# useful for limiting the scope of changes to a particular plot. :class:`pygmt.config` is
# implemented as a context manager, which handles the setup and teardown of a GMT
# session. Python users are likely familiar with the `with open(...) as file:` snippet,
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
# which returns a `file` context manager. In this way, it can be used to override a parameter
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
# for a single command, or a sequence of commands. An application of `pygmt.config` as a context
hemmelig marked this conversation as resolved.
Show resolved Hide resolved
# manager is shown below:

fig = pygmt.Figure()

# This will have a fancy frame
with pygmt.config(MAP_FRAME_TYPE="fancy"):
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
fig.coast(land="black", water="skyblue")

# This figure retains the globally set plain frame
seisman marked this conversation as resolved.
Show resolved Hide resolved
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", Y="-10c", frame=True)
fig.coast(land="black", water="skyblue")
seisman marked this conversation as resolved.
Show resolved Hide resolved

# Set font for a sequence of commands
with pygmt.config(FONT="14p,Helvetica-Bold,white"):
fig.text(text="Mt Kinabalu", x=116.549, y=6.058)
fig.text(text="Maliau Basin", x=116.913, y=4.787)
fig.text(text="Danum Valley", x=117.743, y=4.912)
seisman marked this conversation as resolved.
Show resolved Hide resolved

fig.show()