如何从数据框绘制曲面图/3D图形(Matplotlib)?
要绘制曲面图/3d图形,我们需要2D数据点,而不是1D数据框。
步骤
- 设置图形大小并调整子图之间和周围的填充。
-
创建新图形或使用figure()方法激活现有图形。
-
将一个 ‘~.axes.Axes’ 作为子图布局的一部分添加到图形中,使用 add_subplot() 方法。
-
为样本数初始化变量 ‘n’
-
使用numpy创建x、y和z数据点。
-
使用 plot_surface() 方法制作曲面3d效果。
-
要显示图形,使用 show() 方法。
示例
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
n = 50
x = np.random.rand(n)
y = np.tan(x)
z = np.random.rand(n, n)
surf = ax.plot_surface(y, x, z, rstride=1, cstride=1, cmap='copper',linewidth=0, antialiased=False)
ax.axis('off')
plt.show()