Matplotlib Labels และ Title
สร้างป้ายกำกับสำหรับพล็อต
ด้วย Pyplot คุณสามารถใช้ ฟังก์ชัน xlabel()
และ
ylabel()
เพื่อกำหนดป้ายกำกับสำหรับแกน x และ y
ตัวอย่าง
เพิ่มป้ายกำกับให้กับแกน x และ y:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
ผลลัพธ์:
สร้างชื่อเรื่องสำหรับพล็อต
ด้วย Pyplot คุณสามารถใช้title()
ฟังก์ชันนี้เพื่อตั้งชื่อเรื่องสำหรับพล็อตได้
ตัวอย่าง
เพิ่มชื่อพล็อตและป้ายกำกับสำหรับแกน x และ y:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.plot(x, y)
plt.title("Sports Watch Data")
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.show()
ผลลัพธ์:
ตั้งค่าคุณสมบัติแบบอักษรสำหรับชื่อเรื่องและป้ายกำกับ
คุณสามารถใช้fontdict
พารามิเตอร์ใน
xlabel()
, ylabel()
และtitle()
เพื่อตั้งค่าคุณสมบัติฟอนต์สำหรับชื่อและป้ายกำกับ
ตัวอย่าง
ตั้งค่าคุณสมบัติแบบอักษรสำหรับชื่อและป้ายกำกับ:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}
plt.title("Sports
Watch Data", fontdict = font1)
plt.xlabel("Average Pulse", fontdict =
font2)
plt.ylabel("Calorie Burnage", fontdict = font2)
plt.plot(x,
y)
plt.show()
ผลลัพธ์:
ตำแหน่งชื่อเรื่อง
คุณสามารถใช้loc
พารามิเตอร์
title()
เพื่อจัดตำแหน่งหัวเรื่อง
ค่าทางกฎหมายคือ: 'ซ้าย', 'ขวา' และ 'กลาง' ค่าเริ่มต้นคือ 'ศูนย์'
ตัวอย่าง
วางตำแหน่งชื่อเรื่องไปทางซ้าย:
import numpy as np
import matplotlib.pyplot as plt
x = np.array([80,
85, 90, 95, 100, 105, 110, 115, 120, 125])
y = np.array([240, 250, 260,
270, 280, 290, 300, 310, 320, 330])
plt.title("Sports Watch Data", loc = 'left')
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.show()