Để tạo bề mặt 3D có màu trong Python, thực hiện các bước sau:
Các bước
- Thiết lập kích thước hình và padding.
- Tạo dữ liệu x, y bằng numpy.
- Tính dữ liệu 3D (z).
- Tạo figure mới hoặc kích hoạt figure hiện có.
- Lấy axes 3D.
- Tạo surface plot.
- Hiển thị hình bằng show().
Ví dụ
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
x = np.linspace(-3, 3, 100)
y = np.cos(x)
x, y = np.meshgrid(x, y)
z = x ** 2 + y ** 2 - 2
fig = plt.figure()
ax = plt.axes(projection='3d')
surf = ax.plot_surface(x, y, z, cmap=plt.get_cmap('hot'), edgecolor='none')
plt.show()
Kết quả
Bề mặt 3D màu nhiệt (hot colormap) đại diện cho hàm z = x^2 + y^2 - 2.