Note

Go to the endto download the full example code.

Generate 3D polygons#

Demonstrate how to create polygons in 3D. Here we stack 3 hexagons.

importmatplotlib.pyplotaspltimportnumpyasnpfrommpl_toolkits.mplot3d.art3dimportPoly3DCollection# Coordinates of a hexagonangles=np.linspace(0,2*np.pi,6,endpoint=False)x=np.cos(angles)y=np.sin(angles)zs=[-3,-2,-1]# Close the hexagon by repeating the first vertexx=np.append(x,x[0])y=np.append(y,y[0])verts=[]forzinzs:verts.append(list(zip(x*z,y*z,np.full_like(x,z))))verts=np.array(verts)ax=plt.figure().add_subplot(projection='3d')poly=Poly3DCollection(verts,alpha=.7)ax.add_collection3d(poly)ax.set_aspect('equalxy')plt.show()
polys3d

Tags:plot-type: 3Dstyling: colormaplevel: intermediate

Gallery generated by Sphinx-Gallery