#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
5 min read min read

Interactive Scatter Plots

Learn to create rich interactive scatter plots

Interactive Scatter Plots

Basic Interactive Scatter

code.py
import plotly.express as px
import pandas as pd

df = pd.DataFrame({
    'Age': [25, 30, 35, 40, 45, 50],
    'Salary': [40000, 50000, 60000, 70000, 80000, 90000],
    'Name': ['John', 'Sarah', 'Mike', 'Emma', 'Tom', 'Lisa']
})

fig = px.scatter(df, x='Age', y='Salary')
fig.show()

Show Info on Hover

code.py
fig = px.scatter(df, x='Age', y='Salary', hover_name='Name')
fig.show()

Hover shows the name!

Add More Hover Data

code.py
df['Department'] = ['Sales', 'IT', 'Sales', 'IT', 'HR', 'HR']

fig = px.scatter(df, x='Age', y='Salary',
                 hover_name='Name',
                 hover_data=['Department'])
fig.show()

Color by Category

code.py
fig = px.scatter(df, x='Age', y='Salary',
                 color='Department',
                 hover_name='Name')
fig.show()

Size by Value

code.py
df['Experience'] = [2, 5, 8, 12, 15, 20]

fig = px.scatter(df, x='Age', y='Salary',
                 color='Department',
                 size='Experience',
                 hover_name='Name')
fig.show()

Add Trendline

code.py
fig = px.scatter(df, x='Age', y='Salary', trendline='ols')
fig.show()

ols = ordinary least squares (best fit line)

Facet (Small Multiples)

Create separate charts per category:

code.py
fig = px.scatter(df, x='Age', y='Salary',
                 facet_col='Department')
fig.show()

Custom Markers

code.py
fig = px.scatter(df, x='Age', y='Salary',
                 color='Department',
                 symbol='Department')  # Different shapes
fig.show()

Set Axis Range

code.py
fig = px.scatter(df, x='Age', y='Salary')
fig.update_xaxes(range=[20, 55])
fig.update_yaxes(range=[30000, 100000])
fig.show()

Add Marginal Plots

Show distributions on the sides:

code.py
fig = px.scatter(df, x='Age', y='Salary',
                 marginal_x='histogram',
                 marginal_y='box')
fig.show()

Complete Example

code.py
import plotly.express as px
import pandas as pd

# Sample employee data
df = pd.DataFrame({
    'Name': ['John', 'Sarah', 'Mike', 'Emma', 'Tom', 'Lisa', 'Bob', 'Amy'],
    'Age': [25, 30, 35, 40, 28, 45, 50, 33],
    'Salary': [45000, 55000, 65000, 75000, 50000, 85000, 95000, 60000],
    'Department': ['Sales', 'IT', 'Sales', 'IT', 'HR', 'IT', 'Sales', 'HR'],
    'Experience': [2, 5, 8, 12, 3, 15, 20, 6]
})

fig = px.scatter(df, x='Age', y='Salary',
                 color='Department',
                 size='Experience',
                 hover_name='Name',
                 hover_data=['Experience'],
                 title='Employee Age vs Salary',
                 labels={'Age': 'Employee Age', 'Salary': 'Annual Salary'})

fig.show()

Key Points

  • hover_name shows main label on hover
  • hover_data adds extra info
  • color colors by category
  • size sizes by value
  • trendline='ols' adds best fit line
  • facet_col creates small multiples
  • marginal_x/y adds distribution plots

What's Next?

Learn to create interactive line charts.

SkillsetMaster - AI, Web Development & Data Analytics Courses