Charts with matplotlib · lesson 11 of 11
Parts in this lesson
Aplt.style.use("house.mplstyle")6.10: one style for every chart
Bax.bar_label(bars, fmt=...)6.6: numbers on the bars
Cplt.subplots(1, 4, sharey=True)6.5: small multiples on one scale
Dfig.savefig(folder / f"{name}.svg")6.10: vector files for the report

Project: the Tindahan chart pack

Last updated

The store owners have the clean Tindahan data from Stage 5 and six questions about it. In this project you'll answer each question with one clear chart: the right type, a title that states the finding, the house style, and alt text written from the data. Then you'll move the six charts into a module that rebuilds the whole pack, figures and descriptions, with one call.

By the end of this lesson you can

  • Choose a chart type for each of six questions, and write each title as its answer
  • Draw every chart in one house style, and save it as SVG in reports/figures
  • Write alt text from the data, so it can never go out of date
  • Rebuild the whole chart pack from a module with one function call

Plan it

Project 5.P left four clean files in data/clean/: sales, stores, products and weather for January and February 2024. The owners want a short pack of charts for their monthly meeting, one per question:

#QuestionJob (6.4)Chart
1How did sales move over the two months?changeline, with a 7-day average (6.9)
2Which store brings in the most money?comparisonsorted sideways bars, labelled in pesos (6.6)
3Which day of the week is busiest, at each store?comparison, by groupsmall multiples on one scale (6.5)
4What share of the money does each item bring in?compositionbars of shares, not a pie (6.4)
5Do rainy days sell less?relationshipscatter plot (6.3)
6How much do daily sales vary at each store?distributionbox plots (6.3)

The rules for every chart, from this stage:

  • One question per chart, and the title is the answer, written after looking at the chart (6.6).
  • The house style from lesson 6.10, so the six look like one pack: grey for context, pink for the point (6.7).
  • Honest scales: bars from zero, shared axes for small multiples (6.5, 6.8).
  • Saved as SVG in reports/figures/, with alt text that says the finding and the numbers (6.10).

Each chart is a function that takes the tables and returns the figure and its alt text. The alt text is built from the same numbers the chart draws, so if the data changes, the description changes with it.

Look at the data

As always, look first. The setup cell loads the four files, applies the house style, works out each sale's pesos (kilos times the price per kilo), and puts the tables in one dictionary, t, which every chart function takes. It also defines save, which writes a figure to reports/figures/. Run both cells.

06-charts.ipynb

720 rows from 1 January to 29 February: 60 days, 4 stores, 3 items, as project 5.P made them. Batangas sold the most, in kilos and in pesos.

Charts 1 to 3

Run the setup cell, then each chart. Each cell defines the chart's function, draws it, saves it, and prints its alt text.

06-charts.ipynb
  • Chart 1 keeps the raw days thin and grey and the 7-day average thick and pink, both from the house style's colour cycle. One annotation names the average, so no legend is needed. The average stays between 171 and 183 kg: no rise or fall over the two months, which is itself a finding, and the title says so.
  • Chart 2 sorts the stores, highlights the top one, and labels each bar in pesos with bar_label, so the x-axis can go. set_xlim(0, pesos.max() * 1.25) leaves room for the labels. Batangas took in ₱189,308, Rosario ₱94,683: twice as much.
  • Chart 3 draws four small charts with sharey=True, Saturday in pink in each. The bars are placed at the numbers 0 to 6 and labelled M T W T F S S with set_xticks: as labels, the two Ts and two Ss would be merged into one bar each. fig.suptitle puts one title over all four. Every store's busiest day is Saturday.

Charts 4 to 6

The same pattern, for the last three. Run the cells.

06-charts.ipynb
  • Chart 4 is composition with three parts, so a pie would just about work; bars still read better, and they match the rest of the pack. Rice brings in 67% of the money, sugar 22%, oil 11%: two pesos of every three.
  • Chart 5 compares stores of very different sizes, so it doesn't plot raw kilos (Batangas would sit on top of everything, as in lesson 6.3). It plots each day as a percentage of that store's average day (transform, 4.2), with a line at 100%. On that fairer scale the pattern is clearer than in lesson 6.3: correlation −0.33. The title says "a little less" and no more; Stage 8 is where you'll learn how sure you can be.
  • Chart 6 is the box plot from lesson 6.3, with a title that says what it shows. Median days: Batangas 58 kg, Rosario 30 kg.

Read each printed alt text against its chart. Does it say what a sighted reader would take away?

The pack, from one call

The six functions are now in src/charts.py, with one more, make_all, which draws every chart, saves it, and writes reports/figures/README.md: each chart as a Markdown image with its alt text. Run the cells.

06-charts.ipynb
  • make_all(t) loops over a dictionary of chart names and functions, CHARTS. Adding a seventh chart means writing one function and adding one line to it.
  • plt.close(fig) after each save stops the notebook drawing all six: in a script, you want the files, not the pictures.
  • The README is what GitHub shows when someone opens the folder, so the pack and its descriptions can be read there directly. Open it in a Markdown preview on your computer to see the charts with their alt text.

When March's data arrives, rerun the cleaning (5.P) and then make_all: six new charts, six new descriptions with March's numbers. Only the titles need checking by a person, because a title states a finding, and findings can change.

Finished

Run the notebook in step 5 again from the top: it should list six SVG files and a README, and print six alt texts with the numbers from steps 3 and 4.

Look at what you used:

  • A chart for each job (6.3, 6.4): a line, sorted bars, small multiples, bars of shares, a scatter plot and box plots.
  • Clear charts (6.6): titles that state findings, labels on bars, one annotation, no clutter.
  • Colour on purpose (6.7): grey for context, pink for the point.
  • Honest scales (6.5, 6.8): bars from zero, small multiples on one scale, sales as a percentage of each store's usual day.
  • Dates (6.9): a rolling average and short date labels.
  • A house style, SVG files and alt text (6.10).

The Tindahan project now runs from raw files to clean tables to report charts, with nothing done by hand. In Stage 7 you'll draw statistical charts faster with seaborn, and make charts the owners can explore themselves.

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis