| by Arround The Web | No comments

Seaborn Regplot

A regression analysis examines the associations between independent factors or predictors and dependent variables or attributes. There are many contradictory approaches to evaluating the regression model. In Python, the “seaborn” module method called “seaborn.regplot()” plots/draws the regression plot.

This write-up will deliver you with a thorough guide on Python “seaborn.regplot()” method using the below content:

What is the “seaborn.regplot()” Method in Python?

The “seaborn.regplot()” function in Python is utilized to plot/create data and a linear regression model fit. It is an axes-level function, which means that it takes an axes object as input and returns the same axes object.

Syntax

The syntax of the “seaborn.regplot()” is shown below:

seaborn.regplot(x, y, data=None, x_estimator=None, x_bins=None, x_ci='ci', scatter=True, fit_reg=True, ci=95, n_boot=1000, units=None, order=1, logistic=False, lowess=False, robust=False, logx=False, x_partial=None, y_partial=None, truncate=False, dropna=True, x_jitter=None, y_jitter=None, label=None, color=None, marker='o', scatter_kws=None, line_kws=None, ax=None)

Parameters

In the above syntax:

  • The “x” and “y” parameters are the input variables. They can be strings, series, or vector arrays. If they are strings, they should correspond to column names in the “data” DataFrame.
  • The “data” parameter is a DataFrame that contains the “x” and “y” parameters.
  • The “x_estimator” parameter is a callable function that accepts a vector and retrieves a scalar. This can be used to transform the “x” before constructing the regression model.
  • The “x_ci” parameter specifies the confidence intervals for the regression line. The default value is “None”, meaning that no confidence intervals are plotted.
  • The “order” attribute determines the order of the polynomial regression model. The default is “1”, which implies that a linear regression model is fit.
  • The “robust” parameter implies whether to use a robust regression model. The default value is “False”, meaning a standard linear regression model is fit.
  • The “ax” parameter is an axes object that can be used to specify the axes on which the plot will be drawn. If it is not specified, a new axes object will be created.
  • The other optional parameters perform special tasks when passed inside the “seaborn.regplot()” method.

Return Value

The “seaborn.regplot()” function returns the axes object on which the plot was created.

Example 1: Applying the “seaborn.regplot()” Function to Create a Regression Plot

The following code demonstrates the “seaborn.regplot()” method:

import seaborn

import matplotlib.pyplot as plt

data = seaborn.load_dataset("tips")

seaborn.regplot(x = "total_bill", y = "tip", data = data)

plt.show()

In the above code:

  • The “seaborn” and “matplotlib” modules are imported.
  • The “seaborn.load_dataset()” function is used to import the predefined dataset named “tips”.
  • The “seaborn.regplot()” function is used to plot the regression plot. This function draws a scatterplot of two parameters, “x” and “y”, along with a linear regression model fit. In this case, “x” is “total_bill”, “y” is “tip”, and the “data” parameter specifies the source dataset to use.

Output

The regression plot has been created based on the above snippet.

Example 2: Applying the “seaborn.regplot()” Function to Create a Regression Plot With Parameters

The below code uses the “markers” and “color” parameters to draw the regression plot:

import seaborn

import matplotlib.pyplot as plt

data = seaborn.load_dataset("tips")

seaborn.regplot(x = "total_bill", y = "tip",color="g", marker="*", data = data)

plt.show()

In the above code lines, the “seaborn.regplot()” function takes the “color” and “marker” parameters along with other parameters to draw the regression plot accordingly.

Output

The regression plot has been created.

Example 3: Applying the “seaborn.regplot()” Function to a Different Dataset

The below example code uses the “exercise” dataset instead and draws a regression plot:

import seaborn

import matplotlib.pyplot as plt

data = seaborn.load_dataset("exercise")

seaborn.regplot(x = "id", y = "pulse", data = data)

plt.show()

In this block of code, the “seaborn.load_dataset()” function takes the “exercise” dataset as an argument to load the predefined dataset. The “seaborn.regplot()” function is then utilized to plot the regression plot based on the values of the passed parameters.

Output

Conclusion

In Python, the “seaborn.regplot()” method of the “seaborn” module takes the “Datasets” and draws/constructs the regression plot. In this article, we applied the “seaborn.regplot()” method on various predefined datasets such as “tips” and “exercise” to draw the regression plot. This blog presented a detailed guide on Python’s “seaborn.regplot()” method using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply