说明:代码运行环境为 Win10+Python3+jupyter notebook
箱形图简介:
因为箱形图是基于五数概括法的图形汇总,在介绍箱形图前,先简单介绍一下五数概括法。
五数概括法使用下面五个数来汇总数据:
(1)最小值
(2)第一四分位数(Q1)
(3)中位数(Q2)
(4)第三四分位数(Q3)
(5)最大值
而箱形图就是五数概括法的图形表示。如下图所示:
(图片来源:https://blog.csdn.net/weixin_39501270/article/details/77369597)
箱形图的应用:
(1)可以作为一种检测异常值的方法;
(2)用于多组数据的图形汇总,便于对各组数据进行直观比较分析。
箱形图的绘制方法主要有:
方法1:利用pandas包中的Series.plot()、DataFrame.plot()或DataFrame.boxplot()方法;
方法2:利用seaborn包中的cataplot()或者boxplot(),其中seaborn.boxplot()是seaborn.cataplot()的参数kind='box'时的一种情况;
方法3:利用matplotlib包中axes对象的boxplot()方法。
导出需要的各种包,并准备好数据:
%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as
tips = pd.read_csv('examples/tips.csv')
tips['tip_pct'] = tips['tip'] / (tips['total_bill'] - tips['tip'])
tips的表结构为:
方法1具体示例:
Series.plot()示例:
fig,axes = plt.subplots()
tips['tip_pct'][tips.tip_pct < 0.5].plot(kind='box',ax=axes)
axes.set_ylabel('values of tip_pct')
fig.savefig('p1.png') # 将绘制的图形保存为p1.png
上述代码绘制的图形为:
Series.plot()的用法具体参考:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.plot.html#pandas.Series.plot
DataFrame.plot()示例:
fig,axes = plt.subplots(1,4)
color = dict(boxes='DarkGreen', whiskers='DarkOrange',
medians='DarkBlue', caps='Red')
boxes表示箱体,whisker表示触须线
medians表示中位数,caps表示最大与最小值界限
tips.plot(kind='box',ax=axes,subplots=True,
title='Different boxplots',color=color,sym='r+')
sym参数表示异常值标记的方式
axes[0].set_ylabel('values of total_bill')
axes[1].set_ylabel('values of tip')
axes[2].set_ylabel('values of size')
axes[3].set_ylabel('values of tips_pct')
fig.subplots_adjust(wspace=1,hspace=1) # 调整子图之间的间距
fig.savefig('p2.png') # 将绘制的图片保存为p2.png
上述代码绘制的图形为:
DataFrame.plot()的用法具体参考:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.plot.html
DataFrame.boxplot()示例:
fig,axes = plt.subplots()
tips.boxplot(column='tip_pct',by=['smoker','time'],ax=axes)
column参数表示要绘制成箱形图的数据,可以是一列或多列
by参数表示分组依据
axes.set_ylabel('values of tip_pct')
fig.savefig('p3.png') # 将绘制的图形保存为p3.png
上述代码绘制的图形为:
DataFrame.boxplot()的用法具体参考:
方法2具体示例:
seaborn.cataplot()示例:
sns.catplot(x='tip_pct',y='day',hue='smoker',kind='box',
data=tips[tips.tip_pct < 0.5])
hue表示分组的依据
fig.savefig('p4.png') # 将绘制的图形保存为p4.png
上述代码绘制的图形为:
seaborn.catplot()的用法具体参考:
http://seaborn.pydata.org/generated/seaborn.catplot.html?highlight=seaborn%20catplot#seaborn.catplot
seaborn.boxplot()示例:
fig,axes = plt.subplots()
sns.boxplot(x='day',y='tip_pct',hue='smoker',
data=tips[tips.tip_pct < 0.5],orient='v',ax=axes)
orient参数表示箱形图的方向
axes.set_title('Boxplots grouped by smoker')
fig.savefig('p5.png') # 将绘制的图形保存为p5.png
上述代码绘制的图形为:
seaborn.boxplot()的用法具体参考:
http://seaborn.pydata.org/generated/seaborn.boxplot.html#seaborn.boxplot
方法3具体示例:
axes.boxplot()示例:
fig,axes = plt.subplots()
axes.boxplot(x=tips.tip_pct,sym='rd',positions=[2])
sym参数表示异常值的标记方式
positions表示箱形图的位置标签
axes.set_xlabel('tip_pct')
fig.savefig('p6.png') # 将绘制的图形保存为p6.png
上述代码绘制的图形为:
axes.boxplot()的用法具体参考:
其他参考资料:
《Python for Data Analysis》第二版
《商务与经济统计》第十三版
pandas、matplotlib、seaborn官方文档
作者:Backcanhave7
来源:CSDN
原文:https://blog.csdn.net/qq_41080850/article/details/83829045
网友评论