Seaborn繪圖
- 全部代碼:https://github.com/lawlite19/Blog-Back-Up/blob/master/code/seaborn_study.py
- 個人部落格位址:http://lawlite.me/2017/06/14/Seaborn%E7%BB%98%E5%9B%BE/
一、介紹與安裝
1、介紹
- 官網:http://seaborn.pydata.org/index.html
- Github: https://github.com/mwaskom/seaborn
-
其實是在matplotlib的基礎上進行了更進階的Seaborn
封裝,進而使得作圖更加容易API
- 在大多數情況下使用
就能做出很具有吸引力的圖,而使用seaborn
就能制作具有更多特色的圖。應該把matplotlib
視為Seaborn
的補充matplotlib
2、安裝
- 直接
即可pip3 install seaborn
二、分布圖
1、distplot
- 導入包
#-*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
#%matplotlib inline # 為了在jupyter notebook裡作圖,需要用到這個指令
- 加載
中的資料集:seaborn
tips = sns.load_dataset('tips')
- 分布圖
-
是高斯分布密度圖,繪圖在0-1之間kde
-
是否畫直方圖hist
-
在X軸上畫一些分布線rug
-
可以制定某個分布進行拟合fit
-
legend時的值label
-
制定橫軸的說明axlabel
-
sns.distplot(tips['total_bill'], bins=None, hist=True, kde=False, rug=True, fit=None,
hist_kws=None, kde_kws=None, rug_kws=None,
fit_kws=None, color=None, vertical=False,
norm_hist=False, axlabel=None, label=None, ax=None)
sns.plt.show()
- 拟合分布
- 這裡使用了gamma分布拟合
from scipy import stats
sns.distplot(tips.total_bill, fit=stats.gamma, kde=False)
sns.plt.show()
2、kdeplot
- 高斯機率密度圖
-
可以是二維的分布data2
-
是否填充shade
-
核函數,還有很多核函數,比如cos, biw等kernel
-
累積的作圖,最後的值應該是接近1cumulative
-
多少個點估計gridsize
-
ax = sns.kdeplot(tips['total_bill'], data2=tips.tip, shade=False, vertical=False,
kernel="gau", bw="scott",
gridsize=, cut=, clip=None,
legend=True, cumulative=False,
shade_lowest=True, ax=None)
sns.plt.show()
二、pairplot
1、兩兩作圖
-
為例iris
-
: DataFrame格式的資料data
-
: label類别對應的column namehue
-
: 指定feature的列名vars
-
: 作圖的方式,可以是reg或scatterkind
-
: 對角線作圖的方式,可以是hist或kdediag_kind
-
iris = sns.load_dataset('iris')
g = sns.pairplot(iris, hue='species', hue_order=None, palette=None,
vars=list(iris.columns[:-]),
x_vars=None, y_vars=None,
kind="reg", diag_kind="hist",
markers=['o','s','D'], size=, aspect=,
dropna=True, plot_kws=None,
diag_kws=None, grid_kws=None)
sns.plt.show()
三、stripplot和swarmplot
1、stripplot
- tips為例,檢視每天的資料資訊
-
: X軸資料x
-
: Y軸資料y
-
: 區分不同種類資料的column namehue
-
: DataFrame類型資料data
-
: 将資料分開點,防止重疊jitter
-
tips = sns.load_dataset('tips')
ax = sns.stripplot(x='day', y='total_bill', hue=None, data=tips, order=None,
hue_order=None, jitter=True,
split=False, orient=None,
color=None, palette=None, size=,
edgecolor="gray", linewidth=,
ax=None)
- 檢視關于性别消費的資訊
ax = sns.stripplot(x='sex', y='total_bill', hue='day', data=tips, order=None,
hue_order=None, jitter=True,
split=False, orient=None,
color=None, palette=None, size=,
edgecolor="gray", linewidth=,
ax=None)
2、swarmplot
- 與stripplot類似,隻是資料點不會重疊 (适合小資料量)
tips = sns.load_dataset('tips')
ax = sns.swarmplot(x='sex', y='total_bill', hue='day', data=tips)
sns.plt.show()
四、boxplot
1、boxplot示意圖
- 函數
-
:指定X軸,Y軸的columns name值x, y
-
: 指定要區分的類别hue
-
tips = sns.load_dataset('tips')
ax = sns.boxplot(x='day', y='total_bill', hue=None, data=tips, order=None,
hue_order=None, orient=None,
color=None, palette=None,
saturation=, width=,
fliersize=, linewidth=None,
whis=, notch=False, ax=None)
sns.plt.show()
- 可以和上面的stripplot一起用
tips = sns.load_dataset('tips')
ax = sns.boxplot(x='day', y='total_bill', hue=None, data=tips, order=None,
hue_order=None, orient=None,
color=None, palette=None,
saturation=, width=,
fliersize=, linewidth=None,
whis=, notch=False, ax=None)
sns.stripplot(x='day', y='total_bill', hue=None, data=tips, order=None,
hue_order=None, jitter=True, split=False,
orient=None, color=None, palette=None,
size=, edgecolor="gray", linewidth=,
ax=None)
sns.plt.show()
五、jointplot
1、jointplot
- 聯合作圖
-
: 有scatter” | “reg” | “resid” | “kde” | “hexkind
-
tips = sns.load_dataset('tips')
from scipy import stats
g = sns.jointplot(x='total_bill', y='tip',
data=tips, kind="reg",
stat_func=stats.pearsonr,
color=None, size=, ratio=,
space=, dropna=True, xlim=None,
ylim=None, joint_kws=None,
marginal_kws=None, annot_kws=None)
sns.plt.show()
- 可以在基礎上再作圖
- plot_joint就是在聯合分布上作圖
- plot_marginals就是在邊緣分布上再作圖
六、violinplot
1、小提琴圖,和boxplot很像
- 對稱的kde圖
- 中間的白點是中位數,黑色粗線對應分位數
-
: 指定圖裡面用什麼劃分,有inner
"box", "quartile", "point", "stick", None
-
為四分位數劃分quartile
-
很像rug,就是可以看出密度情況stick
-
-
: 縮放每個圖對應的area, 取值有scale
"area", "count", "width"
-
指定每個有相同的areaarea
-
會按數量縮放(數量少的就比較窄扁)count
-
tips = sns.load_dataset('tips')
ax = sns.violinplot(x='day', y='total_bill',
hue='smoker', data=tips, order=None,
hue_order=None, bw="scott",
cut=, scale="area",
scale_hue=True, gridsize=,
width=, inner="quartile",
split=False, orient=None,
linewidth=None, color=None,
palette='muted', saturation=,
ax=None)
sns.plt.show()
七、pointplot, bar
1、pointplot
- 點圖
-
:點的取值是,預設是estimator
np.mean
-
tips = sns.load_dataset('tips')
sns.pointplot(x='time', y='total_bill', hue='smoker', data=tips, order=None,
hue_order=None, estimator=np.mean, ci=,
n_boot=, units=None, markers="o",
linestyles="-", dodge=False, join=True,
scale=, orient=None, color=None,
palette=None, ax=None, errwidth=None,
capsize=None)
sns.plt.show()
2、barplot
- 條形圖
- y軸是
,和點圖其實差不多mean value
- y軸是
tips = sns.load_dataset('tips')
sns.barplot(x='day', y='total_bill', hue='sex', data=tips, order=None,
hue_order=None, estimator=np.mean, ci=,
n_boot=, units=None, orient=None,
color=None, palette=None, saturation=,
errcolor=".26", errwidth=None, capsize=None,
ax=None)
sns.plt.show()
3、countplot
tips = sns.load_dataset('tips')
sns.countplot(x='day', hue='sex', data=tips)
sns.plt.show()
八、factorplot
1、可以通過這個函數繪制以上幾種圖
- 指定
即可,有kind
,point
,bar
,count
,box
,violin
strip
-
和row
指定繪制的行數和列數,給出一個種類類型的列名即可col
titanic = sns.load_dataset('titanic')
sns.factorplot(x='age', y='embark_town',
hue='sex', data=titanic,
row='class', col='sex',
col_wrap=None, estimator=np.mean, ci=,
n_boot=, units=None, order=None,
hue_order=None, row_order=None,
col_order=None, kind="box", size=,
aspect=, orient=None, color=None,
palette=None, legend=True,
legend_out=True, sharex=True,
sharey=True, margin_titles=False,
facet_kws=None)
sns.plt.show()
九、heatmap
1、heatmap
flight = sns.load_dataset('flights')
flights = flight.pivot('month','year','passengers')
sns.heatmap(flights, annot=True, fmt='d')
sns.plt.show()
十、時序繪圖
1、tsplot
- condition: 和
差不多,指定類别hue
- estimator: 預設為
np.mean
gammas = sns.load_dataset('gammas')
sns.tsplot(data=gammas, time='timepoint', unit='subject',
condition='ROI', value='BOLD signal',
err_style="ci_band", ci=, interpolate=True,
color=None, estimator=np.mean, n_boot=,
err_palette=None, err_kws=None, legend=True,
ax=None)
sns.plt.show()
Reference
- Youtube: https://www.youtube.com/playlist?list=PLgJhDSE2ZLxYlhQx0UfVlnF1F7OWF-9rp
- Github: https://github.com/knathanieltucker/seaborn-weird-parts