單變量
直方圖看數據分佈
plt.figure(figsize=(12,5))
#默認圖形
plt.subplot(141)
sns.distplot(x)
#不畫直方圖
plt.subplot(142)
sns.distplot(x,hist=False)
#不畫線圖
plt.subplot(143)
sns.distplot(x,kde=False,bins=20)
#設置fit
plt.subplot(144)
sns.distplot(x, kde=False, fit=stats.gamma)
多變量
使用seaborn自帶數據集進行分析
sns.jointplot(x="sepal_length", y="sepal_width", data=iris);
當數據較多時,需要看數據聚集程度,可以用下面的方法
with sns.axes_style("white"):
sns.jointplot(x="sepal_length", y="sepal_width", data=iris, kind="hex", color="k")
查看所有變量之間的相關性
sns.pairplot(iris)
分類變量
數圖
#基本圖
plt.subplot(221)
sns.stripplot(x="day", y="total_bill", data=tips)#如果數據堆疊可以使用 jitter=True
#樹圖
plt.subplot(222)
sns.swarmplot(x="day", y="total_bill", data=tips)
#查看分類
plt.subplot(223)
sns.swarmplot(x="day", y="total_bill", hue="sex",data=tips)
#橫置
plt.subplot(224)
sns.swarmplot(x="total_bill", y="day", hue="sex", data=tips);
盒圖/小提琴圖
#盒圖
plt.subplot(121)
sns.boxplot(x="day", y="total_bill", hue="sex", data=tips)
#小提琴圖
plt.subplot(122)
sns.violinplot(x="day", y="total_bill", hue="sex", data=tips)
#在一個小提琴圖裏繪製變量對比
plt.subplot(121)
sns.violinplot(x="day", y="total_bill", hue="sex", data=tips, split=True)
#組合
plt.subplot(122)
sns.violinplot(x="day", y="total_bill", data=tips, inner=None)
sns.swarmplot(x="day", y="total_bill", data=tips, color="w", alpha=.5)
factorplot
factorplot是一封裝了多種圖形的函數
seaborn.factorplot(x=None, y=None, hue=None, data=None, row=None, col=None, col_wrap=None, estimator=, ci=95, n_boot=1000, units=None, order=None, hue_order=None, row_order=None, col_order=None, kind=‘point’, size=4, aspect=1, orient=None, color=None, palette=None, legend=True, legend_out=True, sharex=True, sharey=True, margin_titles=False, facet_kws=None, **kwargs)
- x,y,hue 數據集變量 變量名
- date 數據集 數據集名
- row,col 更多分類變量進行平鋪顯示 變量名
- col_wrap 每行的最高平鋪數 整數
- estimator 在每個分類中進行矢量到標量的映射 矢量
- ci 置信區間 浮點數或None
- n_boot 計算置信區間時使用的引導迭代次數 整數
- units 採樣單元的標識符,用於執行多級引導和重複測量設計 數據變量或向量數據
- order, hue_order 對應排序列表 字符串列表
- row_order, col_order 對應排序列表 字符串列表
- kind : 可選:point 默認, bar 柱形圖, count 頻次, box 箱體, violin 提琴, strip 散點,swarm 分散點
size 每個面的高度(英寸) 標量
aspect 縱橫比 標量
orient 方向 “v”/“h”
color 顏色 matplotlib顏色
palette 調色板 seaborn顏色色板或字典
legend hue的信息面板 True/False
legend_out 是否擴展圖形,並將信息框繪製在中心右邊 True/False
share{x,y} 共享軸線 True/False
example
#盒圖
sns.factorplot(x="time", y="total_bill", hue="smoker",
col="day", data=tips, kind="box", size=4, aspect=.5)
熱力圖
#默認圖形
plt.subplot(231)
sns.heatmap(heat_data)
#設置閾值,最大最小
plt.subplot(232)
sns.heatmap(heat_data, vmin=0.2, vmax=0.5)
#設置中心值
plt.subplot(233)
sns.heatmap(heat_data, center=0)
#顯示數字
plt.subplot(234)
sns.heatmap(heat_data, annot=True,fmt="f")#fmt="d"顯示整數
#設置間隔
plt.subplot(235)
sns.heatmap(heat_data, linewidths=.5)
#設置顏色
plt.subplot(236)
sns.heatmap(heat_data, cmap="YlGnBu")