如何在 Seaborn 分佈圖中填充曲線下的面積?


若要在 Seaborn 分佈圖中填充曲線下的面積,我們可以使用 distplot()fill_between() 方法。

步驟

  • 設定圖形大小並調整子圖之間及周圍的填充。
  • 建立資料點列表。
  • 繪製觀察值的一元分佈。
  • 若要填充曲線下的面積,請使用 fill_between() 方法。
  • 設定或檢索自動調整邊距,x=0 和 y=0。
  • 若要顯示圖形,請使用 show() 方法。

示例

import seaborn as sns
import scipy.stats as stats
import matplotlib.pyplot as plt

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

x = [2.0, 7.5, 9.0, 8.5]
ax = sns.distplot(x, fit_kws={"color": "red"}, kde=False, fit=stats.gamma, hist=None, label="label 1")
l1 = ax.lines[0]

x1 = l1.get_xydata()[:, 0]
y1 = l1.get_xydata()[:, 1]

ax.fill_between(x1, y1, color="red", alpha=0.3)
ax.margins(x=0, y=0)

plt.show()

輸出

更新於:2021-08-03

3K+ 瀏覽量

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告