Transparency for Poly3DCollection plot in matplotlib

前端 未结 5 1291
无人共我
无人共我 2020-11-30 06:25

I am trying to draw some objects with the fabulous Matplotlib package for Python. These objects consist of points implemented with plt.scatter() and patches imp

5条回答
  •  自闭症患者
    2020-11-30 06:32

    I found a nice workaround: After plotting the data, do another plot on top with the same color and lighter line style. Instead of Poly3DCollection I use Line3DCollection, so no faces are plotted. The result looks very much as anticipated.

    See below the new plot and the script creating it.

    enter image description here

    from matplotlib import pyplot as plt
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    
    x = [0, 2, 1, 1]
    y = [0, 0, 1, 0]
    z = [0, 0, 0, 1]
    
    vertices = [[0, 1, 2], [0, 1, 3], [0, 2, 3], [1, 2, 3]]
    
    tupleList = list(zip(x, y, z))
    
    poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] for ix in range(len(vertices))]
    ax.scatter(x,y,z)
    ax.add_collection3d(Poly3DCollection(poly3d, facecolors='w', linewidths=1, alpha=0.5))
    ax.add_collection3d(Line3DCollection(poly3d, colors='k', linewidths=0.2, linestyles=':'))
    
    plt.show()
    

提交回复
热议问题