分類資料可視化 - 統計圖
barplot() / countplot() / pointplot()
1. barplot()
#柱狀圖 - 置信區間估計
#置信區間:樣本均值 + 抽樣誤差
示例1:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context('paper')
#加載資料
titanic = sns.load_dataset('titanic')
print(titanic.head())
sns.barplot(x = 'sex', y = 'survived', hue = 'class', data = titanic,
palette = 'hls',
order = ['male', 'female'], #篩選類别
capsize = 0.05, #誤差線橫向延申寬度
saturation = 8, #顔色飽和度
errcolor = 'gray', errwidth = 2, #誤差線顔色、寬度
ci = 'sd' #置信區間誤差 --> 0-100内值、 'sd' 、None
)
#計算資料
print(titanic.groupby(['sex', 'class']).mean()['survived'])
print(titanic.groupby(['sex', 'class']).std()['survived'])
python可視化進階---seaborn1.7 分類資料可視化 - 統計圖 barplot() / countplot() / pointplot() 示例2:
tips = sns.load_dataset('tips')
sns.barplot(x = 'day', y = 'total_bill', hue = 'sex', data = tips,
palette = 'Blues', edgecolor = 'w')
tips.groupby(['day','sex']).mean()
python可視化進階---seaborn1.7 分類資料可視化 - 統計圖 barplot() / countplot() / pointplot() 示例3:
#加載資料
crashes = sns.load_dataset('car_crashes').sort_values('total', ascending = False)
#建立圖表
f, ax = plt.subplots(figsize = (6,15))
#設定第一個柱狀圖
sns.set_color_codes('pastel')
sns.barplot(x = 'total', y = 'abbrev', data = crashes,
label = 'Total', color = 'b', edgecolor = 'w')
#設定第二個柱狀圖
sns.set_color_codes('muted')
sns.barplot(x = 'alcohol', y = 'abbrev', data = crashes,
label = 'Alcohol-involved', color = 'b', edgecolor = 'w')
ax.legend(ncol = 2, loc = 'lower right')
sns.despine(left = True, bottom = True)
python可視化進階---seaborn1.7 分類資料可視化 - 統計圖 barplot() / countplot() / pointplot() 2、countplot()
#計數柱狀圖
#x/y --> 以x或者y軸繪圖(橫向,豎向)
#用法和barplot相似
sns.countplot(x = 'class', hue = 'who', data = titanic, palette = 'magma')
python可視化進階---seaborn1.7 分類資料可視化 - 統計圖 barplot() / countplot() / pointplot() sns.pointplot(x = 'time', y = 'total_bill', hue = 'smoker', data=tips,
palette = 'hls',
dodge = True, #設定點是否分開
join = True, #是否連線
markers = ['o','x'],linestyles = ['-','--'],#設定點樣式、線型
)
#計算資料
tips.groupby(['time','smoker']).mean()['total_bill']
3.pointplot()
#折線圖 - 置信區間估計
python可視化進階---seaborn1.7 分類資料可視化 - 統計圖 barplot() / countplot() / pointplot()