用 Python Matplotlib 繪製輪廓直方圖


在輪廓直方圖中,每個箱包含其項的均值。若要使用 Python 繪製輪廓直方圖,我們可以使用 **Seaborn** 的**regplot** 方法。

步驟

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

  • 使用 numpy 建立 **x** 和 **y** 資料點。

  • 使用 **seaborn.regplot** 來繪製資料和線性迴歸模型擬合。使用引數 **x_bins** 將 x 變數分成離散的箱子。使用 **fit_reg=True** 來繪製將 **x** 和 **y** 變數關聯起來的迴歸模型。

  • 若要顯示圖形,請使用 **show()** 方法。

示例

import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt

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

x = np.random.uniform(-5, 5, 1000)
y = np.random.normal(x**2, np.abs(x) + 1)

sns.regplot(x=x, y=y, x_bins=20, marker='o', fit_reg=True)

plt.show()

輸出

將產生以下輸出 −

更新於:19-Oct-2021

580 次瀏覽

開啟你的職業生涯

透過完成課程獲取認證

開始吧
廣告
© . All rights reserved.