Skip to main content
 首页 » 编程设计

python之Python 中的 Plot3d

2024年10月24日8mayingbao

我有一个由 Meshlab 生成的带有顶点和面数据的 OBJ 文件。 在 MATLAB 中,我使用函数“patch”在 1 个数组 (5937x3) 中使用顶点数据,在另一个数组中使用面 (11870x3) 数据,结果是这样的:

Simplified version of the code 
 
[V,F] = read_vertices_and_faces_from_obj_file(filename); 
 
patch('Vertices',V,'Faces',F,'FaceColor','r','LineStyle','-') 
 
axis equal 

Result

问题是,我如何在 Python 中做到这一点?在Matlab中有一个简单的方法吗??

我将非常感谢任何帮助。

请您参考如下方法:

您最好的选择是使用 mplot3d toolkit来自 matplotlib 库。

有人问了类似的问题here .也许从该问题中摘录的经过稍微编辑的代码会对您有所帮助。

代码:

from mpl_toolkits.mplot3d import Axes3D 
from mpl_toolkits.mplot3d.art3d import Poly3DCollection 
import matplotlib.pyplot as plt 
 
fig = plt.figure() 
ax = Axes3D(fig) 
# Specify 4 vertices 
x = [0,1,1,0] # Specify x-coordinates of vertices 
y = [0,0,1,1] # Specify y-coordinates of vertices 
z = [0,1,0,1] # Specify z-coordinates of vertices 
verts = [zip(x, y, z)] # [(0,0,0), (1,0,1), (1,1,0), (0,1,1)] 
tri = Poly3DCollection(verts) # Create polygons by connecting all of the vertices you have specified 
tri.set_color(colors.rgb2hex(sp.rand(3))) # Give the faces random colors 
tri.set_edgecolor('k') # Color the edges of every polygon black 
ax.add_collection3d(tri) # Connect polygon collection to the 3D axis 
plt.show()