分类数据可视化 - 分布图(箱型图,小提琴图,LV图)
boxplot() / violinplot() / lvplot()
1. boxplot()
#绘制箱型图
import seaborn as sns #导入数据 tips = sns.load_dataset('tips') sns.boxplot(x = 'day', y = 'total_bill', data = tips, linewidth = 2, #线宽 width = 0.8, #箱之间的间隔比例 fliersize = 3, #异常点大小 palette = 'hls', #设置调色板 whis = 1.5, #设置IQR notch = True, #设置是否以中值做凹槽 order = {'Thur','Fri','Sat','Sun'}, #筛选类别 ) #可以添加散点图 sns.swarmplot(x = 'day', y = 'total_bill', data = tips, color = 'k', size = 3, alpha = 0.8)
#通过参数再分类
#通过参数再分类 sns.boxplot(x = 'day', y = 'total_bill', data = tips, hue = 'smoker', palette = 'Reds')
2. violinplot()
# 小提琴图
示例1:
sns.violinplot(x = 'day', y = 'total_bill', data = tips, linewidth = 2, #线宽 width = 0.8, #箱之间的间隔比例 palette = 'hls', #设置调色板 order = {'Thur', 'Fri', 'Sat','Sun'}, #筛选类别 scale = 'count', #测度小提琴图的宽度: area-面积相同,count-按照样本数量决定宽度,width-宽度一样 gridsize = 50, #设置小提琴图的平滑度,越高越平滑 inner = 'box', #设置内部显示类型 --> 'box','quartile','point','stick',None #bw = 0.8 #控制拟合程度,一般可以不设置 )
示例2:通过hue再分类
#通过hue参数再分类 sns.violinplot(x = 'day', y = 'total_bill', data = tips, hue = 'smoker', palette = 'muted', split = True, #设置是否拆分小提琴图 inner = 'quartile')
示例3:结合散点图
#插入散点图 sns.violinplot(x = 'day', y = 'total_bill', data = tips, palette = 'hls', inner = None) sns.swarmplot(x = 'day', y = 'total_bill', data = tips,color = 'w', alpha = .5)
3.lvplot()
#LV图表
#绘制LV图 sns.lvplot(x = 'day', y = 'total_bill', data = tips, palette = 'mako', #hue = 'smoker', width = 0.8, #箱之间间隔比例 linewidth = 12, scale = 'area', #设置框的大小 --> 'linear'、'exonential'、'area' k_depth = 'proportion' #设置框的数量 --> 'proportion','tukey','trustworthy' ) #可以添加散点图 sns.swarmplot(x = 'day', y = 'total_bill', data = tips, color ='k', size =3, alpha = 0.8)