Để vẽ biểu đồ cột ngang, dùng pandas.DataFrame.plot.barh. Biểu đồ cột so sánh các nhóm rời rạc.
Nhập thư viện:
import pandas as pd import matplotlib.pyplot as plt
Tạo DataFrame 4 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],
"Units_Sold": [100, 110, 150, 80, 200, 90]}) Vẽ biểu đồ cột ngang:
dataFrame.plot.barh(x='Car', y='Cubic_Capacity', title='Car Specifications', color='blue')
Ví dụ
import pandas as pd
import matplotlib.pyplot as plt
# Tạo DataFrame
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],
"Units_Sold": [100, 110, 150, 80, 200, 90]})
# Vẽ biểu đồ cột ngang
dataFrame.plot.barh(x='Car', y='Cubic_Capacity', title='Car Specifications', color='blue')
plt.xlabel("CC (Cubic Capacity)")
plt.show() Kết quả
