在 IPython 或 Jupyter Notebook 中显示可旋转的 3D 图

matplotlibserver side programmingprogramming更新于 2024/11/21 3:35:00

通过在轴上创建 3D 投影并使用 view_init() 迭代该轴的不同角度,我们可以旋转输出图。

步骤

  • 创建一个新图形,或激活现有图形。

  • 将 `~.axes.Axes` 添加到图形中作为子图排列的一部分,其中 nrow = 1、ncols = 1、index = 1 和投影 = '3d'。

  • 使用方法 get_test_data 返回带有测试数据集的元组 X、Y、Z。

  • 绘制带有数据测试数据 x、y 和z。

  • 为了使其可旋转,我们可以使用 view_init() 方法以度(而不是弧度)设置轴的仰角和方位角。

  • 要显示图形,请使用 plt.show() 方法。

示例

from mpl_toolkits.mplot3d importaxes3d
importmatplotlib.pyplot as plt

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

X, Y, Z =axes3d.get_test_data(0.1)
ax.plot_wireframe(X, Y, Z, rstride=5, cstride=5)

# 旋转轴并更新
for angle in range(0, 360):
   ax.view_init(30, angle)

plt.show()

输出


相关文章