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-points 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()
lines

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()
lines

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()
lines

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()
lines

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()
lines

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()
lines

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-points thick, green line with style "-.-" (dash dot dash)
fig.plot(x=[1, 8], y=[3, 9], pen="7p,green,-.-")

fig.show()
lines

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

Gallery generated by Sphinx-Gallery