Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - วาด Stacked Horizontal Bar Chart

บทนำ

Stacked Horizontal Bar Chart ใช้ barh() ตั้ง stacked=True

ขั้นตอน

  1. นำเข้าไลบรารี
  2. สร้าง DataFrame ข้อมูลรถยนต์
  3. วาด Stacked Horizontal Bar Chart:
    dataFrame.plot.barh(stacked=True, title='Car Specifications', 
        color=("orange", "cyan"))

โค้ดเต็ม

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

ผลลัพธ์

ได้ Stacked Horizontal Bar Chart แสดง Cubic Capacity และ Reg_Price สะสมกัน