การสร้าง Stacked Horizontal Bar Chart
ใช้ barh() สองครั้ง ครั้งที่สองระบุ left parameter เป็นค่าของครั้งแรก
ขั้นตอนหลัก
- ตั้งค่าขนาดรูปและ padding
- เตรียมข้อมูล year, issues_addressed, issues_pending
- พล็อต barh แรก (completed) สีแดง
- พล็อต barh ที่สอง (pending) left=issues_addressed สีเหลือง
- เพิ่ม legend และ title
- แสดงผลด้วย show()
ตัวอย่างโค้ด
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
year = [2014, 2015, 2016, 2017, 2018, 2019]
issues_addressed = [10, 14, 0, 10, 15, 15]
issues_pending = [5, 10, 50, 2, 0, 10]
b1 = plt.barh(year, issues_addressed, color="red")
b2 = plt.barh(year, issues_pending, left=issues_addressed, color="yellow")
plt.legend([b1, b2], ["Completed", "Pending"], title="Issues", loc="upper right")
plt.show()
คำอธิบาย
left parameter กำหนดตำแหน่งเริ่มต้นของแท่งที่สอง ทำให้ stacking ได้ถูกต้อง