แบ่งปันแกน x ระหว่าง subplot2grid
ใช้พารามิเตอร์ sharex ใน subplot2grid ที่สอง
ขั้นตอน
- สร้างข้อมูลสุ่ม t, x, y1, y2 ด้วย NumPy
- สร้าง figure ด้วย figure()
- สร้าง subplot แรกที่ตำแหน่งเฉพาะ colspan=3, rowspan=2
- สร้าง subplot ที่สอง sharex=ax1 (subplot แรก)
- วาดกราฟด้วย t, y1 และ y2
- ปรับ padding ด้วย tight_layout()
- แสดงด้วย show()
ตัวอย่างโค้ด
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()
ผลลัพธ์
