How to Set Marker Size in Seaborn Scatterplots

Manav Narula Feb 02, 2024
How to Set Marker Size in Seaborn Scatterplots

A scatter plot is considered to be one of the most basic and frequently used graphs. It can help in identifying any underlying pattern between the variables and show their relation.

In Python, the seaborn module is considered very efficient for creating different types of plots. It is based on the matplotlib library and is relatively easy to use. The scatterplot() function from the seaborn module can be to create scatter plots.

In this tutorial, we will discuss how to set the size of the markers in scatter plots.

To set the size of markers, we can use the s parameter. This parameter can be used since seaborn is built on the matplotlib module. We can specify this argument in the scatterplot() function and set it to some value.

For example,

import random
import seaborn as sns
import matplotlib as plt

s_x = random.sample(range(0, 100), 20)
s_y = random.sample(range(0, 100), 20)

sns.scatterplot(y=s_y, x=s_x, s=70)

seaborn marker size

Alternatively, we can control the size of the points based on some variables. In this method, we specify the required variable as the value of this parameter.

See the code below.

import random
import seaborn as sns
import matplotlib as plt

s_x = random.sample(range(0, 100), 20)
s_y = random.sample(range(0, 100), 20)

sns.scatterplot(y=s_y, x=s_x, s=s_x)

seaborn marker size

We used the s_x variable to control the size of the marker, so for higher values of s_x the size of the points was bigger. Alternatively, we can also use the size parameter to achieve the same. It also alters the size of the marker, based on some variables and adds a legend to make things clearer.

For example,

import random
import seaborn as sns
import matplotlib as plt

s_x = random.sample(range(0, 100), 20)
s_y = random.sample(range(0, 100), 20)

sns.scatterplot(y=s_y, x=s_x, size=s_x, sizes=(50, 150))

seaborn marker size

Generally, the sizes parameter is also used, which specifies the range for the size parameter. If we use the legend argument and set it to full, then the size for every unique marker will be displayed.

Additionally, we can use the color or the marker parameter to change the color and shape of the marker. There are many different shapes available for the marker in the scatter plot using the seaborn module.

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