Closed as not planned
Description
Bug summary
I don't know why it is that I am getting these errors. I'll post an image with the errors.

Code for reproduction
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
# Define vertices of a 4D hypercube (tesseract)
def tesseract_vertices():
vertices = np.array([[1 if i & (1 << j) else -1 for j in range(4)] for i in range(16)])
return vertices
# Define edges of the tesseract
def tesseract_edges(vertices):
edges = []
for i in range(len(vertices)):
for j in range(i + 1, len(vertices)):
if np.sum(np.abs(vertices[i] - vertices[j])) == 2:
edges.append((i, j))
return edges
# Perspective projection from 4D to 3D
def perspective_projection(v, distance=2):
w = v[3]
factor = distance / (distance - w)
return np.array(v[:3]) * factor
# Project all vertices of the tesseract into 3D
def project_tesseract(vertices):
return np.array([perspective_projection(v) for v in vertices])
# Define faces based on the tesseract edges
def tesseract_faces(vertices, edges):
faces = []
# List of adjacent vertices that form square faces (hardcoded for simplicity)
square_faces = [
[0, 1, 3, 2], [4, 5, 7, 6], [0, 1, 5, 4], [2, 3, 7, 6],
[0, 2, 6, 4], [1, 3, 7, 5], [8, 9, 11, 10], [12, 13, 15, 14],
[8, 9, 13, 12], [10, 11, 15, 14], [8, 10, 14, 12], [9, 11, 15, 13],
[0, 4, 12, 8], [1, 5, 13, 9], [2, 6, 14, 10], [3, 7, 15, 11]
]
for face in square_faces:
# Project face vertices into 3D
projected_face = [perspective_projection(vertices[i]) for i in face]
faces.append(projected_face)
return faces
# Plot the 3D projection of the tesseract
def plot_tesseract_3d(vertices, edges, faces):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Add edges
for edge in edges:
points = vertices[list(edge)]
ax.plot(points[:, 0], points[:, 1], points[:, 2], 'b-', lw=1)
# Add faces with transparency and colors
for face in faces:
poly3d = Poly3DCollection([face], alpha=0.5, edgecolor='k', facecolor='cyan')
ax.add_collection3d(poly3d)
# Add vertices with colors
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2], c='r', s=100)
for i, vertex in enumerate(vertices):
ax.text(vertex[0], vertex[1], vertex[2], f'{i}', color='black')
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
plt.title('3D Perspective Projection of a Tesseract')
plt.show()
def main():
vertices_4d = tesseract_vertices()
edges = tesseract_edges(vertices_4d)
print("Edges: ", len(edges)) # This prints the number of edges
vertices_3d = project_tesseract(vertices_4d)
faces = tesseract_faces(vertices_4d, edges)
plot_tesseract_3d(vertices_3d, edges, faces)
if __name__ == "__main__":
main()
Actual outcome

Expected outcome
No errors to appear.
Additional information
No response
Operating system
macOS Sequoia 15.0.1
Matplotlib Version
3.9.2
Matplotlib Backend
macosx
Python version
Python 3.12.7
Jupyter version
No response
Installation
pip