Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Vẽ đồ thị tích lũy dữ liệu datetime trong Matplotlib

Để vẽ biểu đồ tích lũy (cumulative) cho dữ liệu thời gian (datetime), ta sử dụng Pandas DataFrame và plot kind='bar'.

Các bước

  • Thiết lập figure size và autolayout.
  • Tạo DataFrame với cột thời gian (durations) và số lượng (admissions).
  • Vẽ biểu đồ cột với x='durations', y='admissions'.
  • Hiển thị bằng show().

Ví dụ

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()

Kết quả

Vẽ đồ thị tích lũy dữ liệu datetime trong Matplotlib