Visualizing the Reg Plots
Learn how to plot, design, and interpret reg plots for data visualizations.
We'll cover the following...
Overview
Reg plot stands for regression plot; we use it to build a linear regression model of our data to see how variables are related.
Plotting reg plot
Let’s get started by importing the required libraries and the mpg
dataset. Then, we draw a reg plot using the sns.regplot()
function and pass displacement
and mpg
to the x
and y
parameters, respectively. The regplot()
function draws a scatter plot for the displacement
and mpg
variables and then fits a regression line on top. It also computes the 95% confidence interval, represented as a shaded line below the main regression line. We can see a downward trend in the plot; as the displacement
increases, the mpg
decreases.
mpg_df = sns.load_dataset("mpg")sns.regplot(x = 'displacement', y='mpg', data= mpg_df)plt.savefig('output/graph.png')
We can set truncate=False
in the sns.regplot()
function to extend the regression line to the axis limit. By default, it’s set to True
, which enforces the regression line within the data points.
sns.regplot(y='mpg', x = 'displacement', data= mpg_df, truncate=False)plt.savefig('output/graph.png')
To plot a regression line without a scatter plot, we specify scatter=False
in the sns.regplot()
...