Fill interior of matplotlib.pyplot polygon plot

Hi all,

I would like to fill the interior of a polygon plot. Here is the code I have.

import matplotlib.pyplot as plt
from shapely.geometry import LineString
from shapely.ops import unary_union

polygons = []
buffer = 1
line = LineString([[0,0],[0,10]]).buffer(buffer)
polygons.append(line)

line = LineString([[5,0],[0,10]]).buffer(buffer)
polygons.append(line)

line = LineString([[-3,3],[12,3]]).buffer(buffer)
polygons.append(line)

line = LineString([[-3,7],[12,7]]).buffer(buffer)
polygons.append(line)


line = LineString([[5,15],[9,20]]).buffer(buffer)
polygons.append(line)


# combine into a single polygon
my_polygon = unary_union(polygons)

fig, axs = plt.subplots()
axs.set_aspect('equal', 'datalim')
for geom in my_polygon.geoms:
    xs, ys = geom.exterior.xy
    axs.plot(xs, ys, alpha=0.5, color="blue")
    for inner in geom.interiors:
        xi, yi = zip(*inner.coords[:])
        axs.plot(xi, yi, color="blue", alpha=.5)

This looks correct, but there is no fill. If I change the code from axs.plot to axs.fill, its close, but it fills in the inner triangle of the “A” shape

polygon_filled

Can someone please show me how to fill to exclude the shaded areas, such as the the triangle in the A?

Thank you.

I made title more specific because the answer is different from what it would be for something else, such as a tkinter.canvas drawing.

1 Like