蟒蛇画平行六面体 [英] python draw parallelepiped

查看:11
本文介绍了蟒蛇画平行六面体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想画一个平行六面体.其实我是从 python 脚本开始绘制一个立方体的:

将 numpy 导入为 np从 mpl_toolkits.mplot3d 导入 Axes3D导入 matplotlib.pyplot 作为 plt点 = np.array([[-1, -1, -1],[1, -1, -1 ],[1, 1, -1],[-1, 1, -1],[-1, -1, 1],[1, -1, 1 ],[1, 1, 1],[-1, 1, 1]])fig = plt.figure()ax = fig.add_subplot(111, 投影='3d')r = [-1,1]X, Y = np.meshgrid(r, r)ax.plot_surface(X,Y,1, alpha=0.5)ax.plot_surface(X,Y,-1, alpha=0.5)ax.plot_surface(X,-1,Y, alpha=0.5)ax.plot_surface(X,1,Y, alpha=0.5)ax.plot_surface(1,X,Y, alpha=0.5)ax.plot_surface(-1,X,Y, alpha=0.5)ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')plt.show()

为了获得平行六面体,我将点矩阵乘以以下矩阵:

P =[[2.06498904e-01 -6.30755443e-07 1.07477548e-03][1.61535574e-06 1.18897198e-01 7.85307721e-06][7.08353661e-02 4.48415767e-06 2.05395893e-01]]

作为:

Z = np.zeros((8,3))对于范围内的 i (8):Z[i,:] = np.dot(points[i,:],P)Z = 10.0*Z

我的想法是这样表示:

ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])ax.set_xlabel('X')ax.set_ylabel('Y')ax.set_zlabel('Z')plt.show()

这就是我得到的:

然后我如何将曲面放在这些不同的点上以形成平行六面体(以上面的立方体的方式)?

解决方案

使用 3D PolyCollection 绘制曲面(

I am trying to draw a parallelepiped. Actually I started from the python script drawing a cube as:

import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

points = np.array([[-1, -1, -1],
                  [1, -1, -1 ],
                  [1, 1, -1],
                  [-1, 1, -1],
                  [-1, -1, 1],
                  [1, -1, 1 ],
                  [1, 1, 1],
                  [-1, 1, 1]])


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

r = [-1,1]

X, Y = np.meshgrid(r, r)

ax.plot_surface(X,Y,1, alpha=0.5)
ax.plot_surface(X,Y,-1, alpha=0.5)
ax.plot_surface(X,-1,Y, alpha=0.5)
ax.plot_surface(X,1,Y, alpha=0.5)
ax.plot_surface(1,X,Y, alpha=0.5)
ax.plot_surface(-1,X,Y, alpha=0.5)

ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

In order to obtain a parallelepiped, I have multiplied the points matrix by the following matrix:

P = 

[[2.06498904e-01  -6.30755443e-07   1.07477548e-03]

 [1.61535574e-06   1.18897198e-01   7.85307721e-06]

 [7.08353661e-02   4.48415767e-06   2.05395893e-01]]

as:

Z = np.zeros((8,3))

for i in range(8):

   Z[i,:] = np.dot(points[i,:],P)

Z = 10.0*Z

My idea is then to represent as follows:

ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

And this is what I get:

How can I then put surfaces on these different points to form the parallelepiped (in the way of the cube above)?

解决方案

Plot surfaces with 3D PolyCollection (example)

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

points = np.array([[-1, -1, -1],
                  [1, -1, -1 ],
                  [1, 1, -1],
                  [-1, 1, -1],
                  [-1, -1, 1],
                  [1, -1, 1 ],
                  [1, 1, 1],
                  [-1, 1, 1]])

P = [[2.06498904e-01 , -6.30755443e-07 ,  1.07477548e-03],
 [1.61535574e-06 ,  1.18897198e-01 ,  7.85307721e-06],
 [7.08353661e-02 ,  4.48415767e-06 ,  2.05395893e-01]]

Z = np.zeros((8,3))
for i in range(8): Z[i,:] = np.dot(points[i,:],P)
Z = 10.0*Z

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

r = [-1,1]

X, Y = np.meshgrid(r, r)
# plot vertices
ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2])

# list of sides' polygons of figure
verts = [[Z[0],Z[1],Z[2],Z[3]],
 [Z[4],Z[5],Z[6],Z[7]], 
 [Z[0],Z[1],Z[5],Z[4]], 
 [Z[2],Z[3],Z[7],Z[6]], 
 [Z[1],Z[2],Z[6],Z[5]],
 [Z[4],Z[7],Z[3],Z[0]]]

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

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

这篇关于蟒蛇画平行六面体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆