8000 I keep getting these Axes errors and I don't know why. "Cannot attribute 'add_collection3d' for class 'Axes' Attribute 'add_collection3d' is unknown" · Issue #29003 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

I keep getting these Axes errors and I don't know why. "Cannot attribute 'add_collection3d' for class 'Axes' Attribute 'add_collection3d' is unknown" #29003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MrCherub opened this issue Oct 21, 2024 · 2 comments
Labels

Comments

@MrCherub
Copy link

Bug summary

I don't know why it is that I am getting these errors. I'll post an image with the errors.

Screenshot 2024-10-21 at 12 13 24 AM

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

Screenshot 2024-10-21 at 12 53 44 AM It seems to be working but I don't know why the errors appear.

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

@rcomer
Copy link
Member
rcomer commented Oct 21, 2024

Hi @MrCherub, I believe these errors come from the type annotation checker and the reason it’s happening is explained at #27455. It is not a problem with your actual code.

@rcomer rcomer added the Community support Users in need of help. label Oct 21, 2024
@timhoffm
Copy link
Member

See https://github.com/matplotlib/matplotlib/issues/27455#issuecomment-2425901612 on a workaround to placate the type checker.

@timhoffm timhoffm closed this as not planned Won't fix 513E , can't repro, duplicate, stale Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants
0