要将 3D 子图添加到 matplotlib 图,我们可以采取以下步骤 -
设置图形大小并调整子图之间和周围的填充。
使用 numpy 创建 x、y 和 z 数据点。
创建新地物或激活现有地物。
将 'ax' 添加到图中,作为带有projection='3d'的子图排列的一部分。
使用方法绘制 x、y 和 z 数据点plot()。
要显示图形,请使用。show()方法。
from matplotlib import pyplot as plt import numpy as np # Set the figure size plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True # Create x, y and z data points using numpy x = np.linspace(-5, 5, 100) y = np.linspace(-5, 5, 100) z = x ** 2 + y ** 2 fig = plt.figure() # add subplot with projection='3d' ax = fig.add_subplot(111, projection='3d') # Plot x, y and z data points ax.plot(x, y, z, color='red', lw=7) plt.show()输出结果
它将产生以下输出 -