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

Cách vẽ biểu đồ cột từ Pandas Series trong Matplotlib

Vẽ bar chart từ Pandas Series/DataFrame:

  • Tạo dictionary dữ liệu với các khóa trong khoảng 1-10.
  • Tạo DataFrame từ dictionary.
  • Sử dụng plot(kind="bar") để vẽ biểu đồ cột.
  • Hiển thị bằng show().

Ví dụ

import pandas as pd
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
d = {'y=1/x': [1 / i for i in range(1, 10)],
     'y=x': [i for i in range(1, 10)],
     'y=x^2': [i * i for i in range(1, 10)],
     'y=x^3': [i * i * i for i in range(1, 10)]}
df = pd.DataFrame(d)
df.plot(kind='bar')
plt.show()

Kết quả

Biểu đồ cột nhóm (grouped bar chart) từ DataFrame Pandas.