我尝试使用 matplotib 在平面顶部绘制一个立方体。
我正在做一个关于无人机(无人机)着陆的项目,我必须将这些测试可视化。我已经绘制了一架着陆飞机,如下所示。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
xs = np.linspace(-10, 10, 100)
ys = np.linspace(-10, 20, 100)
X, Y = np.meshgrid(xs, zs)
Z = 0 / X
fig = plt.figure(figsize=(25,25))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z)
ax.set_ylim([-40, 40])
ax.set_zlim([0, 10])
ax.set_xlim([-10, 10])
# Hide grid lines
# ax.grid(False)
plt.xlabel("X axis")
plt.ylabel("Y axis")
# Hide axes ticks
# ax.set_xticks([])
# ax.set_yticks([])
# ax.set_zticks([])
# plt.savefig("foo.png")
plt.show()
输出如下图。
现在我想在图中绘制一个立方体。我有一个函数可以生成立方体的所有顶点,但我在尝试绘制它时遇到了错误。顶点是从西北顶部、东北顶部、东南顶部、西南顶部生成的。底部也是如此。
这是函数。
def createVertices(c3, stepSize):
hs = 0.5 * stepSize
a = [c3[0]-hs,c3[1]+hs,c3[2]+hs]
b = [c3[0]+hs,c3[1]+hs,c3[2]+hs]
c = [c3[0]+hs,c3[1]-hs,c3[2]+hs]
d = [c3[0]-hs,c3[1]-hs,c3[2]+hs]
e = [c3[0]-hs,c3[1]+hs,c3[2]-hs]
f = [c3[0]+hs,c3[1]+hs,c3[2]-hs]
g = [c3[0]+hs,c3[1]-hs,c3[2]-hs]
h = [c3[0]-hs,c3[1]-hs,c3[2]-hs]
return [a,b,c,d,e,f,g,h]
我必须动态地执行此操作,因为我有 100 多个测试用例。任何帮助将不胜感激。
其他信息:
贾里德提出了几乎类似的问题,但我遇到了来自
AttributeError: 'int' object has no attribute 'ndim'
的错误ax.plot_surface(X,Y,1, alpha=0.5)
。
我设法解决了动态创建立方体的问题。这是整个脚本。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
# create an array of all cube's center points
xPoints = np.linspace(30,50,3, dtype = int)
yPoints = np.linspace(-20,20,5, dtype = int)
zPoints = np.linspace(5,35,7, dtype = int)
allPoints= []
for xp in xPoints:
for yp in yPoints:
for zp in zPoints:
allPoints.append([xp,yp,zp])
# plot each of the cube
for c3 in allPoints:
# figure name
figName = ""+str(c3[0])+"_"+str(c3[1])+"_"+str(c3[2])+".pdf"
# landing strip
xs = np.linspace(10, 60, 100)
ys = np.linspace(-10, 20, 100)
X, Y = np.meshgrid(xs, ys)
Z = 0 / X
# create vertices
hs = 0.5 * 2
a = [c3[0]-hs,c3[1]+hs,c3[2]+hs]
b = [c3[0]+hs,c3[1]+hs,c3[2]+hs]
c = [c3[0]+hs,c3[1]-hs,c3[2]+hs]
d = [c3[0]-hs,c3[1]-hs,c3[2]+hs]
e = [c3[0]-hs,c3[1]+hs,c3[2]-hs]
f = [c3[0]+hs,c3[1]+hs,c3[2]-hs]
g = [c3[0]+hs,c3[1]-hs,c3[2]-hs]
h = [c3[0]-hs,c3[1]-hs,c3[2]-hs]
# arrange the vertices
vertices = [h,g,c,d,e,f,b,a]
x, y, z = [],[],[]
for vert in vertices:
x.append(vert[0])
y.append(vert[1])
z.append(vert[2])
# Face IDs
vertices = [[0,1,2,3],[1,5,6,2],[3,2,6,7],[4,0,3,7],[5,4,7,6],[4,5,1,0]]
tupleList = list(zip(x, y, z))
poly3d = [[tupleList[vertices[ix][iy]] for iy in range(len(vertices[0]))] for ix in range(len(vertices))]
# finally some plotting ;-)
fig = plt.figure(figsize=(25,25))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x,y,z)
ax.add_collection3d(Poly3DCollection(poly3d, facecolors='r', linewidths=1, alpha=0.5))
ax.plot_surface(X, Y, Z)
ax.set_ylim([-40, 40])
ax.set_zlim([0, 40])
ax.set_xlim([0, 60])
# Hide grid lines
# ax.grid(False)
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.savefig(figName)
我希望它对将来的人有帮助