如何使用Matplotlib和Numpy从三维图计算体积和面积?

7gyucuyw  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(146)

大家好,我想知道如何计算体积和表面积的三维图?
我正在使用正确的大小为宽度,长度和高度。
这是我的代码:

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import numpy as np

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

# vertices of a prism
v = np.array([[-2, -2, -2.5], [2, -2, -2.5], [2, 2, -2.5],  [-2, 2, -2.5], [2,0,2.5], 
        [-2,0,2.5]])
ax.scatter3D(v[:, 0], v[:, 1], v[:, 2])

# generate list of sides' polygons of our prism
verts = [ [v[0],v[1],v[4],v[5]], [v[0],v[3],v[5]],
 [v[2],v[1],v[4]], [v[2],v[3],v[5],v[4]], [v[0],v[1],v[2],v[3]]]

# plot sides
ax.add_collection3d(Poly3DCollection(verts, 
 facecolors='cyan', linewidths=1, edgecolors='r', alpha=.25))

plt.show()

uyto3xhc

uyto3xhc1#

请尝试将其添加到代码中

a=v[1]-v[0]
b=v[3]-v[0]
c=v[4]-v[1]

Volume=np.dot(a,np.cross(b,c))

surface_area=2* (abs(a[0]*b[1]*c[2]) + abs(a[1]*b[2]*c[0]) + abs(a[2]*b[0]*c[1]))

print(volume)
print(Surface_area)
qlvxas9a

qlvxas9a2#

这就是我想要的,代码的工作感谢@阿里Redha

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection
import numpy as np

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

# vertices of a prism
v = np.array([[-5, -2, -2.5], [5, -2, -2.5], [5, 2, -2.5],  [-5, 2, -2.5], [5,0,2.5], 
        [-5,0,2.5]])
ax.scatter3D(v[:, 0], v[:, 1], v[:, 2])

# generate list of sides' polygons of our prism
verts = [ [v[0],v[1],v[4],v[5]], [v[0],v[3],v[5]],
 [v[2],v[1],v[4]], [v[2],v[3],v[5],v[4]], [v[0],v[1],v[2],v[3]]]

# plot sides
ax.add_collection3d(Poly3DCollection(verts, 
 facecolors='cyan', linewidths=1, edgecolors='r', alpha=.25))

# Calculate Volume and Surface Area
a=v[1]-v[0]
b=v[3]-v[0]
c=v[4]-v[1]

volume=np.dot(a,np.cross(b,c))

surface_area=2* (abs(a[0]*b[1]*c[2]) + abs(a[1]*b[2]*c[0]) + abs(a[2]*b[0]*c[1]))

# Annotation
ax.set_title("Prism")
ax.text(2,0,6.6,"Volume= {}".format(volume), size=10,
                           verticalalignment='center', rotation=270)
ax.text(2,0,7.2,"Surface Area = {}".format(surface_area), size=10,
                           verticalalignment='center', rotation=270)
plt.show()

相关问题