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

วิธีสร้าง histogram จากรายการข้อมูลใน Matplotlib


สร้าง histogram จาก list ข้อมูล

ใช้เมธอด hist() วาด histogram จากข้อมูล list

ขั้นตอน

  1. สร้าง list ข้อมูล (x data points)
  2. วาด histogram ด้วย hist()
  3. แสดงด้วย show()

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

from matplotlib import pyplot as plt

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

x = [[300, 400, 500, 2000, 10], 
     [300, 400, 500, 2000, 10], 
     [300, 400, 500, 2000, 10], 
     [300, 400, 500, 2000, 10], 
     [300, 400, 500, 2000, 10]]

plt.hist(x, 10)
plt.show()

ผลลัพธ์

วิธีสร้าง histogram จากรายการข้อมูลใน Matplotlib