Graphical representations of linear models

This notebook provides a brief introduction to the plotting functions in seaborn that help visualize linear relationships between variables.

For much more detail, you can check out the documentation on graphing quantitative and categorical linear models.


In [1]:
import seaborn as sns
tips = sns.load_dataset("tips")
exercise = sns.load_dataset("exercise")
titanic = sns.load_dataset("titanic")

Linear models with quantitative variables

Seaborn visualizes linear regressions with regplot by plotting a regression line and confidence band over a scatterplot of the data:


In [2]:
sns.regplot("total_bill", "tip", tips);


The higher-level function lmplot can draw this plot separately for different parts of a dataset.


In [3]:
sns.lmplot("total_bill", "tip", hue="smoker", col="sex", data=tips);


You can also use the jointplot function to show the marginal distributions of the two variables.


In [4]:
sns.jointplot("total_bill", "tip", tips, kind="reg");


The interactplot function can be used to help understand complex interactions in your dataset.


In [5]:
sns.interactplot("age", "fare", "survived", titanic.dropna(), logistic=True);


Linear models with categorical variables

The low-level barplot and pointplot functions, and the higher-lever factorplot function, can draw similar plots in cases where you have categorical predictor variables.


In [6]:
sns.factorplot("time", "pulse", hue="kind", col="diet", data=exercise);



In [7]:
sns.factorplot("sex", "survived", hue="class", data=titanic, kind="bar", palette="Purples_d");


Linear relationships in large datasets

To explore many relationships simultaneously, you can plot a heatmap of the correlation values.


In [8]:
with sns.plotting_context(rc={"figure.figsize": (9, 9)}):
    sns.corrplot(titanic.dropna());


You can also fit a linear model and plot the model coefficients.


In [9]:
sns.coefplot("survived ~ pclass + scale(age) + sibsp + parch + scale(fare)", titanic.dropna());