ในการรวมหลาย matplotlib axes subplots เป็น figure เดียว เราสามารถใช้เมธอด subplots() พร้อม nrow=2
ขั้นตอน
- ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots
- สร้างจุดข้อมูล x, y1 และ y2 ด้วย numpy
- สร้าง figure และชุด subplots
- พลอต x, y1 และ y2 ด้วยเมธอด plot()
- เพื่อแสดงรูป ใช้เมธอด show()
ตัวอย่าง
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()
ผลลัพธ์
