在 matplotlib 中,n、bins 和 patches 是什麼?


hist() 方法在 matplotlib 中返回 n、binspatches。如果有多個輸入資料集,Patches 是用於建立直方圖的各個 artists 的容器,或此類容器的列表。Bins 定義了範圍內等寬度的 bin 數量。

讓我們舉個例子來了解其工作原理。

步驟

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

  • 使用 numpy 建立隨機資料點。

  • 繪製一個包含 100 個 bin 的直方圖。

  • 設定一個 artist 物件上的屬性。

  • 使用 show() 方法顯示圖形。

示例

import numpy as np
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

x = np.random.normal(size=100)

n, bins, patches = plt.hist(x, bins=100)

plt.setp(patches[0], 'facecolor', 'yellow')

plt.show()

輸出

更新於: 2021-08-09

2K+ 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.