Python Central HubSign in

Data Analytics with Python / Phase 5: Data Visualization with Matplotlib

Scatter Plot

Explore relationships between two numeric variables and handle overplotting and annotations.

A scatter plot places one point per pair of values (x, y), so you can see at a glance whether two variables move together. McKinney uses this exact technique to check relationships in economic data โ€” for example, plotting the change in money supply (m1) against the change in unemployment (unemp) to look for a pattern.

diagram Reading a scatter plot mermaid
Each point is one observation; the overall pattern of points reveals the relationship between two variables.

Basic scatter plot

Python
import matplotlib.pyplot as plt
 
x = [1, 2, 3, 4, 5, 6, 7]
y = [50, 55, 65, 70, 75, 78, 85]
 
plt.figure(figsize=(7, 4))
plt.scatter(x, y)
plt.title("Hours vs score")
plt.xlabel("Hours")
plt.ylabel("Score")
plt.grid(True, alpha=0.3)
plt.tight_layout()
plt.show()

Handle overplotting

Use transparency:

Python
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
plt.scatter(x, y, alpha=0.6)
plt.title("Scatter with alpha")
plt.tight_layout()
plt.show()

Annotate points

Python
import matplotlib.pyplot as plt
 
plt.figure(figsize=(7, 4))
plt.scatter(x, y)
 
plt.annotate("top", xy=(7, 85), xytext=(6, 88), arrowprops={"arrowstyle": "->"})
 
plt.title("Annotated scatter")
plt.tight_layout()
plt.show()

Visualize it

Each dot below is one (x, y) observation โ€” watch the cloud of points build up and notice the upward trend:

sketch A scatter plot shows one point per observation p5.js
Every (x, y) pair becomes one dot; the pattern of dots reveals whether the two variables relate.

๐Ÿงช Try It Yourself

Exercise 1 โ€“ Draw a scatter plot

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 โ€“ Control transparency

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 โ€“ Annotate a point

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.

Check your understanding

quizCheck yourself
  1. What does one point in a scatter plot represent?

    Show answer

    A โ€” One (x, y) pair of values โ€” Scatter plots show whether two variables move together, one point per observation.

  2. In the Hours vs score example, what pattern do the points show?

    Show answer

    A โ€” Score rises as hours rise โ€” The scores climb from 50 to 85 as hours go from 1 to 7, an upward trend.

  3. How does the lesson handle overplotting?

    Show answer

    A โ€” Use transparency with alpha, for example alpha=0.6 โ€” With transparency, overlapping points look darker, which reveals dense regions.

  4. Which function labels one point with an arrow?

    Show answer

    A โ€” plt.annotate โ€” annotate takes the text, the point xy, the text position xytext and arrowprops for the arrow style.

Next

Continue to Pie Chart to see how (and when) to show parts of a whole.

Was this page helpful?

Let us know how we did