Contour plot 3D hiển thị đường đồng mức (iso-lines) của mặt hàm z=f(x,y) trong không gian 3D.
Cài đặt
pip install matplotlib
Ví dụ
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
def fun(x, y):
return np.sin(np.sqrt(x**2 + y**2))
x = np.linspace(-5, 5, 30)
y = np.linspace(-5, 5, 30)
X, Y = np.meshgrid(x, y)
Z = fun(X, Y)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('X-axis')
ax.set_ylabel('Y-axis')
ax.set_zlabel('Z-axis')
ax.set_title('3D Contour Plot')
plt.show()
Kết quả

Giải thích
contour3Dvẽ 50 cấp độ contour, colormap 'binary'.- Hàm
sin(sqrt(x²+y²))tạo mặt sóng tròn đồng tâm. - Dùng để trực quan hàm mất mát, gradient descent.