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

การพล็อตกราฟสะสม (Cumulative Graph) ของข้อมูลวันที่ใน Python ด้วย Matplotlib


ขั้นตอนการทำงาน

  • กำหนดขนาดรูปภาพและปรับระยะห่างระหว่างและรอบๆ subplots
  • สร้าง DataFrame ของ Pandas พร้อมข้อมูลเวลาและจำนวนนักศึกษาที่เข้ารับการศึกษา
  • พล็อต DataFrame ด้วยเมธอด plot() โดยกำหนด kind='bar'
  • ใช้เมธอด show() เพื่อแสดงรูปภาพ

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

import pandas as pd
from matplotlib import pyplot as plt

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

college_student_data = {'durations': [1, 2, 2.5, 3, 4.5, 5, 5.5, 6, 6.5, 7], 'admissions': [5, 50, 150, 3380, 760, 340, 115, 80, 40, 10]}
df = pd.DataFrame(data=college_student_data)
df.plot(kind='bar', x='durations', y='admissions', legend=False, color='red', rot=0)
plt.show()

ผลลัพธ์

การพล็อตกราฟสะสม (Cumulative Graph) ของข้อมูลวันที่ใน Python ด้วย Matplotlib