天天看點

python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()

分類資料可視化 - 分布圖(箱型圖,小提琴圖,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)      
python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()

#通過參數再分類

#通過參數再分類
sns.boxplot(x = 'day', y = 'total_bill', data = tips,
            hue = 'smoker', palette = 'Reds')      
python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()

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      #控制拟合程度,一般可以不設定
               )      
python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()

示例2:通過hue再分類

#通過hue參數再分類
sns.violinplot(x = 'day', y = 'total_bill', data = tips,
               hue = 'smoker', palette = 'muted',
               split = True, #設定是否拆分小提琴圖
               inner = 'quartile')      
python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()

示例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)      
python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()

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)
       
python可視化進階---seaborn1.6 分類資料可視化 - 分布圖 boxplot() / violinplot() / lvplot()