回歸圖只要探討兩連續數值變量的變化趨勢情況,繪制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()