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

Python Pandas - Vẽ biểu đồ cột ngang chồng (Stacked Horizontal Bar Chart)


Để vẽ stacked horizontal bar chart, dùng barh() với tham số stacked=True.

Nhập thư viện:

import pandas as pd 
import matplotlib.pyplot as plt

Tạo DataFrame 3 cột:

dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'],
    "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]})

Vẽ stacked horizontal bar chart:

dataFrame.plot.barh(stacked=True, title='Car Specifications', color=("orange", "cyan"))

Ví dụ

import pandas as pd 
import matplotlib.pyplot as plt 
dataFrame = pd.DataFrame({"Car": ['Bentley', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Jaguar'],
    "Cubic_Capacity": [2000, 1800, 1500, 2500, 2200, 3000],
    "Reg_Price": [7000, 1500, 5000, 8000, 9000, 6000]}) 
dataFrame.plot.barh(stacked=True, title='Car Specifications', color=("orange", "cyan")) 
plt.show()

Kết quả

Python Pandas - Vẽ biểu đồ cột ngang chồng (Stacked Horizontal Bar Chart)