การสร้างฮิสโตแกรม

Statistical Thinking in Python (ตอนที่ 1)

Justin Bois

Teaching Professor at the California Institute of Technology

ผลการเลือกตั้งรัฐสวิงของสหรัฐฯ ปี 2008

ข้อมูลจาก Data.gov (https://www.data.gov/)

ch1-2.003.png

Statistical Thinking in Python (ตอนที่ 1)

การสร้างฮิสโตแกรม

import matplotlib.pyplot as plt
_ = plt.hist(df_swing['dem_share'])
_ = plt.xlabel('percent of vote for Obama')
_ = plt.ylabel('number of counties')
plt.show()
Statistical Thinking in Python (ตอนที่ 1)

อย่าลืมใส่ชื่อแกนเสมอ

Statistical Thinking in Python (ตอนที่ 1)

ผลการเลือกตั้งรัฐสวิงของสหรัฐฯ ปี 2008

ข้อมูลจาก Data.gov (https://www.data.gov/)

ch1-2.013.png

Statistical Thinking in Python (ตอนที่ 1)

ฮิสโตแกรมที่มีการแบ่ง bin ต่างกัน

ข้อมูลจาก Data.gov (https://www.data.gov/)

ch1-2.015.png

Statistical Thinking in Python (ตอนที่ 1)

การกำหนด bin ของฮิสโตแกรม

bin_edges = [0, 10, 20, 30, 40, 50,
                60, 70, 80, 90, 100]
_ = plt.hist(df_swing['dem_share'], bins=bin_edges)
plt.show()

ch1-2.022.png

Statistical Thinking in Python (ตอนที่ 1)

การกำหนด bin ของฮิสโตแกรม

_ = plt.hist(df_swing['dem_share'], bins=20)
plt.show()

ch1-2.027.png

Statistical Thinking in Python (ตอนที่ 1)

Seaborn

  • แพ็กเกจวิเคราะห์ข้อมูลเชิงสถิติที่พัฒนาบน Matplotlib โดย Michael Waskom
Statistical Thinking in Python (ตอนที่ 1)

การตั้งค่าสไตล์ Seaborn

import seaborn as sns
sns.set()
_ = plt.hist(df_swing['dem_share'])
_ = plt.xlabel('percent of vote for Obama')
_ = plt.ylabel('number of counties')
plt.show()
Statistical Thinking in Python (ตอนที่ 1)

ฮิสโตแกรมในสไตล์ Seaborn

ch1-2.038.png

1 ข้อมูลจาก Data.gov (https://www.data.gov/)
Statistical Thinking in Python (ตอนที่ 1)

มาฝึกกันเถอะ!

Statistical Thinking in Python (ตอนที่ 1)

Preparing Video For Download...