A scatterplot is a type of chart that shows the relationship between two numerical variables. Each member of the dataset gets plotted as a point whose x-y coordinates relates to its values for the two variables.

This example shows how to create a scatterplot with seaborn and how to add a regression line.

## Libraries

First, we need to load a few libraries:

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

## Dataset

Since scatter plot are made for visualizing relationships between two numerical variables, we need a dataset that contains at least two numerical columns.

Here, we will use the `iris` dataset that we load directly from the gallery:

``````path = 'https://raw.githubusercontent.com/holtzy/The-Python-Graph-Gallery/master/static/data/iris.csv'

## Scatter plot

A scatterplot can be made using `regplot()` function of seaborn library. An example dataset from seaborn repository, iris dataset, is used in the example. The plot shows the relationship between sepal lenght and width of plants. In order to show the most basic utilization of this function, the following parameters should be provided:

• `x` : positions of points on the X axis
• `y` : positions of points on the Y axis
``````sns.regplot(
x=df["sepal_length"],
y=df["sepal_width"],
fit_reg=False
)
plt.show()``````

## Fit a linear regression model

By just removing the `fit_reg=False` parameter, the `regplot()` function will fit and plot a linear regression model to the scatterplot.

This is a good way to visualize the relationship between two variables.

``````sns.regplot(
x=df["sepal_length"],
y=df["sepal_width"]
)
plt.show()``````

## Going further

This post explains how to create a scatterplot with seaborn.

You might be interested in more advanced examples on how to visualize linear regression or how to color points according to a third variable.

## Contact & Edit

👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥