1. 程式人生 > >python視覺化進階---seaborn1.5 分類資料視覺化

python視覺化進階---seaborn1.5 分類資料視覺化

分類資料視覺化 - 分類散點圖

stripplot() / swarmplot()

載入模組,設定風格、尺度

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

#設定風格、尺度
sns.set_style('whitegrid')
sns.set_context('paper')

#不發出警告
import warnings
warnings.filterwarnings('ignore')

1.stripplot()

#按照不同類別對樣本資料進行分佈散點圖繪製

示例1:hue

tips = sns.load_dataset('tips')
print(tips.head())

sns.stripplot(x = 'day',       #x ---> 設定分組統計欄位
              y = 'total_bill',#y ---> 資料分佈統計欄位
              #這裡xy資料對調,會使得散點圖橫向分佈
              data = tips,    #data ---> 對應資料
              jitter = True, #jitter ---> 當資料重合較多時,用該引數做一些調整,也可以設定間距如,jitter = 0.1    
              size = 5, edgecolor = 'w', linewidth = 1, marker = 'o'
              )

#通過hue引數再分類
sns.stripplot(x = 'sex', y = 'total_bill', hue = 'day', data=tips, jitter = True)

通過stripplot() 按照x軸裡的類別進行分類

並通過hue = ‘day’可以再對散點圖中的數值進行分類

示例2:設定調色盤palette

#設定調色盤
sns.stripplot(x = 'sex' , y = 'total_bill', hue = 'day',
              data = tips, jitter = True,
              palette = 'Set2',#設定調色盤
              dodge = True,
              )

示例3:用order引數進行篩選分類類別

#篩選分類類別
#檢視day欄位的唯一值
print(tips['day'].value_counts())
#order  ---> 篩選類別
sns.stripplot(x = 'day', y = 'total_bill', data = tips, jitter = True,
              order = ['Sat','Sun'])

2、swarmplot() 分簇散點圖

#用法和stripplot類似
sns.swarmplot(y = 'total_bill', x = 'day', data= tips,
              size = 5, edgecolor = 'w', linewidth = 1, marker = 'o',
              palette = 'Reds')