Exercise: Fix the Misleading Charts

Fix four charts of a city's recycling numbers that run but mislead, a bar chart whose axis starts at 40, a line with no title or labels, two lines told apart only by red and green, and two charts side by side on different scales, then put four charts on one page saved as a PNG, and let pytest read each chart's Axes.

Journeyman · Python · Updated

By

Clearwater City wants a one-page summary of its recycling program for the city council, and the charts it has so far mislead. None of them has a wrong number in it: one starts its axis at 40, one has no title or labels, one tells two lines apart only by red and green, and one puts two years side by side on different scales. You'll fix each chart to exact requirements, then put four charts on one page and save it. Tests draw each chart and read what it holds, its title, its labels, its axes and its lines, without ever looking at a pixel.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-data-fix-misleading-charts, also linked from the Exercise box beside the lesson. Clone it into your python-course folder. In VS Code's terminal:

cd ~/python-course
git clone https://github.com/ask-codey/exercise-data-fix-misleading-charts.git

Git makes a folder, exercise-data-fix-misleading-charts, with the exercise code inside. Get the Exercise Code with Git walks through cloning step by step, if you'd like a reminder. Open that folder in VS Code (File > Open Folder), open its terminal with Terminal > New Terminal, and make the exercise's virtual environment. On Windows (PowerShell):

python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt

On a Mac (zsh):

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

The prompt starts with (.venv) once the virtual environment is on. The exercise's README says what each command does, and what to do if PowerShell won't run the second one. Then make the data, once:

python seed.py
Wrote pickups.csv: 144 rows.

The folder holds:

  • seed.py: makes pickups.csv, the city's recycling log: six neighborhoods, every month of 2024 and 2025, our own made-up data. Its fixed random seed means everyone gets the same rows.
  • charts.py: the charts, for you to fix and finish. Its helpers at the top, monthly_totals, recycling_rates and draw_neighborhood_tons, are ready to use.
  • explore.ipynb: a notebook for looking at each chart while you fix it. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: draws each chart with your functions, prints what it now holds, and saves the page as recycling-summary.png. Ready to use.
  • tests/: the tests, which draw your charts and read them, and conftest.py, which turns on matplotlib's Agg backend (it draws charts in memory, with no window) and gives every test its own fresh copy of the log. You don't need to change them.
  • requirements.txt and pytest.ini: the packages the exercise needs, at exact versions, and where pytest finds the tests and your code.

What to do

Each draw_ function draws one chart on the Axes it's given and returns it, so the same function can draw a chart on its own or as one chart of a page. Its docstring says what's wrong with it and exactly what the fixed chart must have:

def draw_rate_bars(ax, pickups):
    """Bars of each neighborhood's share of households recycling in 2025.

    The problem: the y-axis starts at 40, so a few points' difference looks huge.
    The fix: the y-axis runs from 0 to 100. Keep the bars, the title and the y-axis label.
    """

Fix the four charts, then build the page. Each requirement is exact, because the tests check it word for word:

  1. draw_rate_bars(ax, pickups): the y-axis runs from 0 to 100, not from 40. Keep the bars, the title and the y-axis label.
  2. draw_tons_by_month(ax, pickups): the title Recycling rose 18% after curbside pickup began, the x-axis label Month and the y-axis label Recycled (tons).
  3. draw_recycled_and_trash(ax, pickups): no colors of your own (take out color="red" and color="green"); the Recycled line has circle markers ("o") and a solid line ("-"), and the Trash line square markers ("s") and a dashed line ("--"). Keep the labels, the legend, the title and the y-axis label.
  4. before_and_after(pickups): the 2024 and 2025 charts share one y-axis (sharey=True).
  5. summary_page(pickups, path), which stops with NotImplementedError: Not built yet. for now: a 2×2 page, plt.subplots(2, 2, figsize=(10, 8), layout="constrained"), with draw_tons_by_month top left, draw_recycled_and_trash top right, draw_rate_bars bottom left and draw_neighborhood_tons for 2025 bottom right; the page title Clearwater City recycling, 2024 to 2025 with fig.suptitle; saved with fig.savefig(path, dpi=150, bbox_inches="tight"); and the Figure returned.

Each function leaves the table it's given unchanged; the tests check that too.

Check your work

Run the tests from the exercise folder, with the virtual environment on:

python -m pytest

At first all 13 tests fail. That's expected: each chart misleads and the page isn't built yet, and each fix you make turns its tests green. A failing test's message says what it read, what it expected and what it found, for example the rate chart's y-axis (ax.get_ylim()) should be (0.0, 100.0), but it was (40.0, 70.0).

To see your charts at work, run python report.py, then open recycling-summary.png in VS Code to look at the page.

Hints

  • Look before you fix. Run each chart's cell in the notebook and say what a reader would wrongly believe from it. Honest Axes: Where the Scale Starts shows the same trick with two neighborhoods.
  • One method sets both ends of an axis. ax.set_ylim takes the bottom and the top.
  • A chart's words are three calls. set_title, set_xlabel and set_ylabel, as Titles, Labels and Units That Explain shows.
  • More than color. marker= and linestyle= go in each ax.plot call, as Charts Everyone Can Read shows; leave color= out and matplotlib picks the colors.
  • One argument shares a scale, and one grid holds the page. plt.subplots takes sharey=True, and Several Charts on One Page: subplots and savefig draws four charts on axs[0, 0] to axs[1, 1].

When you're done

Every test is green: pytest's last line says 13 passed. And python report.py prints:

1) rate chart's y-axis: 0 to 100
2) monthly chart's title: Recycling rose 18% after curbside pickup began
3) monthly chart's y-axis label: Recycled (tons)
4) line markers: o, s
5) line styles: -, --
6) 2024 and 2025 share a y-axis: True
7) saved the page: recycling-summary.png
8) charts on the page: 4

Now open recycling-summary.png and read the page as a council member would. Which chart's title would you change to state its finding, the way the top-left one does?

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. Hand In Your Work with Git walks through each step.

  1. On GitHub, click +, then New repository. Name it (for example exercise-data-fix-misleading-charts), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In VS Code's terminal, in your exercise folder, send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-data-fix-misleading-charts.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-data-fix-misleading-charts, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

A misleading chart rarely has a wrong number in it; it has an axis, a missing label or a color choice that tells the wrong story. Fixing those, and checking them with tests, is what makes a chart page someone can trust.

Key terms

Related