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

การแสดง Tick Labels เมื่อใช้ Shared Axis ใน Matplotlib

เมื่อใช้ plt.subplot() พร้อม sharey (หรือ sharex) ตามค่าเริ่มต้น tick labels จะแสดงบน axes ทุกตัว ไม่ซ่อนอัตโนมัติเหมือน subplots() ทำให้เห็นป้ายทุกซับพลอต

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

  1. ตั้งค่าขนาดรูปและเลย์เอาต์
  2. สร้าง ax1 ด้วย plt.subplot(1, 2, 1)
  3. สร้าง ax2 ด้วย plt.subplot(1, 2, 2, sharey=ax1)
  4. พลอตข้อมูลทั้งสอง axes
  5. แสดงผล - tick labels แสดงทั้งคู่

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

from matplotlib import pyplot as plt

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

ax1 = plt.subplot(1, 2, 1)
ax1.plot([1, 4, 9])

ax2 = plt.subplot(1, 2, 2, sharey=ax1)
ax2.plot([1, 8, 27])

plt.show()

ผลลัพธ์

การแสดง Tick Labels เมื่อใช้ Shared Axis ใน Matplotlib