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.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.