Menu
CoddyTech

Scatter from Columns

Part of the Data and Plots section of Coddy's Machine Learning journey. Lesson 48 of 60.

Does a bigger battery mean a longer range? A scatter of two columns answers at a glance, one dot per row:

A scatter titled E-bike Range: seven e-bikes as dots, battery in Wh across and range in km up, rising together from 250 Wh and 35 km to 750 Wh and 110 km

A column goes wherever a list went, so ax.scatter takes two columns:

bikes = pd.DataFrame({
    "battery_wh": [250, 300, 400, 500, 500, 625, 750],
    "range_km": [35, 45, 60, 65, 75, 95, 110],
})
fig, ax = plt.subplots()
ax.scatter(bikes["battery_wh"], bikes["range_km"])
ax.set_title("E-bike Range")
ax.set_xlabel("Battery (Wh)")
ax.set_ylabel("Range (km)")
plt.show()

Dots rising to the right mean the columns grow together: a relationship a model can learn. A shapeless cloud means none.

ax.scatter never sees the column names, so the axis labels are yours to set.

challenge icon

Challenge

Medium

A park ranger measured 12 trees in trees.csv: age_years, trunk_cm and height_m. Which goes with height, age or trunk width? The starter reads one of those two names into column and sets up the chart.

  1. Draw one dot per tree: that column across, height_m up.
  2. Label the x axis with the column's name.

Yours should look like this one, for another park's crown_m:

A scatter titled Tree Size with crown_m across and Height (m) up: ten dots rising loosely, from trees about 6 m tall with small crowns to 14 m with 7.5 m crowns

Coddy runs your code for both columns and describes each chart. Compare them: the tighter rising band answers the question.

Try it yourself

import pandas as pd
import matplotlib.pyplot as plt

trees = pd.read_csv("trees.csv")
column = input()

fig, ax = plt.subplots()
ax.set_title("Tree Size")
ax.set_ylabel("Height (m)")

# Write your code below

# Don't change the line below
plt.show()
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Data and Plots

Practice on your own: Online Python compiler