Scatter Plot in Plotly
Last Updated: 08 Nov 2025
Scatter plots show individual data points as markers — perfect for correlation, clustering, outliers, or 2D relationships.
Hinglish Tip: Scatter plot = "har point ko alag dikhana" — jaise stars in the sky. No line, just dots with meaning.
Basic Scatter Plot
import plotly.express as px
# Sample data
x = [1, 2, 3, 4, 5]
y = [10, 15, 13, 18, 22]
fig = px.scatter(x=x, y=y, title="Basic Scatter")
fig.show()
Key Parameters in px.scatter()
| Parameter | Values / Shorthand | Purpose |
|---|---|---|
| data_frame | df (Pandas DataFrame) | Use real dataset |
| x, y | Column names or lists | X and Y axis data |
| color | Column name or list | Color by category |
| size | Column name | Bubble size |
| hover_name | Column | Label on hover |
| hover_data | List of columns | Extra info on hover |
| symbol | Column or marker type | Different shapes |
| title | String | Plot title |
| labels | Dict: 'col': 'New Name' | Rename axes |
| width, height | Pixels (e.g. 800) | Figure size |
Full Featured Example
import plotly.express as px
import pandas as pd
# Sample dataset
df = pd.DataFrame({
'City': ['Delhi', 'Mumbai', 'Bangalore', 'Chennai', 'Kolkata'],
'Temperature': [32, 30, 28, 35, 29],
'Humidity': [60, 80, 70, 75, 85],
'Pollution': [120, 95, 80, 110, 130],
'Region': ['North', 'West', 'South', 'South', 'East']
})
fig = px.scatter(
df,
x='Temperature',
y='Humidity',
size='Pollution',
color='Region',
hover_name='City',
hover_data={'Pollution': True, 'Temperature': False},
symbol='Region',
title='City Climate Analysis: Temp vs Humidity',
labels={
'Temperature': 'Temp (°C)',
'Humidity': 'Humidity (%)',
'Pollution': 'AQI Level'
},
width=900,
height=600
)
fig.update_layout(
legend_title="Region",
font=dict(size=12)
)
fig.show()
Hinglish Tip:
size='Pollution'→ bada bubble = zyada pollution — ek nazar mein samajh aayega!
Add Trendline (Regression)
fig = px.scatter(df, x='Temperature', y='Humidity', trendline="ols", title="With Trendline")
fig.show()
Options:
"ols","lowess","rolling","expanding"
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| No hover info | Use hover_name + hover_data |
| Too many points → slow | Use render_mode='webgl' for 100k+ points |
| Default colors hard to read | Use color_discrete_sequence=px.colors.qualitative.Bold |
| Can't export | Install kaleido: pip install kaleido |
| Small plot | Set width=900, height=600 |