# Seaborn(sns)官方文档学习笔记（第四章 线性关系的可视化）

Seaborn(sns)官方文档学习笔记系列包括：

```%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt

import seaborn as sns
sns.set(color_codes=True)

np.random.seed(sum(map(ord, "regression")))

```

## 绘制线性回归模型的函数

```sns.regplot(x="total_bill", y="tip", data=tips);
```
```sns.lmplot(x="total_bill", y="tip", data=tips);
```

```tips.head()

total_bill  tip   sex   smoker  day   time  size  big_tip
0   16.99   1.01  Female  No  Sun   Dinner  2   False
1   10.34   1.66  Male  No  Sun   Dinner  3   True
2   21.01   3.50  Male  No  Sun   Dinner  3   True
3   23.68   3.31  Male  No  Sun   Dinner  2   False
4   24.59   3.61  Female  No  Sun   Dinner  4   False
sns.lmplot(data=tips,x="size",y="tip")
```

```sns.lmplot(x="size", y="tip", data=tips, x_jitter=.05);
```

```sns.lmplot(x="size", y="tip", data=tips, x_estimator=np.mean);
```

## 不同类型的模型拟合

```anscombe = sns.load_dataset("anscombe")
sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),
ci=None, scatter_kws={"s": 80});
```

```sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
ci=None, scatter_kws={"s": 80});
```

```sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
order=2, ci=None, scatter_kws={"s": 80});
```

```sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'III'"),
ci=None, scatter_kws={"s": 80});
```

```sns.lmplot(x="x", y="y", data=anscombe.query("dataset == 'III'"),
robust=True, ci=None, scatter_kws={"s": 80});
```

```tips["big_tip"] = (tips.tip / tips.total_bill) > .15
sns.lmplot(x="total_bill", y="big_tip", data=tips,
y_jitter=.03);
```

```sns.lmplot(x="total_bill", y="big_tip", data=tips,
logistic=True, y_jitter=.03);
```

```sns.lmplot(x="total_bill", y="tip", data=tips,
lowess=True);
```

residplot()是一个有用的工具，用于检查简单的回归模型是否拟合数据集。它拟合并移除一个简单的线性回归，然后绘制每个观察值的残差值。 理想情况下，这些值应随机散布在y = 0附近：

```sns.residplot(x="x", y="y", data=anscombe.query("dataset == 'I'"),
scatter_kws={"s": 80});
```

```sns.residplot(x="x", y="y", data=anscombe.query("dataset == 'II'"),
scatter_kws={"s": 80});
```

## 调节其他变量

```sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips);
```

```sns.lmplot(x="total_bill", y="tip", hue="smoker", data=tips,
markers=["o", "x"], palette="Set1");
```

```sns.lmplot(x="total_bill", y="tip", hue="smoker", col="time", data=tips);
```
```sns.lmplot(x="total_bill", y="tip", hue="smoker",
col="time", row="sex", data=tips);
```

## 控制绘制的大小和形状

```f, ax = plt.subplots(figsize=(5, 6))
sns.regplot(x="total_bill", y="tip", data=tips, ax=ax);
```

```sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
col_wrap=2, size=3);
```
```sns.lmplot(x="total_bill", y="tip", col="day", data=tips,
aspect=.5);
```

## 在其他背景下绘制回归

```sns.jointplot(x="total_bill", y="tip", data=tips, kind="reg");
```

```sns.pairplot(tips, x_vars=["total_bill", "size"], y_vars=["tip"],
size=5, aspect=.8, kind="reg");
```

```sns.pairplot(tips, x_vars=["total_bill", "size"], y_vars=["tip"],
hue="smoker", size=5, aspect=.8, kind="reg");
```