Để vẽ biểu đồ màu 2D mượt (smooth) cho hàm z = f(x, y), bạn có thể dùng imshow với nội suy bilinear.
- Thiết lập kích thước hình vẽ và khoảng đệm.
- Tạo mảng x, y bằng NumPy.
- Tính toán z = f(x, y).
- Hiển thị dữ liệu như ảnh 2D bằng
plt.imshow(z, interpolation='bilinear'). - Hiển thị hình vẽ.
Ví dụ minh họa
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
def f(x, y):
return np.array([i * i + j * j for j in y for i in x]).reshape(5, 5)
x = y = np.linspace(-1, 1, 5)
z = f(x, y)
plt.imshow(z, interpolation='bilinear')
plt.show()
Kết quả đầu ra
