Exercise: Hilltop Creamery Charts

Write four chart functions for an ice cream shop's year, a line of the cones sold each day, bars of the scoops by flavor, a scatter plot of temperature against cones and a histogram of order totals, each returning fig and ax, and let pytest read each chart's title, labels and data.

Journeyman · Python · Updated

By

Hilltop Creamery's owner wants the shop's year on four charts for the end-of-year meeting: the cones sold each day, the flavors from best seller down, whether hot days sell more cones, and what a typical order comes to. You'll write the four functions that draw them. Each one returns its fig and ax, and the tests read each chart, its title, its labels and the data it draws, without looking at a single pixel.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-data-creamery-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-creamery-charts.git

Git makes a folder, exercise-data-creamery-charts, with the exercise code inside. 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, and the exercise's README says what each command does. requirements.txt adds matplotlib to the packages you've used so far. Then make the data, once:

python seed.py
Wrote daily.csv: 365 rows.
Wrote flavors.csv: 12 rows.
Wrote orders.csv: 5000 rows.

The folder holds:

  • seed.py: makes the shop's three files, our own made-up data, the same on every computer: daily.csv (one row for each day of 2025: date, high_temp_f, cones_sold, rainy), flavors.csv (12 flavors in menu order: flavor, scoops_sold) and orders.csv (July's 5,000 orders: order_id, items, order_total).
  • charts.py: the four functions, for you to finish.
  • explore.ipynb: a notebook for looking at the data and trying your charts. Choose the .venv kernel with Select Kernel (top right) before you run it. The tests don't read it.
  • report.py: makes the four charts with your functions, saves them as PNG files in a charts folder, and prints what each holds. Ready to use.
  • tests/: the tests, which call each function and read the chart it returns, and conftest.py, which turns on matplotlib's Agg backend (it draws in memory, so no window opens) and gives every test a fresh copy of the data. 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

Every function in charts.py is already declared with its name and parameters, and for now it stops with a NotImplementedError that says Not built yet. The docstring under each one says exactly what its chart must show:

def flavor_bars(flavors):
    """A bar chart of the scoops sold of each flavor, largest first.

    flavors has a flavor column and a scoops_sold column, in menu order.
    One vertical bar a flavor (ax.bar), sorted from the most scoops on the left to the fewest on
    the right, with each flavor's name under its bar.
    Title: "Scoops sold by flavor, 2025"
    x-axis label: "Flavor"
    y-axis label: "Scoops sold"
    Turn the flavor names on their side with ax.tick_params(axis="x", labelrotation=90) so they fit.

    Flavors with 900, 4000 and 2500 scoops: the bars' heights, left to right, are 4000, 2500, 900.
    """
    raise NotImplementedError("Not built yet.")

Each function starts with fig, ax = plt.subplots(), draws on ax, and ends with return fig, ax. The titles and labels must match exactly, capital letters and punctuation included:

  1. cones_over_time(daily): a line chart (ax.plot) of cones_sold against date, one line through all 365 days, with the dates tilted by fig.autofmt_xdate(). Title Cones sold each day, 2025, x-axis Date, y-axis Cones sold. For a three-day table with 12, 30 and 21 cones, the line's y values are 12, 30 and 21.
  2. flavor_bars(flavors): a bar chart (ax.bar) of scoops_sold by flavor, sorted largest first, the names turned on their side. Title Scoops sold by flavor, 2025, x-axis Flavor, y-axis Scoops sold. Worked through: Pistachio 900, Vanilla Bean 4000 and Strawberry 2500, sorted largest first, are Vanilla Bean 4000, Strawberry 2500 and Pistachio 900, so the bars' heights from left to right are 4000, 2500 and 900, with those names under them. The table you were given stays in menu order.
  3. temperature_vs_cones(daily): a scatter plot (ax.scatter) with high_temp_f along the x-axis and cones_sold up the y-axis, one dot a day, alpha=0.5. Title Hotter days sell more cones, x-axis High temperature (°F), y-axis Cones sold. Days of 50, 80 and 65 degrees that sold 40, 300 and 150 cones make dots at (50, 40), (80, 300) and (65, 150).
  4. order_totals_histogram(orders, bins): a histogram (ax.hist) of order_total, with bins passed straight on. Title Most orders are under $15, x-axis Order total ($), y-axis Number of orders. Orders of $4.25, $6.50, $12.00 and $14.75 with bins=[0, 5, 10, 15] make three bars of 1, 1 and 2 orders.

Don't call plt.show() or fig.savefig() inside the functions: report.py saves the charts.

Check your work

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

python -m pytest

At first all 16 tests fail. That's expected: each function you finish turns its tests green. Each test reads one thing from your chart (the title and labels, the number of lines, bars or dots, the values they show), and a failing test's message says what it read and what it expected, for example flavor_bars(df)'s bar heights for 900, 4000, 2500 should give [4000, 2500, 900], but it gave [900, 4000, 2500].

To see your charts, run python report.py and open the PNG files it saves in the charts folder.

Hints

  • Draw it in the notebook first. In explore.ipynb, a chart shows under the cell, so you can see what you're building before the tests read it. Figures and Axes: How matplotlib Draws has the three lines every chart starts with.
  • x first, then y. ax.plot, ax.bar and ax.scatter all take the values along the bottom first.
  • Sort, then draw. sort_values("scoops_sold", ascending=False) hands back a sorted copy and leaves flavors as it was, as Bar Charts: Comparing Categories shows.
  • alpha goes in the scatter call. Scatter Plots: Do Two Things Move Together? passes it to ax.scatter with the two columns.
  • bins is already what ax.hist wants. A number or a list of edges both work, as Histograms: How Values Are Spread shows; pass it on as it is.

When you're done

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

1) points on the line: 365
2) tallest bar: Vanilla Bean 8573
3) shortest bar: Pistachio 2030
4) dots on the scatter plot: 365
5) histogram bars: 10
6) orders in the tallest bar: 1425
7) charts saved in: charts

Now open charts/temperature-vs-cones.png: does the chart back up the title you were asked to give it? And in charts/order-totals.png, if the owner prices a family deal at $20, about how many of July's orders were that big or bigger?

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-creamery-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-creamery-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-creamery-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 chart function that returns fig and ax is a chart you can check: its title, its labels and every value it draws can be read back and tested, so the chart in the report is the chart you meant to make.

Key terms

Related