# Visualizing linear relationships

Many datasets contain multiple quantitative variables, and the goal of an analysis is often to relate those variables to each other. We [previously discussed](distributions.html#distribution-tutorial) functions that can accomplish this by showing the joint distribution of two variables. It can be very helpful, though, to use statistical models to estimate a simple relationship between two noisy sets of observations. The functions discussed in this chapter will do so through the common framework of linear regression.

In the spirit of Tukey, the regression plots in seaborn are primarily intended to add a visual guide that helps to emphasize patterns in a dataset during exploratory data analyses. That is to say that seaborn is not itself a package for statistical analysis. To obtain quantitative measures related to the fit of regression models, you should use [statsmodels](https://www.statsmodels.org/). The goal of seaborn, however, is to make exploring a dataset through visualization quick and easy, as doing so is just as (if not more) important than exploring a dataset through tables of statistics.

```py
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

```

```py
sns.set(color_codes=True)

```

```py
tips = sns.load_dataset("tips")

```

## Functions to draw linear regression models

Two main functions in seaborn are used to visualize a linear relationship as determined through regression. These functions, [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") and [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") are closely related, and share much of their core functionality. It is important to understand the ways they differ, however, so that you can quickly choose the correct tool for particular job.

In the simplest invocation, both functions draw a scatterplot of two variables, `x` and `y`, and then fit the regression model `y ~ x` and plot the resulting regression line and a 95% confidence interval for that regression:

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

```

![http://seaborn.pydata.org/_images/regression_7_0.png](img/06f05fb132ef0dc7f9cca60a16b84f1a.jpg)

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

```

![http://seaborn.pydata.org/_images/regression_8_0.png](img/ff165b205839381bf1fa11c2df3d176d.jpg)

You should note that the resulting plots are identical, except that the figure shapes are different. We will explain why this is shortly. For now, the other main difference to know about is that [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") accepts the `x` and `y` variables in a variety of formats including simple numpy arrays, pandas `Series` objects, or as references to variables in a pandas `DataFrame` object passed to `data`. In contrast, [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") has `data` as a required parameter and the `x` and `y` variables must be specified as strings. This data format is called “long-form” or [“tidy”](https://vita.had.co.nz/papers/tidy-data.pdf) data. Other than this input flexibility, [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") possesses a subset of [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot")’s features, so we will demonstrate them using the latter.

It’s possible to fit a linear regression when one of the variables takes discrete values, however, the simple scatterplot produced by this kind of dataset is often not optimal:

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

```

![http://seaborn.pydata.org/_images/regression_10_0.png](img/18916d57b3e69c5de1769a7b26e0f713.jpg)

One option is to add some random noise (“jitter”) to the discrete values to make the distribution of those values more clear. Note that jitter is applied only to the scatterplot data and does not influence the regression line fit itself:

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

```

![http://seaborn.pydata.org/_images/regression_12_0.png](img/2e23cc65e61b2659c68ac3746b8ebad1.jpg)

A second option is to collapse over the observations in each discrete bin to plot an estimate of central tendency along with a confidence interval:

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

```

![http://seaborn.pydata.org/_images/regression_14_0.png](img/4dda0f476dee5bc15c815a9403521409.jpg)

## Fitting different kinds of models

The simple linear regression model used above is very simple to fit, however, it is not appropriate for some kinds of datasets. The [Anscombe’s quartet](https://en.wikipedia.org/wiki/Anscombe%27s_quartet) dataset shows a few examples where simple linear regression provides an identical estimate of a relationship where simple visual inspection clearly shows differences. For example, in the first case, the linear regression is a good model:

```py
anscombe = sns.load_dataset("anscombe")

```

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

```

![http://seaborn.pydata.org/_images/regression_17_0.png](img/7facbea48d8c72d5400863700e07aa18.jpg)

The linear relationship in the second dataset is the same, but the plot clearly shows that this is not a good model:

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

```

![http://seaborn.pydata.org/_images/regression_19_0.png](img/af2ff74dd015731938ea83ab029d73e7.jpg)

In the presence of these kind of higher-order relationships, [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") and [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") can fit a polynomial regression model to explore simple kinds of nonlinear trends in the dataset:

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

```

![http://seaborn.pydata.org/_images/regression_21_0.png](img/b2cde7454aa9f4cc76186d93a3cab208.jpg)

A different problem is posed by “outlier” observations that deviate for some reason other than the main relationship under study:

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

```

![http://seaborn.pydata.org/_images/regression_23_0.png](img/d9085f52d35beabc56a47c8326aee8cf.jpg)

In the presence of outliers, it can be useful to fit a robust regression, which uses a different loss function to downweight relatively large residuals:

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

```

![http://seaborn.pydata.org/_images/regression_25_0.png](img/696b41cce47f6657e61372ff64381129.jpg)

When the `y` variable is binary, simple linear regression also “works” but provides implausible predictions:

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

```

![http://seaborn.pydata.org/_images/regression_27_0.png](img/2b4711757bd351625d2244392491e1cf.jpg)

The solution in this case is to fit a logistic regression, such that the regression line shows the estimated probability of `y = 1` for a given value of `x`:

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

```

![http://seaborn.pydata.org/_images/regression_29_0.png](img/afea667bcdb80a09c24a25f5d5d0bd82.jpg)

Note that the logistic regression estimate is considerably more computationally intensive (this is true of robust regression as well) than simple regression, and as the confidence interval around the regression line is computed using a bootstrap procedure, you may wish to turn this off for faster iteration (using `ci=None`).

An altogether different approach is to fit a nonparametric regression using a [lowess smoother](https://en.wikipedia.org/wiki/Local_regression). This approach has the fewest assumptions, although it is computationally intensive and so currently confidence intervals are not computed at all:

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

```

![http://seaborn.pydata.org/_images/regression_31_0.png](img/640b99998425ffa57b452eca0b5e129b.jpg)

The [`residplot()`](../generated/seaborn.residplot.html#seaborn.residplot "seaborn.residplot") function can be a useful tool for checking whether the simple regression model is appropriate for a dataset. It fits and removes a simple linear regression and then plots the residual values for each observation. Ideally, these values should be randomly scattered around `y = 0`:

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

```

![http://seaborn.pydata.org/_images/regression_33_0.png](img/86f9e6a8b938de1a3aeb9b237b524f0b.jpg)

If there is structure in the residuals, it suggests that simple linear regression is not appropriate:

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

```

![http://seaborn.pydata.org/_images/regression_35_0.png](img/61c737744b3c54b93ad9bc691e4461bc.jpg)

## Conditioning on other variables

The plots above show many ways to explore the relationship between a pair of variables. Often, however, a more interesting question is “how does the relationship between these two variables change as a function of a third variable?” This is where the difference between [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") and [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") appears. While [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") always shows a single relationship, [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") combines [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") with [`FacetGrid`](../generated/seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") to provide an easy interface to show a linear regression on “faceted” plots that allow you to explore interactions with up to three additional categorical variables.

The best way to separate out a relationship is to plot both levels on the same axes and to use color to distinguish them:

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

```

![http://seaborn.pydata.org/_images/regression_37_0.png](img/c1be6d813a335d32887cfd051ef9167f.jpg)

In addition to color, it’s possible to use different scatterplot markers to make plots the reproduce to black and white better. You also have full control over the colors used:

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

```

![http://seaborn.pydata.org/_images/regression_39_0.png](img/b065f9278c97b242c9a517aa98c090fa.jpg)

To add another variable, you can draw multiple “facets” which each level of the variable appearing in the rows or columns of the grid:

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

```

![http://seaborn.pydata.org/_images/regression_41_0.png](img/02113b4a6b876c8e3b32dd2bb7eae74c.jpg)

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

```

![http://seaborn.pydata.org/_images/regression_42_0.png](img/eb0843cd39205e7144eb4acdf1b8356d.jpg)

## Controlling the size and shape of the plot

Before we noted that the default plots made by [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") and [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") look the same but on axes that have a different size and shape. This is because [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") is an “axes-level” function draws onto a specific axes. This means that you can make multi-panel figures yourself and control exactly where the regression plot goes. If no axes object is explicitly provided, it simply uses the “currently active” axes, which is why the default plot has the same size and shape as most other matplotlib functions. To control the size, you need to create a figure object yourself.

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

```

![http://seaborn.pydata.org/_images/regression_44_0.png](img/a94aa08c017f4688743921ccc9d8a4d0.jpg)

In contrast, the size and shape of the [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot") figure is controlled through the [`FacetGrid`](../generated/seaborn.FacetGrid.html#seaborn.FacetGrid "seaborn.FacetGrid") interface using the `size` and `aspect` parameters, which apply to each _facet_ in the plot, not to the overall figure itself:

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

```

![http://seaborn.pydata.org/_images/regression_46_0.png](img/fce6798715088ab4d9f615ae89a67b2c.jpg)

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

```

![http://seaborn.pydata.org/_images/regression_47_0.png](img/ea6e78ca63d3d86dece589f475f2338d.jpg)

## Plotting a regression in other contexts

A few other seaborn functions use [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") in the context of a larger, more complex plot. The first is the [`jointplot()`](../generated/seaborn.jointplot.html#seaborn.jointplot "seaborn.jointplot") function that we introduced in the [distributions tutorial](distributions.html#distribution-tutorial). In addition to the plot styles previously discussed, [`jointplot()`](../generated/seaborn.jointplot.html#seaborn.jointplot "seaborn.jointplot") can use [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") to show the linear regression fit on the joint axes by passing `kind="reg"`:

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

```

![http://seaborn.pydata.org/_images/regression_49_0.png](img/0a3f41c0a016c66f0f3379c128f550b9.jpg)

Using the [`pairplot()`](../generated/seaborn.pairplot.html#seaborn.pairplot "seaborn.pairplot") function with `kind="reg"` combines [`regplot()`](../generated/seaborn.regplot.html#seaborn.regplot "seaborn.regplot") and [`PairGrid`](../generated/seaborn.PairGrid.html#seaborn.PairGrid "seaborn.PairGrid") to show the linear relationship between variables in a dataset. Take care to note how this is different from [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot"). In the figure below, the two axes don’t show the same relationship conditioned on two levels of a third variable; rather, [`PairGrid()`](../generated/seaborn.PairGrid.html#seaborn.PairGrid "seaborn.PairGrid") is used to show multiple relationships between different pairings of the variables in a dataset:

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

```

![http://seaborn.pydata.org/_images/regression_51_0.png](img/65fcd97ee44e136d797a4d343a58cc4f.jpg)

Like [`lmplot()`](../generated/seaborn.lmplot.html#seaborn.lmplot "seaborn.lmplot"), but unlike [`jointplot()`](../generated/seaborn.jointplot.html#seaborn.jointplot "seaborn.jointplot"), conditioning on an additional categorical variable is built into [`pairplot()`](../generated/seaborn.pairplot.html#seaborn.pairplot "seaborn.pairplot") using the `hue` parameter:

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

```

![http://seaborn.pydata.org/_images/regression_53_0.png](img/b166f746ed213b5ac4e495320f99b0fd.jpg)