PyGMT: Plotting Holes In Polygons?

Hello again! After successfully plotting and coloring a large number of polygons as per my previous post, I’ve discovered that many of the polygons are being plotted over by other polygons. I suspect that this may be due to PyGMT not respecting the holes? From what I’ve read, it’s been suggested to mark the holes manually. However, I have far to many polygons to correct them by hand. Is there a way to make PyGMT plot the polygons as I wish it to?

My code for plotting the polygons:

import geopandas as gpd
import pygmt
import pandas as pd
import fiona
import os

main_dir = r'C:\Users\USER\Desktop\testing'

# Geologic bed polygons gmt file path
polyData = os.path.join(main_dir,'Data', 'GMC_geo_poly.gmt')

# Geologic bed cpt file path
polyColor = os.path.join(main_dir,'Data', 'geo_poly_color.cpt')

# Map save file path
saveName = os.path.join(main_dir, 'Results', 'geologic_map.png')

# Parameters
region = [-125, -122, 38, 40]

fig = pygmt.Figure()

    region = region,
    projection = 'M6i',
    frame = 'f'

# plots the polygons from the gmt file
    data = polyData,
    pen="0.1p,black,-",  # set outline colour
    cmap = polyColor,
    color = '+z',
    close=True,  # force close polygons
    transparency = 50
    shorelines = '0.1p,black',
    resolution = 'h',
    water = 'lightskyblue2',
    #transparency = 50,

# saves a copy of the generated figure

An example of part of the map with excessive plotting over holes (holes are fainter in color because I set transparency on, as normally they would be completely obscured):

You could try running your polyData through gmt spatial -Sh first. See the note here:

1 Like

I eventually figured out how to use gmt spatial -Sh, but it wasn’t able to correct the issue. As it turns out, there is/was data loss when converting the shp file to kml, which is where the problem with the holes was rooted. I ended up simply using QGIS to convert the shp file crs to epsg:4326 and then converting directly to gmt. The correct information about the polygons was preserved and they plotted perfectly.