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 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
MediumA 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.
- Draw one dot per tree: that column across,
height_mup. - Label the x axis with the column's name.
Yours should look like this one, for another park's crown_m:
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()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Data and Plots
7pandas DataFrames
SeriesDataFramesReading CSV FilesInspecting DataSelecting ColumnsRecap: Movie Ratings10Plotting DataFrames
Plotting a DataFrameScatter from ColumnsOne Color per GroupHistogram of a ColumnRecap: Cafe Sales2NumPy Arrays
Creating ArraysShape and SizeRanges of NumbersIndexing and SlicingRecap: Temperature GridPractice on your own: Online Python compiler