Matplotlib การเพิ่มเส้นกริด
เพิ่มเส้นกริดให้กับพล็อต
ด้วย Pyplot คุณสามารถใช้grid()
ฟังก์ชันนี้เพื่อเพิ่มเส้นกริดให้กับพล็อตได้
ตัวอย่าง
เพิ่มเส้นกริดให้กับพล็อต:
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")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid()
plt.show()
ผลลัพธ์:
ระบุเส้นกริดที่จะแสดง
คุณสามารถใช้axis
พารามิเตอร์ในgrid()
ฟังก์ชันเพื่อระบุเส้นกริดที่จะแสดง
ค่าทางกฎหมายคือ: 'x', 'y' และ 'ทั้ง' ค่าเริ่มต้นคือ 'ทั้งสอง'
ตัวอย่าง
แสดงเส้นกริดสำหรับแกน x เท่านั้น:
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")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(axis = 'x')
plt.show()
ผลลัพธ์:
ตัวอย่าง
แสดงเส้นกริดสำหรับแกน 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.title("Sports Watch Data")
plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(axis = 'y')
plt.show()
ผลลัพธ์:
ตั้งค่าคุณสมบัติของเส้นสำหรับ Grid
คุณยังสามารถตั้งค่าคุณสมบัติเส้นของเส้นตารางได้ดังนี้: grid(color = ' color ', linestyle = ' linestyle ', linewidth = number )
ตัวอย่าง
ตั้งค่าคุณสมบัติเส้นของกริด:
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")
plt.xlabel("Average
Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x,
y)
plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)
plt.show()