|
| 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