Python數(shù)據(jù)可視化(七):箱線圖繪制

使用seaborn包繪制箱線圖

# libraries & dataset
import seaborn as sns
import matplotlib.pyplot as plt
# set a grey background (use sns.set_theme() if seaborn version 0.11.0 or above) 
sns.set(style="darkgrid")
# 加載示例數(shù)據(jù)集
df = sns.load_dataset('iris')
df.head()
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
# 繪制基礎(chǔ)箱線圖
sns.boxplot(y=df["sepal_length"])
plt.show()
image.png
# 繪制多個(gè)變量的箱線圖
sns.boxplot(data=df.loc[:, ['sepal_length', 'sepal_width']])
plt.show()
image.png
# 繪制分組箱線圖
sns.boxplot(x=df["species"], y=df["sepal_length"])
plt.show()
image.png
# 添加擾動(dòng)點(diǎn)
# boxplot
ax = sns.boxplot(x='species', y='sepal_length', data=df)
# add stripplot
ax = sns.stripplot(x='species', y='sepal_length', data=df, color="orange", jitter=0.2, size=4)

# add title
plt.title("Boxplot with jitter", loc="left")

# show the graph
plt.show()
image.png
# 設(shè)置linewidth參數(shù)自定義邊框線的寬度
sns.boxplot(x=df["species"], y=df["sepal_length"], linewidth=5)
plt.show()
image.png
# 設(shè)置notch=True參數(shù)添加缺口
sns.boxplot(x=df["species"], y=df["sepal_length"], notch=True)
plt.show()
image.png
# 設(shè)置width參數(shù)之定義箱型的寬度
sns.boxplot(x=df["species"], y=df["sepal_length"], width=0.3)
plt.show()
image.png
# 自定義顏色
# 設(shè)置palette參數(shù)自定義顏色畫板
sns.boxplot(x=df["species"], y=df["sepal_length"], palette="Blues")
plt.show()
image.png
# 設(shè)置color參數(shù)自定義顏色
sns.boxplot(x=df["species"], y=df["sepal_length"], color='skyblue')
plt.show()
image.png
# 對(duì)每組設(shè)置不同的顏色
my_pal = {"versicolor": "g", "setosa": "b", "virginica":"m"}
sns.boxplot(x=df["species"], y=df["sepal_length"], palette=my_pal)
plt.show()
image.png
# 設(shè)置order參數(shù)自定義分組的排序
sns.boxplot(x='species', y='sepal_length', data=df, order=["versicolor", "virginica", "setosa"])
plt.show()
image.png
# 根據(jù)每組的中位數(shù)進(jìn)行降序排序
# Find the order
my_order = df.groupby(by=["species"])["sepal_length"].median().iloc[::-1].index

# Give it to the boxplot
sns.boxplot(x='species', y='sepal_length', data=df, order=my_order)
plt.show()
image.png
# 添加文本注釋標(biāo)簽
ax = sns.boxplot(x="species", y="sepal_length", data=df)

# Calculate number of obs per group & median to position labels
medians = df.groupby(['species'])['sepal_length'].median().values
nobs = df['species'].value_counts().values
nobs = [str(x) for x in nobs.tolist()]
nobs = ["n: " + i for i in nobs]

# Add it to the plot
pos = range(len(nobs))
for tick,label in zip(pos,ax.get_xticklabels()):
    ax.text(pos[tick],
            medians[tick] + 0.03,
            nobs[tick],
            horizontalalignment='center',
            size='x-small',
            color='w',
            weight='semibold')

plt.show()
image.png
# 按分組變量填充顏色
df = sns.load_dataset('tips')
df.head()
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
sns.boxplot(x="day", y="total_bill", hue="smoker", data=df, palette="Set1", width=0.5)
plt.show()
image.png
# Grouped violinplot
sns.violinplot(x="day", y="total_bill", hue="smoker", data=df, palette="Pastel1")
plt.show()
image.png

參考來(lái)源:https://www.python-graph-gallery.com/boxplot/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容