How to Create a Scatter Plot in Seaborn ?

Spread the love

In this post you will learn How to create a Scatter Plot in Seaborn.

Scatter Plot in Seaborn –

Let’s read a dataset to work with.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

url = ""
df = pd.read_csv(url)

sns.relplot() –

There are two ways to create a scatter plot in seaborn. One using relplot() and another using scatterplot() . The relplot() is a figure level function for visualizing statistical relationships using two common approaches: scatter plots and line plots.

By Default relplot() plots a scatter plot.

scatterplot() (with kind=’scatter’, the default)

lineplot() (with kind=’line’)

Later we will see how to use scatterplot to create a scatter plot. Right now we will focus on the relplot().

To create a scatter plot, you have to define the x, y and the data in the relplot. Let’s plot strike rate (SR) on the x-axis and Runs on the y-axis.

sns.relplot(x="SR", y="Runs", data=df);

Right now, in this plot we have added two dimension. We can also add a third dimension to this plot using the hue parameter. Let’s add the Nationality of a player to the hue parameter.

sns.relplot(x="SR", y="Runs", hue="Nationality", data=df);

We can also use the style parameter to emphasize the difference between the classes.

sns.relplot(x="SR", y="Runs", hue="Nationality", style="Nationality", data=df);

In the example above the variable to the hue was categorical so seaborn uses the qualitative palette. If the hue variable is numeric then sequential palette is used.

sns.relplot(x="SR", y="Runs", hue="50", data=df);

You can also change the size of each points using the size parameter.

sns.relplot(x="SR", y="Runs", size="50", data=df);

We can also increase the size of these points using the sizes parameter.

sns.relplot(x="SR", y="Runs", size="50", sizes=(20, 200), data=df);

There is also a col parameter that create a faceted figure with multiple subplots arranged across the columns of the grid. Let’s create a separate plot for each teams.

sns.relplot(x="SR", y="Runs", col="Team",col_wrap=3, data=df);

There is also a row parameter for the rows. Let’s use a different dataset to show it.

tips = sns.load_dataset('tips')
sns.relplot(data=tips, x="total_bill", y="tip", hue="day", col="time", row="sex");

scatterplot() –

As I said before you can also use the scatterplot() function to create a scatter plot in seaborn. It is very similar to the relplot(). Let’s use it now.

sns.scatterplot(x="SR", y="Runs", data=df);

You can also use the hue parameter.

sns.scatterplot(x="SR", y="Runs", hue="Nationality", data=df)

You can also use the size and sizes parameters.

sns.scatterplot(x="SR", y="Runs", hue="Nationality",size="50", sizes=(20,200), data=df);

Rating: 1 out of 5.

Leave a Reply