Shapefile to gmt (python)

The answer depends a lot on how the ‘lines’ in your shapefile are stored as, are they single linestrings, or multilinestrings? Or are they stored as a feature collection? Might be best if you can provide a sample of your ‘fault.shp’ file (including the .shx and .dbf files) so we can examine it.

That said, what I usually do is to load the shapefile using geopandas first, convert the ‘lines’ to a numpy array of x and y coordinates, and have PyGMT plot that. The code below should help to get you started:

import geopandas as gpd
import pygmt

# Load data from shapefile and convert to numpy array
gdf = gpd.read_file(filename="fault.shp")
linestrings = [geom for geom in gdf.geometry]

# Plot in PyGMT
fig = pygmt.Figure()
for line in linestrings:
    x, y = line.coords.xy
    fig.plot(x=x, y=y, pen="thin")
fig.show()
2 Likes