Note
Go to the end to download the full example code.
Plotting lines
Plotting lines is handled by the pygmt.Figure.plot method.
import pygmt
Plot lines
Create a Cartesian figure using the pygmt.Figure.basemap method. Pass lists
containing two values to the x and y parameters of the
pygmt.Figure.plot method. By default, a 0.25-point thick, black, solid
line is drawn between these two data points.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
fig.plot(x=[1, 5], y=[5, 9])
fig.show()

Additional line segments can be added by including more data points in the lists
passed to x and y.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
fig.plot(x=[1, 5, 8], y=[5, 9, 4])
fig.show()

To plot multiple lines, pygmt.Figure.plot needs to be used for each line
separately.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
fig.plot(x=[1, 5, 8], y=[5, 9, 4])
fig.plot(x=[2, 4, 9], y=[3, 7, 5])
fig.show()

Change line attributes
The line attributes can be set by the pen parameter which takes a string
argument with the optional values width,color,style.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
# Set the pen width to "5p" (5 points), and use the default color "black" and the
# default style "solid"
fig.plot(x=[1, 8], y=[3, 9], pen="5p")
fig.show()

The line color can be set and is added after the line width to the pen
parameter.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
# Set the line color to "red", use the default style "solid"
fig.plot(x=[1, 8], y=[3, 9], pen="5p,red")
fig.show()

The line style can be set and is added after the line width or color to the
pen parameter.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
# Set the line style to "..-" (dot dot dash), use the default color "black"
fig.plot(x=[1, 8], y=[3, 9], pen="5p,..-")
fig.show()

The line width, color, and style can all be set in the same pen parameter.
For a gallery example showing other pen settings, see
Line styles.
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
# Draw a 7-point thick, green line with style "-.-" (dash dot dash)
fig.plot(x=[1, 8], y=[3, 9], pen="7p,green,-.-")
fig.show()

Connect points in different ways
By default, consecutive points are connected directly. The
straight_line parameter can instead create right-angle
connections. Use "x" to move along the x-axis first or
"y" to move along the y-axis first.
x = [2, 8]
y = [2, 8]
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/10c", frame=True)
# Connect the points directly
fig.plot(x=x, y=y, pen="2p,black", label="Direct")
# Move along the x-axis first, then the y-axis
fig.plot(x=x, y=y, straight_line="x", pen="2p,red", label="X first")
# Move along the y-axis first, then the x-axis
fig.plot(x=x, y=y, straight_line="y", pen="2p,blue", label="Y first")
# Mark the original points
fig.plot(x=x, y=y, style="c0.2c", fill="white", pen="0.5p,black")
fig.legend(position="BR")
fig.show()

Total running time of the script: (0 minutes 1.013 seconds)