Dùng sharex=ax1 khi tạo subplot2grid thứ hai để cùng trục X.
- Tạo t, x, y1, y2 bằng numpy.
- fig = plt.figure().
- ax1 = plt.subplot2grid((4,3), (0,0), colspan=3, rowspan=2).
- ax2 = plt.subplot2grid((4,3), (2,0), colspan=3, sharex=ax1).
- ax1.plot(t, y1, c='red'), ax2.plot(t, y2, c='orange').
- tight_layout, show().
Ví dụ
import numpy as np from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True t = np.arange(0.0, 2.0, 0.01) x = np.sin(2 * np.pi * t) y1 = np.exp(-t) y2 = x * y1 fig = plt.figure() ax1 = plt.subplot2grid((4, 3), (0, 0), colspan=3, rowspan=2) ax2 = plt.subplot2grid((4, 3), (2, 0), colspan=3, sharex=ax1) ax1.plot(t, y1, c='red') ax2.plot(t, y2, c='orange') plt.tight_layout() plt.show()
Kết quả
Hai subplot chồng dọc, cùng trục X (chia sẻ), trục Y riêng biệt.