draw a unit cube in python

To draw a unit cube in Python, we can use the mpl_toolkits.mplot3d module from the matplotlib library. Here's an example code snippet:

main.py
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# Define the vertices of the cube
vertices = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], 
                     [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]])

# Define the edges that connect the vertices
edges = [(0, 1), (1, 2), (2, 3), (3, 0), 
         (4, 5), (5, 6), (6, 7), (7, 4),
         (0, 4), (1, 5), (2, 6), (3, 7)]

# Plot the vertices and edges using the plot_wireframe function
for edge in edges:
    ax.plot(vertices[edge, 0], vertices[edge, 1], vertices[edge, 2], 'b')
    
plt.show()
665 chars
22 lines

This code defines the 8 vertices of the unit cube, and the 12 edges that connect them. Then, it uses the plot_wireframe function from the Axes3D module to plot each edge as a line segment in 3D space. Finally, it displays the figure using plt.show().

gistlibby LogSnag