> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/python/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/python/chapter-2courses/21python-for-data-science-and-machine-learning-bootcamp/217python-for-data-visualization-seaborn/2176regression-plots.md).

# 2.1.7.6.Regression Plots

## 0. Regression Plots的繪圖種類

* sns.lmplot

## 1. 使用library

```
import seaborn as sns
```

* 將圖表直接嵌入到Notebook之中

```
%matplotlib inline
```

* 讀入資料

```
tips = sns.load_dataset('tips')
tips.head()
```

![](/files/-M4M0T2-2dhtKEB3w0JJ)

## 2.lmplot

* lmplot是一種集合基礎繪圖與基於數據建立回歸模型的繪圖方法, 旨在建立一個方便擬合數據集回歸模型的繪圖方法
* 可以利用參數來調整擬合的模型類型: order、logistic、lowess、robust、logx
* 基於建立回歸模型的繪圖方法

```
sns.lmplot(x='total_bill', y='tip', data=tips)
```

![](/files/-M4M0T2E5pIu1LlxH_i0)

* 分類

```
sns.lmplot(x='total_bill', y='tip', data=tips, hue='sex', markers=['o', 'v'], scatter_kws={'s':100})
```

![](/files/-M4M0T2GYRi77m4TidqF)

* 繪製多張圖
  * col參數

    ```
    sns.lmplot(x='total_bill', y='tip',  data=tips, col='sex', row='time')
    ```

    ![](/files/-M4M0T2IxESencttx1Zd)
* 應用例 1. 建立int.rate, fico的線性回歸模型

  ```
    plt.figure(figsize=(11,7))
    sns.lmplot(y='int.rate',x='fico',data=df)
  ```

  ![](/files/-M4M0T2KAybdBLIXf06U) 2. 加入credit.policy的比較

  ```
    plt.figure(figsize=(11,7))    
    sns.lmplot(y='int.rate',x='fico',data=df,hue='credit.policy',palette='Set1')
  ```

  ![](/files/-M4M0T2MwCmWc48eqQkq) 3. 加入not.fully.paid的比較

  ```
    plt.figure(figsize=(11,7))
    sns.lmplot(y='int.rate',x='fico',data=df,hue='credit.policy',col='not.fully.paid',palette='Set1)
  ```

  ![](/files/-M4M0T2OrQRY7-TrqYL3)
