Để vẽ hàm đa biến 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, z ngẫu nhiên bằng numpy.
- Tạo figure và subplots.
- Tạo scatter plot với x, y, z (z làm màu).
- Tạo colorbar cho ScalarMappable s.
- 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
def func(x, y):
return 3 * x + 4 * y - 2 + np.random.randn(30)
x, y = np.random.randn(2, 30)
y *= 100
z = func(x, y)
fig, ax = plt.subplots()
s = ax.scatter(x, y, c=z, s=100, marker='*', cmap='plasma')
fig.colorbar(s)
plt.show()
Kết quả
Scatter plot 30 điểm, màu theo giá trị z (hàm tuyến tính + noise), marker sao, colormap plasma, có colorbar.