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

Python - Vẽ biểu đồ tròn (Pie Chart) cho Pandas DataFrame với Matplotlib


Vẽ Pie Chart dùng plt.pie(). Biểu đồ tròn thể hiện tỷ lệ dữ liệu số trong một cột.

Nhập thư viện:

import pandas as pd 
import matplotlib.pyplot as plt

Tạo DataFrame:

dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]})

Vẽ Pie Chart cột Reg_Price, nhãn cột Car:

plt.pie(dataFrame["Reg_Price"], labels=dataFrame["Car"])

Ví dụ

import pandas as pd 
import matplotlib.pyplot as plt 
dataFrame = pd.DataFrame({"Car": ['BMW', 'Lexus', 'Tesla', 'Mustang', 'Mercedes', 'Jaguar'],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]}) 
plt.pie(dataFrame["Reg_Price"], labels=dataFrame["Car"]) 
plt.show()

Kết quả

Python - Vẽ biểu đồ tròn (Pie Chart) cho Pandas DataFrame với Matplotlib