盒圖主要是看一個數據的分佈情況,也適用於EDA異常值的查找
Q1是四分之一分爲數
Q3是四分之三分爲數
median中位數
IQR=
基本圖形
fig = plt.figure(figsize=(8,6))
plt.boxplot(data,notch=False,sym='s',vert=True)
plt.xticks([y+1 for y in range(len(data))],['x1','x2','x3'])
plt.xlabel('x')
plt.title('box plot')
線條顏色
我們可以設置線條的顯示顏色
fig = plt.figure(figsize=(8,6))
bplot=plt.boxplot(data,notch=False,sym='s',vert=True)
plt.xticks([y+1 for y in range(len(data))],['x1','x2','x3'])
plt.xlabel('x')
plt.title('box plot')
for components in bplot.keys():
for line in bplot[components]:
line.set_color('blue')
橫圖
fig = plt.figure(figsize = (8,6))
plt.boxplot(data,notch=False,sym='s',vert=False)
plt.yticks([y+1 for y in range(len(data))],['x1','x2','x3'])
plt.ylabel('x')
plt.title('box plot')
分別配色
fig = plt.figure(figsize = (8,6))
bplot = plt.boxplot(data,notch=False,sym='s',vert=True,patch_artist=True)
plt.xticks([y+1 for y in range(len(data))],['x1','x2','x3'])
plt.xlabel('x')
plt.title('box plot')
colors = ['pink','lightblue','lightgreen']
for pathch,color in zip(bplot['boxes'],colors):
pathch.set_facecolor(color)
小提琴圖
fig,axes = plt.subplots(nrows=1,ncols=2,figsize=(12,5))
axes[0].violinplot(data,showmeans=False,showmedians=True)
axes[0].set_title('violin plot')
axes[1].boxplot(data)
axes[1].set_title('box plot')