二.分類圖
1. 分類散點圖
(1)散點圖striplot(kind='strip')
方法1:
seaborn.stripplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, jitter=True, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)
方法2:catplot的kind默認=striplot
sns.catplot(x="sepal_length", y="species", data=iris)
(2)帶分布的散點圖swarmplot(kind='swarm')
方法1:
seaborn.swarmplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, dodge=False, orient=None, color=None, palette=None, size=5, edgecolor='gray', linewidth=0, ax=None, **kwargs)
方法2:
sns.catplot(x="sepal_length", y="species", kind="swarm", data=iris)
2. 分類分布圖
(1)箱線圖boxplot(kind='box')
方法1:
seaborn.boxplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, fliersize=5, linewidth=None, whis=1.5, notch=False, ax=None, **kwargs)
方法2:
sns.catplot(x="sepal_length", y="species", data=iris)
(2)小提琴圖violinplot(kind='violin')
方法1:
seaborn.violinplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, bw='scott', cut=2, scale='area', scale_hue=True, gridsize=100, width=0.8, inner='box', split=False, dodge=True, orient=None, linewidth=None, color=None, palette=None, saturation=0.75, ax=None, **kwargs)
方法2:
sns.catplot(x="sepal_length", y="species", kind="violin", data=iris)
(3)boxenplot(kind='boxen')
方法1:
seaborn.boxenplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, width=0.8, dodge=True, k_depth='proportion', linewidth=None, scale='exponential', outlier_prop=None, ax=None, **kwargs)
方法2:
sns.catplot(x="species", y="sepal_length", kind="boxen", data=iris)
3. 分類估計圖
(1)pointplot(kind='point')
方法1:
seaborn.pointplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, markers='o', linestyles='-', dodge=False, join=True, scale=1, orient=None, color=None, palette=None, errwidth=None, capsize=None, ax=None, **kwargs)
方法2:
sns.catplot(x="sepal_length", y="species", kind="point", data=iris)
(2)直方圖barplot(kind='bar')
方法1:
seaborn.barplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, estimator=<function mean>, ci=95, n_boot=1000, units=None, orient=None, color=None, palette=None, saturation=0.75, errcolor='.26', errwidth=None, capsize=None, dodge=True, ax=None, **kwargs)
方法2:
sns.catplot(x="sepal_length", y="species", kind="bar", data=iris)
(3)計數的直方圖countplot(kind='count')
方法1:
seaborn.countplot(x=None, y=None, hue=None, data=None, order=None, hue_order=None, orient=None, color=None, palette=None, saturation=0.75, dodge=True, ax=None, **kwargs)
方法2:
sns.catplot(x="species", kind="count", data=iris)
三.分布圖
包括單變量核密度曲線,直方圖,雙變量多變量的聯合直方圖,和密度圖
1.單分布
(1)distpot
方法:
seaborn.distplot(a, bins=None, hist=True, kde=True, rug=False, 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)
- 設置
kde=False
則可以只繪制直方圖,或者hist=False
只繪制核密度估計圖
舉例:
sns.distplot(iris["sepal_length"])
(2)kdeplot
方法:
seaborn.kdeplot(data, data2=None, shade=False, vertical=False, kernel='gau', bw='scott', gridsize=100, cut=3, clip=None, legend=True, cumulative=False, shade_lowest=True, cbar=False, cbar_ax=None, cbar_kws=None, ax=None, **kwargs)
kdeplot
可以專門用於繪制核密度估計圖,其效果和distplot(hist=False)
一致,但kdeplot
擁有更多的自定義設置
舉例:
sns.kdeplot(iris["sepal_length"])
2.雙分布
(1)jointplot二元變量分布圖
方法:
seaborn.jointplot(x, y, data=None, kind='scatter', stat_func=None, color=None, height=6, ratio=5, space=0.2, dropna=True, xlim=None, ylim=None, joint_kws=None, marginal_kws=None, annot_kws=None, **kwargs)
jointplot
並不是一個 Figure-level 接口,但其支持kind=
參數指定繪制出不同樣式的分布圖。例如,繪制出核密度估計對比圖 kind = 'kde'。- kind='hex'繪制六邊形計數圖
- kind='reg'繪制回歸擬合圖
舉例:
例如,我們探尋 sepal_length
和 sepal_width
二元特征變量之間的關系。
sns.jointplot(x="sepal_length", y="sepal_width", data=iris)
(2)pairpot
支持將數據集中的特征變量兩兩對比繪圖,默認情況下,對角線上是單變量分布圖,而其他是二元變量分布圖
方法:
seaborn.pairplot(data, hue=None, hue_order=None, palette=None, vars=None, x_vars=None, y_vars=None, kind='scatter', diag_kind='auto', markers=None, height=2.5, aspect=1, dropna=True, plot_kws=None, diag_kws=None, grid_kws=None, size=None)
舉例:
sns.pairplot(iris, hue="species")
(3)rugplot
方法:
seaborn.rugplot(a, height=0.05, axis='x', ax=None, **kwargs)
四.回歸圖
回歸圖只要探討兩連續數值變量的變化趨勢情況,繪制x-y的散點圖和回歸曲線。
1.lmplot
方法:
seaborn.lmplot(x, y, data, hue=None, col=None, row=None, palette=None, col_wrap=None, height=5, aspect=1, markers='o', sharex=True, sharey=True, hue_order=None, col_order=None, row_order=None, legend=True, legend_out=True, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)
- seaborn.lmplot
lmplot
同樣是用於繪制回歸圖,但lmplot
支持引入第三維度進行對比,例如我們設置hue="species"
。
舉例:
sns.lmplot(x="sepal_length", y="sepal_width", hue="species", data=iris)
2.regplot
方法:用線性回歸模型對數據做擬合
seaborn.regplot(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)
- seaborn.regplot
regplot
繪制回歸圖時,只需要指定自變量和因變量即可,regplot
會自動完成線性回歸擬合。
舉例:
sns.regplot(x="sepal_length", y="sepal_width", data=iris)
3.residplot
方法:展示線性回歸模型擬合后各點對應的殘值
seaborn.residplot(x, y, data=None, lowess=False, x_partial=None, y_partial=None, order=1, robust=False, dropna=True, label=None, color=None, scatter_kws=None, line_kws=None, ax=None)
舉例:可以對以年為單位的地震記錄作線性回歸擬合。以下兩張圖分別對應一階線性回歸擬合、擬合后殘值分布情況圖。
plt.figure(figsize=(12,6)) plt.subplot(121) sns.regplot(x="Year", y="ID", data=temp,order=1) # default by 1plt.ylabel(' ') plt.title('Regression fit of earthquake records by year,order = 1') plt.subplot(122) sns.residplot(x="Year", y="ID", data=temp) plt.ylabel(' ') plt.title('Residual plot when using a simplt regression model,order=1') plt.show()
五.矩陣圖
1.熱力圖heatmap
方法:用顏色矩陣去顯示數據在兩個維度下的度量值
seaborn.heatmap(data, vmin=None, vmax=None, cmap=None, center=None, robust=False, annot=None, fmt='.2g', annot_kws=None, linewidths=0, linecolor='white', cbar=True, cbar_kws=None, cbar_ax=None, square=False, xticklabels='auto', yticklabels='auto', mask=None, ax=None, **kwargs)
- 熱力圖在某些場景下非常實用,例如繪制出變量相關性系數熱力圖。
舉例:
import numpy as np sns.heatmap(np.random.rand(10, 10))
2.聚類圖clustermap
方法:
seaborn.clustermap(data, pivot_kws=None, method='average', metric='euclidean', z_score=None, standard_scale=None, figsize=None, cbar_kws=None, row_cluster=True, col_cluster=True, row_linkage=None, col_linkage=None, row_colors=None, col_colors=None, mask=None, **kwargs)
- 除此之外,
clustermap
支持繪制層次聚類結構圖。如下所示,我們先去掉原數據集中最后一個目標列,傳入特征數據即可。當然,你需要對層次聚類有所了解,否則很難看明白圖像多表述的含義。
舉例:
iris.pop("species") sns.clustermap(iris)
六.時間序列圖
1.時間序列圖tsplot
用時間維度序列去展現數據的不確定性
2.網格時序圖plot_ts_d , plot_ts_m
七.多繪圖網格
1.小平面網格
1.1 FaceGrid
1.2 FacetGrid.map
1.3 FacetGrid.map_dataframe
2.配對網格
2.1 PairGrid
2.2 PairGrid.map
2.3 PairGrid.map_diag
2.4 PairGrid.map_offdiag
2.5 PairGrid.map_lower
2.6 PairGrid.map_upper
3.聯合網格
3.1 JointGrid
3.2 JointGrid.plot
3.3 JointGrid.plot_joint
3.4 JointGrid.plot_marginals
參考文獻:
【2】5種方法教你用Python玩轉histogram直方圖
【4】python可視化進階---seaborn1.7 分類數據可視化 - 統計圖 barplot() / countplot() / pointplot()
【5】seaborn官網