Byte by Byte › Book 8: Real-World Coding, Data and AI › Chapter 9: Charts and Data Visualisation in Python
Chapter 9: Charts and Data Visualisation in Python

🔎 The Case
The council members nod politely at Memoon's statistics, but their eyes glaze over. Then Beautine holds up one hand-drawn line: the river rising day by day toward a red danger mark. The room goes silent. Everyone understands at once. A good chart turns numbers into a decision. A bad one confuses or misleads.
💡 The Concepts
Matplotlib is Python's standard charting library; pandas uses it behind the scenes. import matplotlib.pyplot as plt , then build the chart and call plt.show() .
Choose the chart by the question:
| Question | Chart | Matplotlib |
|---|---|---|
| How does something change over time? | Line chart | plt.plot(x, y) |
| How do categories compare? | Bar chart | plt.bar(labels, values) |
| How are values distributed? | Histogram | plt.hist(values, bins=10) |
| Are two variables related? | Scatter plot | plt.scatter(x, y) |
| What are the parts of one whole? | Pie chart (use sparingly) | plt.pie(values, labels=...) |
| Spread and outliers? | Box plot | plt.boxplot(values) |
Every chart needs: a title that states the point, labelled axes with units, a legend when there is more than one series, and a source.
Honest charts. In Book 5 you met misleading charts. Remember:
- A bar chart's value axis must start at zero, or small differences look huge.
- Don't use 3D effects; they distort sizes.
- Don't cherry-pick a time window to hide a trend.
- Use colour to highlight the point (one red danger line), not to decorate.
Subplots put several charts in one figure: fig, axes = plt.subplots(1, 2) .
🛠️ Build It
import pandas as pd import matplotlib.pyplot as plt
df = pd.read_csv("river_clean.csv", parse_dates=["date"])
for place, part in df.groupby("location"):
plt.plot(part["date"], part["level_m"], marker="o", label=place)
plt.axhline(3.2, color="red", linestyle="--", label="Danger mark (3.2 m)")
plt.title("Old Mill rose above the danger mark on 2 and 4 July")
plt.xlabel("Date")
plt.ylabel("Level (m)")
plt.legend()
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig("river_levels.png", dpi=150)
plt.show()
plt.scatter(df["rain_mm"], df["level_m"])
plt.title("More rain, higher river")
plt.xlabel("Rain (mm)")
plt.ylabel("Level (m)")
plt.show()
highest = df.groupby("location")["level_m"].max()
plt.bar(highest.index, highest.values)
plt.title("Highest level by gauge")
plt.ylabel("Level (m)")
plt.show()
Download river_levels.png from Colab's file panel. You will use charts like this in your capstone.
Now make a misleading version on purpose: draw the bar chart again with plt.ylim(2.5, 4) . Write one sentence on why it misleads.
✅ Case Closed
The council sees the line cross the dashed red mark twice in one week and votes to fund stronger gauges and a warning bell. Glim, who once made fake pictures to mislead people, quietly asks Beautine how to make charts that cannot be misread. "Start the axis at zero," she says, "and say where your data came from."
Career Spotlight: Data Visualisation Designer. Turns complex data into clear charts and dashboards for newsrooms, governments and companies.
❓ Test Yourself
- Which chart would you use for: (a) monthly phone data use over a year (b) marks of 40 students (c) height vs shoe size (d) votes for four candidates?
- List four things every chart should have.
- Why must a bar chart's axis start at zero?
- What does
plt.axhlinedraw? When is it useful? - What is a histogram's
binssetting? - Challenge: Make a histogram of the cleaned river levels with 5 bins, with title and axis labels, and describe its shape in one sentence.
📚 Key Terms
data visualisation · Matplotlib · pyplot · line chart · bar chart · histogram · bins · scatter plot · pie chart · box plot · axis · label · legend · title · subplot · savefig · misleading chart · truncated axis
Keep learning with Byte by Byte
Explore Book 8: Real-World Coding, Data and AI. Find the ebook, purchase details and teaching slides on MemuniCity.
Comments
Post a Comment