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

輸出

更新於: 2021 年 6 月 15 日

超過 1 萬次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.