> For the complete documentation index, see [llms.txt](https://vecco-insight.gitbook.io/crash-visualization/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vecco-insight.gitbook.io/crash-visualization/seaborn/untitled-4/4.4.2-linear-relationship.md).

# 4.4.2 Linear Relationship

In statistics, linear regression is a linear approach to modeling the relationship between a scalar response and one or more explanatory variables.

A [scatterplot](/crash-visualization/seaborn/untitled-4/4.4.1-scatter-plot.md) can be a helpful tool in determining the strength of the relationship between two variables. If there appears to be no association between the proposed explanatory and dependent variables, then fitting a linear regression model to the data probably will not provide a useful model.&#x20;

Before attempting to fit a linear model to observed data, a modeler should first determine whether or not there is a relationship between the variables of interest. This does not necessarily imply that one variable causes the other, but that there is some significant association between the two variables.

There are two main functions that can visualize a linear relationship as determined through regression. `regplot()`performs a simple linear regression model fit and plot. `lmplot()` combines `regplot()` and `FacetGrid`. Compared to the first one,`lmplot()` is more computationally intensive and is intended as a convenient interface to fit regression models across conditional subsets of a dataset.

### 1. Regplot

```
import seaborn as sns
tips = sns.load_dataset("tips")
sns.regplot(x = 'total_bill', y = 'tip',data = tips)
```

![Regplot](/files/-MDMhdwWzCVSc5OE5o_O)

### 2. Lmplot

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

![lmplot](/files/-MDMj0HfIhlCqr9sxNh_)

It’s also possible to fit a linear regression when one of the variables takes discrete values. One option is to **add some random noise** (“jitter”) to the discrete values to make the distribution of those values more clear. The other is to collapse over the observations in each discrete bin to plot an estimate of central tendency along with a **confidence interval**.

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

![](/files/-MDMhdwQLy2Lr2kzClgU)

### 3. Conditioning Linear Regression

How does the relationship between these two variables change as a function of a third variable?

&#x20;While [`regplot()`](https://seaborn.pydata.org/generated/seaborn.regplot.html#seaborn.regplot) always shows a single relationship, `lmplot()` combines `regplot()` with `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 practice is to plot both levels on the same axes and to use color to distinguish them.

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

![](/files/-MDMhdwSt7r6WBH9uuYC)

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

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

![](/files/-MDMhdwO6CEn-mCRdI9r)

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

![](/files/-MDMhdwRSxifPdn0cx4L)

&#x20;In the figure below, the two axes don’t show the same relationship conditioned on two levels of a third variable; rather, `PairGrid()` is used to show multiple relationships between different pairings of the variables in a dataset.

{% hint style="info" %}
We can use **height** and **aspect** to control the height and width of each facet.
{% endhint %}

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

![](/files/-MDMheW7_Q1asO_gGbgA)

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

![](/files/-MDMhdwVdyg2WLJpRRpq)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vecco-insight.gitbook.io/crash-visualization/seaborn/untitled-4/4.4.2-linear-relationship.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
