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

การตั้งค่า Active Subplot ด้วย Axes Object ใน Matplotlib


ใช้เมธอด subplots() เพื่อเพิ่ม axes เป็นการจัดเรียง subplot

ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้างรายการ x และ y สำหรับจุดข้อมูล
  • สร้าง figure และชุด subplots ด้วย subplots() 1 แถว 2 คอลัมน์
  • เพิ่ม axes ลงใน figure ปัจจุบันและทำให้เป็น axes ปัจจุบัน ด้วย axes object ตำแหน่งที่ 0
  • พล็อต x และ y ด้วย plot()
  • เพิ่ม axes ลงใน figure ปัจจุบันและทำให้เป็น axes ปัจจุบัน ด้วย axes object ตำแหน่งที่ 1
  • พล็อต x และ y ด้วย plot()
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = [1, 2, 4, 2, 1]
y = [1, 4, 0, 4, 1]
fig, axs = plt.subplots(1, 2)
plt.axes(axs[0])
plt.plot(x, y)
plt.axes(axs[1])
plt.plot(y, x)
plt.show()

ผลลัพธ์

การตั้งค่า Active Subplot ด้วย Axes Object ใน Matplotlib