Transparency for Poly3DCollection plot in matplotlib

前端 未结 5 1303
无人共我
无人共我 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:53

    I made a slight modification to the OP code and got the transparency working. It appears that the facecolors argument of Poly3DCollection overrides the transparency argument, so the solution was to set the color in a separate call to either Poly3DCollection.set_color or Poly3DCollection.set_facecolor:

    from matplotlib import pyplot as plt
    from mpl_toolkits.mplot3d.art3d import Poly3DCollection
    
    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 = 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)
    collection = Poly3DCollection(poly3d, linewidths=1, alpha=0.2)
    face_color = [0.5, 0.5, 1] # alternative: matplotlib.colors.rgb2hex([0.5, 0.5, 1])
    collection.set_facecolor(face_color)
    ax.add_collection3d(collection)
    
    plt.show()
    

    Interestingly, if you explicitly set the edge color with collection.set_edgecolor('k'), the edges will also honor the transparency setting.

提交回复
热议问题