Search code examples
pythonmatplotlibshapestriangulationshading

Adding a layer of polygons to an existing plot in Python Matplotlib


Currently I'm using the tripcolor function for triangulation and shading of 3D point data. What I'm getting is a cut out of map data. And there is more data I want to use. I also have a shapefile which contains a set of polygons. The goal of my work is to classify roof types. So the shapes in the shapefile are borders which include the roofs in the map you can see. What I have now is the set of points in x- y- and z-coordinates so I can render the map you can see below. How can I add one more layer inside this plot which draws the polygons of the shapes into the map?

plt.tripcolor(x, y, z, shading='gouraud')

enter image description here


Solution

  • Adding shapes on a plot can be done using a PolyCollection.

    import matplotlib.pyplot as plt
    from matplotlib import collections
    import numpy as np; np.random.seed(17)
    
    b = np.random.rand(100,3)
    
    fig, ax = plt.subplots()
    ax.tripcolor(b[:,0],b[:,1],b[:,2], shading='gouraud')
    
    polys = [np.random.rand(4,2)*.3+np.random.rand(1,2)*((2*i+1)/6.) for i in range(3)]
    pc = collections.PolyCollection(polys, color="crimson")    
    ax.add_collection(pc)
    
    plt.show()
    

    enter image description here