First Steps with GMT/Python¶
This tutorial will get you started with the basic usage of GMT/Python. Some of the examples shown here are from the GMT Tutorial.
Loading the library¶
The GMT modules are available as functions and classes in the
Python package. So we’ll start by importing it:
/home/travis/miniconda/envs/testing/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88 return f(*args, **kwds) /home/travis/miniconda/envs/testing/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.dtype size changed, may indicate binary incompatibility. Expected 96, got 88 return f(*args, **kwds)
Our first map¶
All figure generation in GMT/Python is handled by the
class. It has methods to add layers to your figure, like a basemap,
coastlines, and data.
We start a new figure by creating an instance of
fig = gmt.Figure()
We add elements to the figure using its methods. For example, lets add
the coastlines of Central America to a 6 inch wide map using the
Mercator projection (
M). Our figure will also have a nice frame with
fig.coast(region=[-90, -70, 0, 20], projection='M6i', land='chocolate', frame=True)
You can see a preview of the figure directly in the Jupyter
To open a PDF preview of the figure using your default PDF viewer use:
This is useful when using the Python shell or IPython terminal app.
However, this command will not interrupt your Python process. So
using it in a Python script will not work because the script will finish
and delete the generated previews. Use
fig.savefig to save the
figure to a file instead (see below).
There is also the option of inserting the figure in an interactive
globe using NASA’s WorldWind
Web. See option
external='globe' in the examples below.
A note for experienced GMT users¶
You’ll probably have noticed several things that are different from classic command-line GMT. Many of these changes reflect the new GMT modern execution mode that will be part of the future 6.0 release. A few are GMT/Python exclusive (like the long argument names).
- The name of method is
pscoast. As a general rule, all
ps*modules had their
psremoved. The exceptions are:
psxy == plot,
psxyz == plot3d, and
psscale == colorbar.
- The arguments don’t use the GMT 1-letter syntax (R, J, B, etc). Those are still available as aliases and the methods will accept them (see below).
- Arguments like
regioncan take lists instead of strings like
1/2/3/4. You can still use the string form but the list form is easier in Python.
- If a GMT argument has no options (like
-Baf), use a
Truevalue instead. An empty string would also be acceptable.
- There is no output redirecting to a PostScript file. The figure is generated in the background and will only be shown or saved when you ask for it.
We could have generated the figure above using the classic GMT argument names (but not the module names):
fig_alias = gmt.Figure() fig_alias.coast(R='-90/-70/0/20', J='M6i', G='gray', S="blue", B=True) fig_alias.show()
Saving the figure¶
fig.showwon’t produce a figure file.
fig.savefig (based on the
matplotlib function) to save your figure:
If you’re running a Python script, you can tell
fig.savefig to open
the figure in an external viewer: