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 @@ -33,6 +33,7 @@
tutorials/coastlines.rst
tutorials/plot.rst
tutorials/text.rst
tutorials/configuration.rst

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

Default GMT parameters can be set globally or locally using :class:`pygmt.config`.
"""

import pygmt

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

# Start with a basic figure with the default style
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 5 options: ``fancy`` (default, seen above), ``fancy+``, ``plain``, ``graph``
# (which does not apply to geographical maps) and ``inside``.
#
# 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 very
# 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,
# which returns a ``file`` context manager. In this way, it can be used to override a parameter
# for a single command, or a sequence of commands. An application of :class:`pygmt.config`
# as a context 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")

# Shift plot origin down by 10cm to plot another map
fig.shift_origin(yshift="-10c")

# This figure retains the default "fancy" frame
fig.basemap(region=[115, 119.5, 4, 7.5], projection="M10c", frame=True)
fig.coast(land="black", water="skyblue")

fig.show()