How To Change Marker Size In Seaborn Scatterplot
Last Updated :
12 Jun, 2024
In Scatterplot Marker Size is important as it visually represents the data points' significance or weightage. To change the marker size in a Seaborn scatterplot, we have several options depending on whether you want a uniform size for all markers or varying sizes based on data values. In this article, we will explore two different methods/approaches to change marker size in seaborn scatterplot in Python.
Modifying Marker Size In Seaborn Scatterplot
Below are the possible approaches to change marker size in seaborn scatterplot in Python.
Method 1: Using the size
Parameter in scatterplot()
In this example, we are using the size parameter in the sns.scatterplot() function to change the marker size in a Seaborn scatterplot. The size parameter takes a column name from the data frame (data) that specifies the marker sizes. By assigning the size parameter to the 'size' column in our data, we can control the marker size based on the values in that column.
Syntax:
sns.scatterplot(x='x_column_name', y='y_column_name', size='size_column_name', data=data_frame)
- x_column_name: The column name in your DataFrame that contains the x-axis data.
- y_column_name: The column name in your DataFrame that contains the y-axis data.
- size_column_name: The column name in your DataFrame that contains the marker size data.
- data_frame: The DataFrame containing your data.
Python
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'x': [1, 2, 3, 4, 5],
'y': [10, 15, 12, 17, 20],
'size': [100, 200, 300, 400, 500] # Marker size data
}
# Create a scatterplot with marker size
sns.scatterplot(x='x', y='y', size='size', data=data)
# Show the plot
plt.show()
Output:

Method 2: Using Matplotlib's scatter
Function with the s
Parameter
In this example, we are using the plot function from Matplotlib along with Seaborn's scatterplot function. First, we create the initial scatterplot using Seaborn to set up the axes and color scheme based on the 'category' column. Then, we use Matplotlib's scatter function to overlay markers on the plot, specifying the marker shape ('o' for circles) and the marker size (s=data['size']) based on the 'size' column in our data.
Syntax:
plt.scatter(x_values, y_values, s=marker_sizes, marker='marker_shape')
- x_values: The x-axis values for the data points.
- y_values: The y-axis values for the data points.
- marker_sizes: An array or list containing the marker sizes corresponding to each data point.
- marker_shape: The shape of the marker, such as 'o' for circles, '^' for triangles, 's' for squares, etc.
Python
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'x': [1, 2, 3, 4, 5],
'y': [10, 20, 30, 40, 50],
'category': ['A', 'B', 'A', 'B', 'A'],
'size': [100, 150, 200, 250, 300]
}
# Creating a scatterplot with marker size based on a column
sns.scatterplot(x='x', y='y', hue='category', data=data)
plt.scatter(data['x'], data['y'], s=data['size'], marker='o')
plt.title('Scatterplot with Marker Size')
plt.show()
Output:
Using Matplotlib's scatter Function with the s ParameterMethod 3: Using s
Parameter in scatterplot()
with Fixed Size
For a uniform marker size for all points, use the s
parameter in scatterplot()
. This approach sets a fixed size for all markers.
Syntax:
sns.scatterplot(x='x_column_name', y='y_column_name', s=fixed_size, data=data_frame)
Example:
Python
import seaborn as sns
import matplotlib.pyplot as plt
data = {
'x': [1, 2, 3, 4, 5],
'y': [10, 15, 12, 17, 20]
}
# Create a scatterplot with fixed marker size
sns.scatterplot(x='x', y='y', s=200, data=data)
plt.show()
Output:
Using s Parameter in scatterplot() with Fixed SizeMethod 4: Using the scatter_kws
Parameter in Seaborn's relplot()
Seaborn's relplot()
provides a high-level interface for drawing scatterplots, and you can pass marker size through the scatter_kws
parameter.
Syntax:
sns.relplot(x='x_column_name', y='y_column_name', size='size_column_name', data=data_frame, scatter_kws={'s': marker_sizes})
Example:
Python
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
data = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [10, 15, 12, 17, 20],
'size': [100, 200, 300, 400, 500] # Marker size data
})
# Create a relational plot with marker size
sns.relplot(x='x', y='y', data=data, kind='scatter', size='size')
plt.show()
Output:
Using the scatter_kws Parameter in Seaborn's relplot()Customizing Scatterplot Marker Size and Style in Seaborn
1. Changing Marker Style
sns.scatterplot(x="total_bill", y="tip", data=tips_dataset, marker='v')
You can change the marker style by passing a marker style value to the marker
 attribute.
2. Setting Context for Larger Plots
sns.set_context('poster')
sns.scatterplot(x="total_bill", y="tip", data=tips_dataset)
This method adjusts the context to make the plot suitable for larger displays like posters.
Conclusion
By using these methods, you can effectively modify the marker size in Seaborn scatterplots to better visualize and interpret your data. Choose the method that best suits your data visualization needs and enhances the clarity of your scatterplots.