首先我們要知道,lmplot是用來繪制回歸圖的。
讓我們來看看他的API:
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, seed=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=True, x_jitter=None, y_jitter=None, scatter_kws=None, line_kws=None, size=None)
可以看出,參數是相當的多啊。
x, y strings, optional:是data數據中,行的名字。
data DataFrame:那就是你的數據了。
我們用實例慢慢驗證,本次試用的數據集是Seaborn內置的tips小費數據集:
第一步,導入相應的包,並輸出數據
import seaborn as sns tips = sns.load_dataset("tips") print(tips.head())
輸出結果:
total_bill tip sex smoker day time size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
我們用一下lmplot,看看繪制出來的是什么樣的圖:
df = sns.lmplot(x='total_bill',y='tip',data=tips)
繪制結果:
我們可以看到,lmplot對所選擇的數據集做出了一條最佳的擬合直線。
hue, col, row strings:其實就是用於分類,我們可以看到,他把smoker所在列中,是否抽煙做了分類。
df = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips)
order int, optional:
控制進行回歸的冪次(一次以上即是多項式回歸)
df = sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,order=3)
col strings:
根據所指定屬性在列上分類
row strings:
根據所指定屬性在行上分類
df = sns.lmplot(x="total_bill", y="tip",data=tips,col="day")
df = sns.lmplot(x="total_bill", y="tip",data=tips,row="sex")
df = sns.lmplot(x="total_bill", y="tip",data=tips,row="sex",col="day")
col_wrap int, optional 指定每行的列數,最多等於col參數所對應的不同類別的數量
df = sns.lmplot(x="total_bill", y="tip",data=tips,col="day",col_wrap=3)
先暫時學習到這兒,之后慢慢補充。