如何在 Python 中绘制带有异常值和四分位数的箱线图

时间:2021-03-25 06:53:24

标签: python data-mining boxplot

我的箱线图显示正确,但我无法理解异常值和四分位数.. 我的箱线图如下所示 enter image description here

这是我的代码

df = pd.read_csv(r'posts_dataset.csv')
df.boxplot(by='city', column=['price'], color='red')
plt.show()

如何将其绘制为其他具有异常值和矩形形状的正式箱线图?

1 个答案:

答案 0 :(得分:1)

很可能你的 price 有一些巨大的异常值或者它有一个沉重的尾巴,例如:

df = pd.DataFrame({'price':np.random.negative_binomial(0.1, 0.0001,5000),
                   'city':np.random.choice(['A','B','C'],5000)})
df.hist(column='price')

enter image description here

如果你做一个箱线图它会被压扁:

df.boxplot(by='city', column=['price'], color='red')

enter image description here

一种选择是采用 log10 :

df['log_price'] = np.log10(df['price']+1)
df.boxplot(by='city', column='log_price', color='red')

enter image description here