我有一个带空心的三维梁截面(从外部多边形和空心多边形的2D多边形代码开始),我需要在Matplotlib 3D空间中绘制它。我根据梁的长度添加z坐标,首先过滤所有涉及的多边形,然后使用以下代码在Matplotlib 3D空间中绘制它们。此时,我能够填充空隙(见附图)但这不是我想要的。我如何改变顶点之间的空白和外部多边形(我的意思是固体部分)
import numpy as np
from sympy import Line, Polygon as Polyg
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt
shell=[(-500,0), (500,0), (400,500), (-400,500), (-500,0)]
hole1= [(-200,100), (200,100), (300,400), (-300,400), (-200,100)]
length =20000
ex = np.array(shell)
int_=np.array(hole1)
#hole2=[(-400,50), (-200,50), (-200,100), (-400,100), (-400,50)]
#holes=(hole1,hole2)
# insert Z cordinate for external polygon
polye1 = np.insert(ex, 2, 0, axis=1)
polye2 = np.insert(ex, 2, length, axis=1)
polyi1 = np.insert(int_, 2, 0, axis=1)
polyi2 = np.insert(int_, 2, length, axis=1)
vertices_e = np.dstack((polye1, polye2))
vertices_i = np.dstack((polyi1, polyi2))
polygons = []
polygonsi = []
for i in np.arange(vertices_e.shape[0]) - 1:
polygons.append(np.array([vertices_e[i, :, 1],
vertices_e[i + 1, :, 1],
vertices_e[i + 1, :, 0],
vertices_e[i, :, 0]]))
polygons.append(polye1)
polygons.append(polye2)
for i in np.arange(vertices_i.shape[0]) - 1:
polygonsi.append(np.array([vertices_i[i, :, 1],
vertices_i[i + 1, :, 1],
vertices_i[i + 1, :, 0],
vertices_i[i, :, 0]]))
polygonsi.append(polyi1)
polygonsi.append(polyi2)
A=polygons
B=polygonsi
fig = plt.figure()
ax = Axes3D(fig, auto_add_to_figure=False)
ax.set_box_aspect([1, 1, 1])
fig.add_axes(ax)
for i in range(0, len(A)):
x, y, z = zip(*A[i])
x1 = [*(x)]
y1 = [*(y)]
z1 = [*(z)]
ax.plot(x1, z1, y1, 'b') # interchange the axis to get correct oriantation
for i in range(0, len(B)):
xi, yi, zi = zip(*B[i])
x2 = [*(xi)]
y2 = [*(yi)]
z2 = [*(zi)]
ax.plot(x2, z2, y2, 'r') # interchange the axis to get correct oriantation
verts = [list(zip(x2, z2, y2))] # this is important variable to fill in between later
ax.add_collection3d(Poly3DCollection(verts, facecolors='cyan', linewidths=1, edgecolors='b', alpha=.25))
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.view_init(30, 120)
plt.show()
1条答案
按热度按时间new9mtju1#
下面是我如何克服这个问题的完整代码(从2D多边形开始),以及如何使用Matplotlib 3D绘图使纵横比相同。希望这对某些人有帮助。
下面给出了如何使用上述函数和类绘制实心空心3D多面体的示例代码: