Python Central HubSign in

Data Analytics with Python / Phase 7: Interactive Visualization with Plotly

Interactive Scatter Plots

Explore relationships with hover info, color, size, and trend lines in Plotly.

When to reach for a scatter plot

A scatter plot answers "how do these two numeric variables relate?" Each point is one row of data, positioned by two measurements at once. Add color for a third variable (a category) and size for a fourth (a number) — a scatter plot can honestly encode more information per point than almost any other chart type.

Basic scatter

Python
import pandas as pd
import plotly.express as px
 
df = pd.DataFrame({
    "hours": [1, 2, 3, 4, 5, 6, 7, 2, 4, 6],
    "score": [50, 55, 65, 70, 75, 78, 85, 52, 69, 77],
    "group": ["A", "A", "A", "A", "A", "A", "A", "B", "B", "B"],
})
 
fig = px.scatter(df, x="hours", y="score", color="group", title="Hours vs score")
fig.show()

Every point is hoverable by default — Plotly shows x, y, and color for whichever point the cursor is nearest, with zero extra code.

Add trend line

Python
import plotly.express as px
 
fig = px.scatter(df, x="hours", y="score", color="group", trendline="ols", title="Trend line")
fig.show()

trendline="ols" fits an ordinary-least-squares regression line per color group and draws it on top — a quick way to see if a relationship looks roughly linear (requires statsmodels installed).

sketch Scatter plot — hover finds the nearest point p5.js
Move your mouse near any dot; the closest point locks on and shows its (x, y) values, like a Plotly tooltip.

Tips

  • Use hover_data=[...] to show more context (e.g. a name or ID column) in the tooltip.
  • Trend lines can be sensitive to outliers — a single extreme point can tilt the whole fitted line.
  • If points overlap heavily, lower opacity or switch to a 2D density/contour plot.
diagram Reading a scatter plot mermaid
Each visual channel of a scatter point can carry a different variable.

Check your understanding

quizCheck yourself
  1. What question does a scatter plot answer?

    Show answer

    A — How two numeric variables relate — Each point is one row positioned by two measurements, so you see the relationship.

  2. What does trendline="ols" draw?

    Show answer

    B — An ordinary-least-squares regression line per color group — It fits an OLS line for each group and requires statsmodels to be installed.

  3. In the hours versus score example, what does color="group" encode?

    Show answer

    B — A third variable, the group (A or B) — color adds the group as an extra dimension without needing a third axis.

  4. Why check the point cloud and not only the trend line?

    Show answer

    B — A fitted line can look convincing on random data with a small sample, and it does not show causation — Correlation is not causation, and small samples can produce misleading lines.

Next

Continue to: Bubble Charts to add a size dimension to your scatter plots.

🧪 Try It Yourself

Exercise 1 – Plot Two Variables

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Color by Group

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Add a Trend Line

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did