การเปรียบเทียบเชิงปริมาณ: แผนภูมิแท่ง

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

Ariel Rokem

Data Scientist

เหรียญโอลิมปิก

,Gold, Silver, Bronze
United States, 137, 52, 67
Germany, 47, 43, 67
Great Britain, 64, 55, 26
Russia, 50, 28, 35
China, 44, 30, 35
France, 20, 55, 21
Australia, 23, 34, 25
Italy, 8, 38, 24
Canada, 4, 4, 61
Japan, 17, 13, 34
การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

เหรียญโอลิมปิก: แสดงข้อมูลด้วยกราฟ

medals = pd.read_csv('medals_by_country_2016.csv', index_col=0)

fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"]) plt.show()

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

เสริม: หมุนป้ายกำกับแกน

fig, ax = plt.subplots()
ax.bar(medals.index, medals["Gold"])

ax.set_xticklabels(medals.index, rotation=90) ax.set_ylabel("Number of medals")
plt.show()

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

เหรียญโอลิมปิก: แสดงเหรียญประเภทอื่น

fig, ax = plt.subplots
ax.bar(medals.index, medals["Gold"])

ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"])
ax.set_xticklabels(medals.index, rotation=90) ax.set_ylabel("Number of medals") plt.show()

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

เหรียญโอลิมปิก: แสดงครบทั้งสามประเภท

fig, ax = plt.subplots
ax.bar(medals.index, medals["Gold"])

ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"])

ax.bar(medals.index, medals["Bronze"], bottom=medals["Gold"] + medals["Silver"])
ax.set_xticklabels(medals.index, rotation=90) ax.set_ylabel("Number of medals") plt.show()
การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

แผนภูมิแท่งแบบซ้อน

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

การเพิ่มคำอธิบายสัญลักษณ์

fig, ax = plt.subplots
ax.bar(medals.index, medals["Gold"])
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"])
ax.bar(medals.index, medals["Bronze"],
       bottom=medals["Gold"] + medals["Silver"])

ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")
การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

การเพิ่มคำอธิบายสัญลักษณ์

fig, ax = plt.subplots
ax.bar(medals.index, medals["Gold"], label="Gold")
ax.bar(medals.index, medals["Silver"], bottom=medals["Gold"], 
       label="Silver")
ax.bar(medals.index, medals["Bronze"],
       bottom=medals["Gold"] + medals["Silver"], 
       label="Bronze")

ax.set_xticklabels(medals.index, rotation=90)
ax.set_ylabel("Number of medals")

ax.legend() plt.show()
การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

แผนภูมิแท่งแบบซ้อนพร้อมคำอธิบายสัญลักษณ์

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

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

การสร้างภาพข้อมูลด้วย Matplotlib เบื้องต้น

Preparing Video For Download...