Matplotlib 中的水平堆積條形圖
要在 Matplotlib 中繪製堆積條形圖,我們可以使用 barh() 方法
步驟
- 設定圖形大小並調整子圖之間的填充和周圍的填充。
- 建立一個包含 年份、已解決的問題 和 未解決的問題,並與年份相對應的列表。
- 使用 年份 和 已解決的問題 資料繪製水平條形圖。
- 要製作堆積式水平條形圖,請使用 barh() 方法與 年份、未解決的問題 和 已解決的問題 資料一起使用
- 將圖例放在圖上。
- 要顯示圖形,請使用 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()
輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP