GMT/Python demo

Demo of the gmt-python package for Scipy 2017.

Try to recreate a few plots from the GMT Tutorial.

In [1]:
import gmt

Importing the module starts a mordern mode session. So there is no need to call gmt begin and gmt end.

GMT creates a temporary directory where everything is stored.

In [2]:
ls -d /tmp/gmt*

Start a new figure.

In [3]:

Copy and modify some code from the tutorial:

gmt pscoast -R-90/-70/0/20 -JM6i -P -Ba -Gchocolate >
In [4]:
gmt.pscoast(R='-90/-70/0/20', J="M6i", G='chocolate', B='a', P=True)

View the plot in the notebook:

In [5]:

Use aliases for the command-line arguments.

In [6]:
Help on function pscoast in module gmt.ps_modules:

    Plot continents, shorelines, rivers, and borders on maps
    Plots grayshaded, colored, or textured land-masses [or water-masses] on
    maps and [optionally] draws coastlines, rivers, and political boundaries.
    Alternatively, it can (1) issue clip paths that will contain all land or
    all water areas, or (2) dump the data to an ASCII table. The data files
    come in 5 different resolutions: (**f**)ull, (**h**)igh,
    (**i**)ntermediate, (**l**)ow, and (**c**)rude. The full resolution files
    amount to more than 55 Mb of data and provide great detail; for maps of
    larger geographical extent it is more economical to use one of the other
    resolutions. If the user selects to paint the land-areas and does not
    specify fill of water-areas then the latter will be transparent (i.e.,
    earlier graphics drawn in those areas will not be overwritten).  Likewise,
    if the water-areas are painted and no land fill is set then the land-areas
    will be transparent.
    A map projection must be supplied.
    Full option list at
    - A = area_thresh
    - B = frame
    - D = resolution
    - G = land
    - I = rivers
    - J = projection
    - N = borders
    - P = portrait
    - R = region
    - S = water
    - W = shorelines
    J : str
        *Required if this is the first plot command*.
        Select map projection.
    R : str or list
        *Required if this is the first plot command*.
        Specify the region of interest.
    A : int, float, or str
        Features with an area smaller than min_area in km^2 or of hierarchical
        level that is lower than min_level or higher than max_level will not be
    B : str
        Set map boundary frame and axes attributes.
    C : str
        Set the shade, color, or pattern for lakes and river-lakes.
    D : str
        Selects the resolution of the data set to use ((f)ull, (h)igh,
        (i)ntermediate, (l)ow, and (c)rude).
    G : str
        Select filling or clipping of “dry” areas.
    I : str
        Draw rivers. Specify the type of rivers and [optionally] append pen
    N : str
        Draw political boundaries. Specify the type of boundary and
        [optionally] append pen attributes
    S : str
        Select filling or clipping of “wet” areas.
    U : bool or str
        Draw GMT time stamp logo on plot.
    W : str
        Draw shorelines [Default is no shorelines]. Append pen attributes.

In [7]:
gmt.pscoast(region='-90/-70/0/20', projection="M6i", 
            land='chocolate', frame='a', portrait=True)

We automatically convert some arguments for you.

Let's look at another command:

gmt pscoast -R-130/-70/24/52 -JB-100/35/33/45/6i -Ba -B+t"Conic Projection" -N1/thickest -N2/thinnest -A500 -Ggray -Wthinnest -P >
In [8]:
gmt.pscoast(region=[-130, -70, 24, 52], projection="B-100/35/33/45/6i", 
            land='gray', frame='a', portrait=True, shorelines='thinnest',
            borders='1/thickest', area_thresh=500)

Lets plot some data!

This example will not work entirely yet:

gmt makecpt -Cred,green,blue -T0,70,300,10000 > quakes.cpt
gmt pscoast -R130/150/35/50 -JM6i -B5 -P -Ggray -K >
gmt psxy -R -J -O @tut_quakes.ngdc -Wfaint -i4,3,5,6s0.1 -h3 -Scc -Cquakes.cpt >>
In [9]:
gmt.pscoast(region=[130, 150, 35, 50], projection='M6i', frame=5, portrait=True, land='gray')
gmt.psxy(data='@tut_quakes.ngdc', pen='faint', i='4,3', style='c0.2c', color='blue')

Save the figure to file:

In [10]:
gmt.psconvert(prefix='my_figure', fmt='g', crop=True)
In [11]:
ls *.png