Để gộp nhiều axes subplots Matplotlib thành một figure, chúng ta có thể dùng subplots() với nrow=2.
Các bước
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Tạo x, y1 và y2 điểm dữ liệu bằng numpy.
- Tạo figure và tập subplots.
- Vẽ x, y1 và y2 dùng plot().
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True x = np.linspace(-10, 10, 100) y1 = np.sin(x) y2 = np.cos(x) fig, axes = plt.subplots(nrows=2) line1, = axes[0].plot(x, y1, color='red') line2, = axes[1].plot(x, y2, color='green') plt.show()
Kết quả
