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

กราฟความถี่ (Frequency Plot) ใน Python/Pandas DataFrame ด้วย Matplotlib


เพื่อแสดงกราฟความถี่ใน Python/Pandas DataFrame ด้วย Matplotlib

  • ตั้งค่าขนาดรูปและ padding ระหว่าง subplots
  • สร้าง figure และ subplots
  • สร้าง DataFrame 2 มิติ
  • คืน Series ที่มีจำนวนค่าที่ไม่ซ้ำด้วย value_counts()
  • แสดงรูปด้วย show()

ตัวอย่าง

import pandas as pd
from matplotlib import pyplot as plt

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

fig, ax = plt.subplots()
df = pd.DataFrame({'numbers': [2, 4, 1, 4, 3, 2, 1, 3, 2, 4]})
df['numbers'].value_counts().plot(ax=ax, kind='bar', xlabel='numbers', ylabel='frequency')
plt.show()

ผลลัพธ์

กราฟความถี่ (Frequency Plot) ใน Python/Pandas DataFrame ด้วย Matplotlib