Home » How to Change Marker Size in Seaborn Scatterplot

How to Change Marker Size in Seaborn Scatterplot

by Erma Khan

You can use the s argument within the scatterplot() function to adjust the marker size in a seaborn scatterplot:

import seaborn as sns

sns.scatterplot(data=df, x='x_var', y='y_var', hue='group_var', s=20)

The greater the value you provide for the s argument, the larger the points in the plot will be.

The following example shows how to use this syntax in practice.

Example: Change Marker Size in Seaborn Scatterplot

Suppose we have the following pandas DataFrame that contains information about the sales made during five consecutive days at two different retail stores:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'day': [1, 2, 3, 4, 5, 1, 2, 3, 4, 5],
                   'store': ['A', 'A', 'A', 'A', 'A',
                             'B', 'B', 'B', 'B', 'B'],
                   'sales': [3, 3, 5, 4, 7, 6, 8, 9, 12, 13]})

#view DataFrame
print(df)

   day store  sales
0    1     A      3
1    2     A      3
2    3     A      5
3    4     A      4
4    5     A      7
5    1     B      6
6    2     B      8
7    3     B      9
8    4     B     12
9    5     B     13

We can use the scatterplot() function in seaborn to create a scatterplot that displays the sales made each day at each store

import seaborn as sns

#create scatterplot with default marker size
sns.scatterplot(data=df, x='day', y='sales', hue='store')

We can use the s argument to increase the size of the points in the plot:

import seaborn as sns

#create scatterplot with increased marker size
sns.scatterplot(data=df, x='day', y='sales', hue='store', s=200)

Notice that the size of the points has increased.

However, the size of the points in the legend have remained the same.

To increase the size of the points in the legend, you can use the markerscale argument within the matplotlib legend() function:

import matplotlib.pyplot as plt
import seaborn as sns

#create scatterplot with increased marker size
sns.scatterplot(data=df, x='day', y='sales', hue='store', s=200)

#increase marker size in legend
plt.legend(markerscale=2)

Note that the default value for markerscale is 1.

By increasing this value, you can change the size of the markers relative to the originally drawn ones.

Feel free to play around with the s argument and markerscale argument to make the points in the scatterplot be the exact size that you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in seaborn:

How to Add a Title to Seaborn Plots
How to Change Font Size in Seaborn
How to Change the Position of a Legend in Seaborn

Related Posts