2021-06-29
閱讀量:
1324
在matplotlib中,如何在箱線圖中顯示 上下四分位,中位數(shù) 等數(shù)值標(biāo)簽?
問:
在matplotlib中,如何在箱線圖中顯示 上下四分位,中位數(shù) 等數(shù)值標(biāo)簽?
答:
在在matplotlib中沒有直接的函數(shù)或參數(shù)設(shè)置數(shù)值標(biāo)簽,可以通過添加文本函數(shù)text(),先把箱線圖的分位數(shù)求出,再寫個循環(huán)逐一把分位數(shù)以文本的形式顯示在圖上,代碼如下:
x = np.random.randn(1000)
plt.figure(figsize=(8, 6), dpi=80)
plt.boxplot(x,
notch=True, # 中位數(shù)處是否凹陷
sym='o', # 指定異常點的形狀
vert=True, # 是否需要將箱線圖垂直擺放,默認(rèn)垂直擺放.False水平擺放
whis=1.5, # 指定上下須與上下四分位的距離,默認(rèn)為1.5倍的四分位差
);
percentiles = np.array([2.5, 25, 50, 75, 97.5])
ptiles_vers = np.percentile(x, percentiles)
ptiles_vers
for i in ptiles_vers:
plt.text(1.1, i, "{}".format(round(i, 2) ))






評論(0)


暫無數(shù)據(jù)
推薦帖子
0條評論
0條評論
0條評論