在立方体上绘制曲面 [英] Plot surfaces on a cube

查看:52
本文介绍了在立方体上绘制曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to plot surfaces to a cube with matplotlib. I am trying to use ax.plot_surface(X, Y, Z), however I am a bit confused. What should the X, Y and Z represent as 2D arrays?

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

推荐答案

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

Each face of the cube is a surface for which you can either define each corner yourself, or use meshgrid:

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

X、Y 和 Z 是(相同的)二维点列表:

X,Y, and Z are (the same) list of 2D points:

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

这篇关于在立方体上绘制曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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