← Back to Pandas Course | Chapter 12: Visualization Basics | Lesson 3 of 6

Scatter plot

A scatter plot shows each row as a dot so you can see relationships between two numeric columns.

In this page:

  1. Scatter plot
Syntax
python
df.plot.scatter(x="column1", y="column2", c="color", s=size)

Scatter plot

df.plot.scatter(x, y) draws one point per row. Use c for colour and s for size to add extra dimensions. Scatter plots reveal correlation, clusters and outliers.

Note: df.corr() gives the numeric correlation to match what you see.

Example: Scatter plot

python
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({"hours": [1, 2, 3, 4, 5], "score": [52, 58, 65, 70, 78]})
ax = df.plot.scatter(x="hours", y="score")
print("points:", len(ax.collections[0].get_offsets()))
print("correlation:", round(df["hours"].corr(df["score"]), 3))

# Output:
# points: 5
# correlation: 0.998
Related Topics
Common Mistakes
  1. Plotting non-numeric columns
  2. Ignoring overplotting
  3. Reading correlation as causation
Chapter Summary
  • scatter shows two numeric columns
  • c and s add colour and size
  • Reveals correlation
  • Points are rows
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.