如何利用矩阵变换后的长方体顶点绘制3D图形?
Hey there! Let's get that transformed cuboid plotted properly—sounds like the main hurdles were probably messy vertex data formatting or incorrect edge/face connections. Here's a straightforward solution with two common plotting approaches (wireframe and filled surfaces) that should work for your setup:
Step 1: Clean Up Your Transformed Vertex Data
First, your new_corner entries are nested arrays (like [[array([[1.95]])], [array([[0.3]])], [array([[-0.708]])]]), which aren't easy to work with for plotting. Let's extract the raw numerical coordinates into a clean numpy array:
import numpy as np # Replace this with your actual list of 8 transformed vertices new_corners = [ [[np.array([[1.95]])], [np.array([[0.3]])], [np.array([[-0.708]])]], # ... add the other 7 vertices here ] def extract_clean_vertices(transformed_corners): vertices = [] for corner in transformed_corners: # Pull out the scalar values from the nested arrays x = corner[0][0][0] y = corner[1][0][0] z = corner[2][0][0] vertices.append((x, y, z)) return np.array(vertices) # Get a (8, 3) array of clean (x,y,z) coordinates clean_vertices = extract_clean_vertices(new_corners)
Step 2: Plot as a Wireframe Cuboid
If you prefer a simple outlined shape, we'll define all 12 edges of the cuboid and plot each one individually. This works great for verifying vertex positions:
import matplotlib.pyplot as plt # Define the 12 edges of the cuboid (index pairs of connected vertices) # Note: This assumes your original cuboid function generates vertices in a standard order # If your vertex order is different, adjust these pairs after checking clean_vertices edges = [ [0,1], [1,2], [2,3], [3,0], # Bottom face edges [4,5], [5,6], [6,7], [7,4], # Top face edges [0,4], [1,5], [2,6], [3,7] # Vertical connecting edges ] # Set up 3D plot fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111, projection='3d') # Plot each edge for start_idx, end_idx in edges: x_vals = [clean_vertices[start_idx, 0], clean_vertices[end_idx, 0]] y_vals = [clean_vertices[start_idx, 1], clean_vertices[end_idx, 1]] z_vals = [clean_vertices[start_idx, 2], clean_vertices[end_idx, 2]] ax.plot3D(x_vals, y_vals, z_vals, color='darkblue', linewidth=2) # Add labels and adjust view ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') ax.view_init(elev=30, azim=45) # Adjust viewing angle as needed plt.tight_layout() plt.show()
Step 3: Plot as a Filled Surface Cuboid
For a solid-looking cuboid, use Poly3DCollection to render filled faces with optional transparency:
from mpl_toolkits.mplot3d.art3d import Poly3DCollection # Define the 6 faces of the cuboid (each face is 4 vertex indices) faces = [ [0,1,2,3], # Bottom face [4,5,6,7], # Top face [0,1,5,4], # Front face [1,2,6,5], # Right face [2,3,7,6], # Back face [3,0,4,7] # Left face ] # Set up 3D plot fig = plt.figure(figsize=(8,6)) ax = fig.add_subplot(111, projection='3d') # Create 3D polygons for each face face_polygons = [] for face in faces: face_coords = [clean_vertices[idx] for idx in face] face_polygons.append(face_coords) # Add the collection to the plot ax.add_collection3D(Poly3DCollection( face_polygons, facecolors='lightcyan', edgecolors='darkblue', linewidths=1, alpha=0.6 # Adjust transparency here )) # Auto-scale axes to fit the cuboid ax.set_xlim(clean_vertices[:,0].min() - 0.1, clean_vertices[:,0].max() + 0.1) ax.set_ylim(clean_vertices[:,1].min() - 0.1, clean_vertices[:,1].max() + 0.1) ax.set_zlim(clean_vertices[:,2].min() - 0.1, clean_vertices[:,2].max() + 0.1) # Add labels ax.set_xlabel('X Axis') ax.set_ylabel('Y Axis') ax.set_zlabel('Z Axis') plt.tight_layout() plt.show()
Quick Troubleshooting Tip
If the cuboid looks distorted or disconnected, double-check the vertex order from your cuboid function. Print clean_vertices to see how the 8 points are arranged, then adjust the edges and faces index pairs to match your actual vertex ordering.
内容的提问来源于stack exchange,提问作者Pradeep Chakravarthi Nutakki

