python - 在立方体上绘制曲面

标签 python matplotlib

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

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()

最佳答案

修复新的 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()

关于python - 在立方体上绘制曲面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33540109/

相关文章:

python - 在 matplotlib 图中自定义 xticks

python - 派生 Enum 时使用 __class__ 获取属性

python - Pandas 数据帧 : Combining location and integer indexing

python - 运行一个从 PowerShell 显式获取参数的 Python 函数(不单独传递参数)

python - 在 basemap 中使用 pcolormesh 填充区域

python - Pyephem,定义和绘制不同时期的恒星

python - 带有 ctypes 函数的 Numpy 数组接口(interface)

python - 如何调整 Tkinter 按钮高度?

python - 如何在 PyQt5 应用程序中更改 Matplotlib NavigationToolbar 的默认文件名?

python - 在 CentOS 6.5 上安装 Matplotlib