如何在 Python matplotlib 中繪製列表的柱狀圖?


要在 Python matplotlib 中為列表繪製柱狀圖,我們可以採取以下步驟。

步驟

  • 設定圖形大小並調整子圖之間和周圍的填充。

  • 列出資料點。

  • 用資料繪製柱狀圖。

  • 要顯示圖形,請使用 Show() 方法。

示例

from matplotlib import pyplot as plt

# Set the figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# List of data points
data = [0, 1, 3, 2, 1, 5, 2, 1, 4, 2, 4, 0]

# Plot bar chart with data points
plt.bar(data, data)

# Display the plot
plt.show()

輸出

它將產生以下輸出 -

更新時間:2021 年 10 月 8 日

8K+ 次瀏覽

啟動你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.