How to Plot the Graph Using the seaborn.lmplot() Function

Manav Narula Feb 02, 2024
How to Plot the Graph Using the seaborn.lmplot() Function

The seaborn module is used for creating statistical plots in Python. It is built on the matplotlib module, so it is very simple to use.

The seaborn.lmplot() function creates a basic scatter plot using the given data onto a FacetGrid.

See the following code.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import random

x = range(50)
y = random.sample(range(100), 50)
cat = [i for i in range(2)] * 25
df = pd.DataFrame({"x": x, "y": y, "Category": cat})

sns.lmplot(x="x", y="y", data=df, fit_reg=False, hue="Category")

seaborn lmplot function

However, the use for this function exceeds over plotting scatter plots. It can also be used to understand the relationship between the data by plotting an optional regression line in the plot. It can also be used for logistic regression.

Unlike the seaborn.regplot() function which is also used to perform simple regression and plot the data, the seaborn.lmplot() function combines the seaborn.FacetGrid() class with the seaborn.regplot() function.

The FacetGrid() is used to visualize the relationship between data distribution with other subsets of data and can be used to create grids for multiple plots. It works on three axes providing rows, columns, and hues. It is very useful when we are working with a complicated dataset.

We can also customize the final figure using the different parameters with the seaborn.lmplot() function. We can supply the necessary customizations, like the color of the plot, as key-value pairs of a dictionary to the line_kws and scatter_kws parameter.

In the below code, we will plot a graph with a regression line using this function.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import random

x = range(50)
y = random.sample(range(100), 50)
cat = [i for i in range(2)] * 25
df = pd.DataFrame({"x": x, "y": y, "Category": cat})

sns.lmplot(x="x", y="y", data=df, hue="Category")

seaborn lmplot function

Note that the fit_reg parameter is set to True by default. Our dataset had multiple categories, so we were able to plot multiple regression lines. If we had removed the hue parameter, then a single regression plot would have been obtained. We can further use many parameters for our regression also. Some of these include the jitter argument, which is used to add some noise to the data, or the estimator parameter used to plot over a given estimated value.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn