Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การปรับความสูง Subplot แบบ Absolute (ไม่ใช่ Relative) ใน Matplotlib

การตั้งค่าความสูง Subplot แบบ Absolute Coordinates

ใช้คลาส Axes() โดยตรง ระบุตำแหน่ง [left, bottom, width, height] ใน figure coordinates (0-1)

ขั้นตอนหลัก

  • ตั้งค่าขนาดรูปและ padding
  • สร้าง figure
  • สร้าง Axes แรก: pl.Axes(figure, [.4, .6, .25, .25]) (left=0.4, bottom=0.6, width=0.25, height=0.25)
  • เพิ่ม axes ด้วย figure.add_axes()
  • พล็อตข้อมูล
  • สร้าง Axes ที่สอง: pl.Axes(figure, [.4, .1, .25, .25]) (bottom=0.1)
  • เพิ่มและพล็อต
  • แสดงผลด้วย show()

ตัวอย่างโค้ด

from matplotlib import pyplot as pl

pl.rcParams["figure.figsize"] = [7.50, 4.50]
pl.rcParams["figure.autolayout"] = True

figure = pl.figure()
axes = pl.Axes(figure, [.4, .6, .25, .25])
figure.add_axes(axes)
pl.plot([1, 2, 3, 4], [1, 2, 3, 4])

axes = pl.Axes(figure, [.4, .1, .25, .25])
figure.add_axes(axes)
pl.plot([1, 2, 3, 4], [1, 2, 3, 4])

pl.show()

คำอธิบาย

พิกัด [left, bottom, width, height] เป็น fraction ของ figure width/height (0-1) แบบ absolute ไม่ขึ้นกับ subplots อื่นๆ