Learning Goals
3 min- Draw a labelled pie chart with
ax.pie. - Recognise when a bar chart would be more honest.
- Draw a scatter plot with
ax.scatter. - Add a trend line and read correlation visually.
Warm-Up · When To Use What
5 minpie proportions of a single whole, ≤ 5 slices bar comparing categories — almost always better than pie line change over time scatter relationship between TWO numeric variables
Pie charts only work with small numbers of slices and clear majorities. Anything more nuanced — use a bar chart. Scatter plots are how you spot correlation; correlation isn't causation, and your README should say so.
New Concept · pie & scatter
14 minPie chart
import matplotlib.pyplot as plt labels = ["Roti", "Milo", "Nasi", "Teh"] counts = [120, 95, 180, 60] fig, ax = plt.subplots(figsize=(6, 6)) ax.pie(counts, labels=labels, autopct="%1.1f%%", startangle=90, counterclock=False) ax.set_title("Sales share · May 2026") fig.savefig("share.png", dpi=200)
autopct="%1.1f%%"prints each slice's percentage on it.startangle=90, counterclock=False= the canonical "top, clockwise" order.- An
explodetuple separates one slice for emphasis.
Scatter
x = df["hours_studied"] y = df["score"] fig, ax = plt.subplots(figsize=(7, 5)) ax.scatter(x, y, alpha=0.6, s=40) ax.set_xlabel("Hours studied") ax.set_ylabel("Score") ax.set_title("Study time vs score")
alpha makes overlapping dots visible; s is the marker size.
Trend line
import numpy as np m, b = np.polyfit(x, y, 1) # linear fit ax.plot(x, m*x + b, color="red", linewidth=2, label=f"y = {m:.2f}x + {b:.1f}") ax.legend()
Encode a third variable
ax.scatter(x, y, c=df["age"], cmap="viridis", s=df["score"]) # colour by age, size by score
Worked Example · Pie + Scatter Side by Side
12 minimport pandas as pd import matplotlib.pyplot as plt import numpy as np df = pd.read_csv("clean.csv", parse_dates=["date"]) df["total"] = df["quantity"] * df["price"] # Pie: revenue share by product share = df.groupby("product")["total"].sum().sort_values(ascending=False) fig, axes = plt.subplots(1, 2, figsize=(12, 5)) axes[0].pie(share, labels=share.index, autopct="%1.1f%%", startangle=90, counterclock=False, wedgeprops={"edgecolor": "white", "linewidth": 1}) axes[0].set_title("Revenue share by product") # Scatter: quantity vs price (per order) axes[1].scatter(df["quantity"], df["price"], alpha=0.6, s=40) m, b = np.polyfit(df["quantity"], df["price"], 1) xs = np.linspace(df["quantity"].min(), df["quantity"].max(), 50) axes[1].plot(xs, m*xs + b, color="red", linewidth=2, label=f"trend: y = {m:.2f}x + {b:.1f}") axes[1].set_xlabel("Quantity per order") axes[1].set_ylabel("Price (RM)") axes[1].set_title("Order size vs unit price") axes[1].legend() fig.tight_layout() fig.savefig("share_and_corr.png", dpi=200) plt.show()
Read the diff
Two panels, two stories. The pie answers "what fraction of revenue comes from each product?"; the scatter (with a fitted trend line) answers "do bigger orders come at lower per-unit prices?". The legend on the scatter shows the regression equation so the trend is auditable.
Try It Yourself
13 minPie chart showing the count of each product. Add percentages.
The same data as a horizontal bar chart with values labelled. Which is easier to read?
From a dataset of your choice, plot two numeric vars; encode a third as colour and a fourth as marker size. Add a colour bar.
Hint
scatter = ax.scatter(x, y, c=z, cmap="viridis", s=w*5, alpha=0.7) fig.colorbar(scatter, label="z")
Mini-Challenge · Pearson Correlation
8 minFor any two numeric columns of your data, compute Pearson's r (df[col1].corr(df[col2])) and draw the scatter with the value in the title. Interpret in one sentence.
Recap
3 minPie charts: only for proportions of a whole, ≤ 5 slices. Otherwise use a bar. Scatter: two numerics; alpha + size to handle overlapping; fit a trend line with np.polyfit for clarity. Tomorrow we compose multiple charts on one figure.
Homework
4 minProduce two PNGs from your data: one pie showing share of a whole (with rationale why pie is appropriate); one scatter with trend line showing a relationship you suspect. Write one paragraph for each interpreting what you see.