Create a Scatter Plot with SeaBorn – Python Pandas

A scatter plot in Seaborn is used to visualize the relationship between two numerical variables with optional semantic groupings. The seaborn.scatterplot() function provides a powerful way to create scatter plots with various customization options.

Basic Syntax

The basic syntax for creating a scatter plot is ?

import seaborn as sns
import matplotlib.pyplot as plt

sns.scatterplot(data=df, x='column1', y='column2')
plt.show()

Creating Sample Data

Let's create sample cricket player data to demonstrate scatter plots ?

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

# Create sample cricket data
data = {
    'Age': [25, 28, 32, 22, 29, 31, 26, 24, 30, 27],
    'Weight': [70, 75, 80, 65, 78, 82, 72, 68, 85, 74],
    'Role': ['Batsman', 'Bowler', 'All-rounder', 'Batsman', 'Bowler', 
             'All-rounder', 'Batsman', 'Wicket-keeper', 'All-rounder', 'Bowler']
}

df = pd.DataFrame(data)
print(df.head())
   Age  Weight         Role
0   25      70      Batsman
1   28      75       Bowler
2   32      80  All-rounder
3   22      65      Batsman
4   29      78       Bowler

Basic Scatter Plot

Create a simple scatter plot showing the relationship between age and weight ?

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

# Sample data
data = {
    'Age': [25, 28, 32, 22, 29, 31, 26, 24, 30, 27],
    'Weight': [70, 75, 80, 65, 78, 82, 72, 68, 85, 74]
}

df = pd.DataFrame(data)

# Create scatter plot
sns.scatterplot(data=df, x='Age', y='Weight')
plt.title('Age vs Weight Scatter Plot')
plt.show()

Scatter Plot with Hue Parameter

Use the hue parameter to color points based on a categorical variable ?

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

# Sample data with roles
data = {
    'Age': [25, 28, 32, 22, 29, 31, 26, 24, 30, 27],
    'Weight': [70, 75, 80, 65, 78, 82, 72, 68, 85, 74],
    'Role': ['Batsman', 'Bowler', 'All-rounder', 'Batsman', 'Bowler', 
             'All-rounder', 'Batsman', 'Wicket-keeper', 'All-rounder', 'Bowler']
}

df = pd.DataFrame(data)

# Create scatter plot with hue
sns.scatterplot(data=df, x='Age', y='Weight', hue='Role')
plt.title('Age vs Weight by Player Role')
plt.ylabel('Weight (kg)')
plt.show()

Advanced Customization

Enhance your scatter plot with size and style parameters ?

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

# Extended sample data
data = {
    'Age': [25, 28, 32, 22, 29, 31, 26, 24, 30, 27],
    'Weight': [70, 75, 80, 65, 78, 82, 72, 68, 85, 74],
    'Role': ['Batsman', 'Bowler', 'All-rounder', 'Batsman', 'Bowler', 
             'All-rounder', 'Batsman', 'Wicket-keeper', 'All-rounder', 'Bowler'],
    'Experience': [5, 8, 12, 2, 9, 11, 6, 4, 10, 7]
}

df = pd.DataFrame(data)

# Create customized scatter plot
plt.figure(figsize=(10, 6))
sns.scatterplot(data=df, x='Age', y='Weight', hue='Role', size='Experience', 
                sizes=(50, 200), alpha=0.7)
plt.title('Cricket Players: Age vs Weight by Role and Experience')
plt.ylabel('Weight (kg)')
plt.xlabel('Age (years)')
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()

Key Parameters

Parameter Description Example
data DataFrame containing the data data=df
x, y Column names for x and y axes x='Age', y='Weight'
hue Variable for color grouping hue='Role'
size Variable for point size size='Experience'
style Variable for point markers style='Category'

Conclusion

Seaborn's scatterplot() function provides an intuitive way to create scatter plots with powerful grouping capabilities. Use the hue parameter for categorical color coding and combine with size and style for multi-dimensional data visualization.

Updated on: 2026-03-26T13:41:40+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements