在立方体上绘制曲面

问题描述 投票:0回答:3

我想用

matplotlib
将曲面绘制到立方体上。我正在尝试使用
ax.plot_surface(X, Y, Z)
,但我有点困惑。
X
Y
Z
应该代表什么二维数组?

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')
# ax.plot_surface(X, Y, Z)  # how?
ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
python matplotlib matplotlib-3d
3个回答
11
投票

修复新的 matplotlib

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)
one = np.ones(4).reshape(2, 2)
ax.plot_wireframe(X,Y,one, alpha=0.5)
ax.plot_wireframe(X,Y,-one, alpha=0.5)
ax.plot_wireframe(X,-one,Y, alpha=0.5)
ax.plot_wireframe(X,one,Y, alpha=0.5)
ax.plot_wireframe(one,X,Y, alpha=0.5)
ax.plot_wireframe(-one,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()

9
投票

立方体的每个面都是一个表面,您可以自己定义每个角,或使用网格:

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)
z = np.array([[1, 1]])
ax.plot_surface(X,Y, 1*z, alpha=0.5)
ax.plot_surface(X,Y,-1*z, alpha=0.5)
ax.plot_surface(X,-1*z,Y, alpha=0.5)
ax.plot_surface(X,1*z,Y, alpha=0.5)
ax.plot_surface(1*z,X,Y, alpha=0.5)
ax.plot_surface(-1*z,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()

enter image description here

X、Y 和 Z 是(相同的)2D 点列表:

>>> numpy.meshgrid([-1,1], [-1,1])
[array([[-1,  1],
       [-1,  1]]), array([[-1, -1],
       [ 1,  1]])]

8
投票

要在绘制长方体时使用对

plot_surface
的单个调用,可以在两个变量上参数化表面,例如使用球坐标。

下图画了一个边长为a、b、c的长方体:

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

def get_cube():   
    phi = np.arange(1,10,2)*np.pi/4
    Phi, Theta = np.meshgrid(phi, phi)

    x = np.cos(Phi)*np.sin(Theta)
    y = np.sin(Phi)*np.sin(Theta)
    z = np.cos(Theta)/np.sqrt(2)
    return x,y,z


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

a = 1
b = 2
c = 3
x,y,z = get_cube()


ax.plot_surface(x*a, y*b, z*c)

ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
ax.set_zlim(-2,2)
plt.show()

enter image description here


另请参阅用 matplotlib 表示体素

© www.soinside.com 2019 - 2024. All rights reserved.