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

การวาง Title ที่ด้านล่าง Figure ใน Matplotlib

ใช้พารามิเตอร์ y ใน plt.title() ค่าลบจะเลื่อน Title ลงด้านล่าง

ขั้นตอน

  1. ตั้งค่าขนาด Figure
  2. สร้างข้อมูล Scatter แบบสุ่ม
  3. Plot Scatter
  4. ตั้ง Title ด้วย y=-0.01
  5. แสดงผล

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


import numpy as np
from matplotlib import pyplot as plt

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

N = 100
x = np.random.rand(N)
y = np.random.rand(N)

plt.scatter(x, y, c=x, s=x*100+1, cmap="plasma")

# y=-0.01 วาง Title ที่ด้านล่างนอกพื้นที่ Plot
plt.title('Scatter plot', y=-0.01)

plt.show()

การปรับตำแหน่ง Title อื่นๆ


# ด้านบนนอกพื้นที่ (Default ~1.0)
plt.title('Top Title', y=1.05)

# ด้านล่างนอกพื้นที่
plt.title('Bottom Title', y=-0.1)

# ใช้ suptitle สำหรับ Figure Title
fig.suptitle('Figure Title', y=0.95)

# ใช้ ax.set_title กับ pad
ax.set_title('Title with pad', pad=-20)

ผลลัพธ์

Scatter Plot พร้อม Title "Scatter plot" อยู่ด้านล่างของกราฟ