Lesson 9 of 144 min
Plotting to see data
Each plot answers one kind of question: a histogram shows how values spread, a scatter plot how two values move together, and a line plot how one value changes over steps.
After this lesson you can draw a histogram, a scatter plot and a line plot, and say what each shows.
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
Put words on the plot
The board names the two directions for you. In your own code, matplotlib draws the plot with no words on it. Three lines add them: what runs across, what runs up, and a title over the plot.
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv("penguins.csv")
plt.hist(df["body_mass_g"])
plt.xlabel("mass (grams)")
plt.ylabel("penguins")
plt.title("How heavy are the penguins?")
plt.show()More bins, or fewer
hist cuts the values into ten bins unless you ask for another number. With bins=30 the bars get thin and you see more detail. Too many, and each bar holds only a few penguins, so the shape gets ragged. Too few, and two peaks can melt into one.
counts, edges, bars = plt.hist(df["body_mass_g"], bins=30)
print(len(counts), "bins, each", round(edges[1] - edges[0]), "grams wide")
plt.show()30 bins, each 120 grams wide
One colour for each kind
Give scatter one kind of penguin at a time, and each gets its own colour. label names it, and plt.legend() shows the names. The cloud from the video splits up: the Gentoo sit apart, with long flippers and heavy bodies. Two values that rise together don't mean one makes the other grow. Here the kind of penguin drives both.
for kind, rows in df.groupby("species"):
plt.scatter(rows["flipper_length_mm"], rows["body_mass_g"], label=kind)
plt.xlabel("flipper (mm)")
plt.ylabel("mass (grams)")
plt.legend()
plt.show()The same plots, straight from the table
You will often see a table draw itself. df.plot calls matplotlib for you and puts the column names on the plot. You will also meet seaborn, a library built on matplotlib that colours by a column in one call.
df.plot.scatter(x="flipper_length_mm", y="body_mass_g")
plt.show()
df["body_mass_g"].plot.hist(bins=20)
plt.show()Run on Python 3.13.3, matplotlib 3.11.2 and pandas 3.1.1. Checked on .
Quiz
Two questions on this lesson. Answer as often as you like.
1 of 2
You want to see if penguins with longer flippers are heavier. Which line draws the plot that answers it?
2 of 2
The masses go from 2,700 to 6,300 grams. plt.hist(mass) cuts them into ten bins of the same width. How wide is each bin?
The ideas in this lesson
- Histogram
- A histogram cuts the values into equal bins and draws a bar for each, as tall as its count.
- Scatter plot
- A scatter plot draws one dot for each row, across by one value and up by another.
- Line plot
- A line plot joins values in their order: right for steps, like training, and wrong when the order means nothing.
It leans on
- Loss
Transcript
What the video says, part by part. Press a time to watch from there.
Did they get heavier?
Here's a mystery. This is the mass of hundreds of penguins. It's drawn as a line plot, the first plot most people learn. Two thirds of the way along, the line jumps up. So did the penguins get heavier over time?
One line draws a plot
We'll come back to it. First, two other plots.
Here's the code. Lines four to six read the table of penguins with pandas. Then they take two of its columns: the mass, and the flipper length.
Line two is new. It brings in a library for plots, matplotlib, under the short name plt.
And plt draws plots.
Line seven draws one. Line eight shows it.
A histogram
Which masses are common, and which are rare?
Line seven says hist, short for histogram, of mass. It cuts the masses, from under three kilos to over six, into ten ranges of the same width. Ten, unless you ask for another number. Each range is called a bin.
It counts the penguins in each bin, and draws a bar as tall as the count.
That's a histogram.
Read it
So, where is the tallest bar?
Around three and a half kilos. That bar holds seventy-one penguins: the most common range. The two bars at the heavy end hold only twenty-two penguins together. Heavy penguins are rare.
A histogram shows how values spread: which are common, and which are rare.
A scatter plot
The table has a row for each penguin, with its flipper length and its mass. Do penguins with longer flippers weigh more?
Line seven now says scatter, of flipper and mass. It draws one dot for each penguin. Across, its flipper length. Up, its mass. Every penguin gets a dot, but two. Nobody measured those two.
They grow together
Before you look closely: do the dots rise, fall, or make no shape at all?
They rise to the right. Longer flippers, heavier penguins. The two grow together.
One penguin, one dot.
A line plot
The table has a row for each penguin, in the file's order. Back to the first picture. Line seven now says plot, of mass. A line plot joins values in their order.
Across is not time. It's the row number, from zero to the last row. The line joins row zero to row one, to row two, and on.
So what is the jump, at row two hundred and twenty? The file lists the penguins by kind. First Adelie, then Chinstrap. And from there on come the Gentoo, the heaviest kind.
If you said they got heavier: a line that jumps up looks like a change over time. But here, across is only the order of the file... so the line says nothing about time.
Over steps
So when is a line plot right? When the order is steps, one after another. Here's a real case: a model, a program that learns from examples. This one is learning to read handwritten digits. After each round through all the pictures, it writes down its loss: a number for how wrong it still is. A hundred rounds, in order.
As the model learns, what should its loss do?
It falls. Fast in the first rounds... then slower and slower. Here, each point is one more round of training, in order. So the line tells you something: the model learned most at the start.
The question, then the plot
So here's what you saw. Pick the plot by the question.
How values spread: a histogram. Two values together: a scatter plot. One value over steps: a line.
Pick the plot
So the question you ask picks the plot you draw. Now try it on this lesson's page, and take the quiz. Find a histogram with two peaks.
You use this lesson in Lab 4: From a table to a first prediction, after The scikit-learn pattern.
Facts last checked on .
