Skip to content
Tutorials Center
My learning

Topics

Lesson 53 of 57 ยท Projects

Project: Visualizing a Pandemic Time Series

Visualize daily case counts for several countries with Matplotlib and pandas: weekly reporting patterns, 7-day rolling averages, cumulative totals, per-capita and log-scale charts.

When an outbreak is in the news, the same few charts appear everywhere: daily new cases, a smoothed line through them, a running total, and a comparison between countries. They look simple, but every one of them hides a decision that can mislead: Should the lines be smoothed? Should the countries be compared in absolute numbers or per person? Is the y axis linear or logarithmic? In this project you will build these charts yourself and see how each choice changes the story.

The data is synthetic: four fictional countries (Avalon, Borealia, Candor and Dunmere) whose daily reported cases come from a simple epidemic model with random noise, a weekly reporting rhythm and two waves. It is not real health data and says nothing about any real disease or place. Because we generate it with a fixed random seed, everyone gets the same numbers, and the techniques apply unchanged to a real CSV file of daily counts.

You will practise pivot, rolling, cumsum and per-capita scaling with pandas, then multi-line and multi-panel charts, date axes, annotations and log scales with Matplotlib. See Dates and Time Series, Rolling Windows and Annotations if you want to refresh any of them.

flowchart LR
    A["1. Load<br/>long table"] --> B["2. Reshape<br/>pivot to wide"]
    B --> C["3. Smooth<br/>7-day mean"]
    C --> D["4. Accumulate<br/>cumsum, per 100k"]
    D --> E["5. Compare<br/>lines, log scale"]
    E --> F["6. One-page summary"]

Each step turns raw daily numbers into a chart that is easier to read and harder to misuse.

Step 1: Look at the raw table

Surveillance data usually arrives in long format: one row per country per day. The setup code at the top (you can skim it) builds that table. Then we reshape it to wide format (one column per country) with pivot, which is the shape that makes time-series plotting and rolling averages easy.

Python Step 1: long table, wide table and the weekly rhythm

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The weekday table shows the first important pattern: Saturday and Sunday are lower, not because fewer people fall ill at the weekend, but because fewer cases are reported. Raw daily numbers therefore zigzag with a seven-day rhythm, and that noise can look like trends.

Step 2: Smooth with a 7-day rolling average

Averaging over exactly seven days gives one full week in every window, so the weekday pattern cancels out. In pandas this is wide.rolling(7).mean(): each day becomes the mean of itself and the six days before it. The first six values are NaN because a full window does not exist yet.

Python Step 2: raw bars and the 7-day average

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The red line shows the shape of the outbreak (a first wave in the spring and a second wave rising in the autumn); the pale bars show how noisy single days are. Remember two properties of a trailing average. It always lags the real turning points by about three days (the window looks backwards), and it understates the highest single days. If you prefer a line centred on each day, use rolling(7, center=True), but that cannot be computed for the most recent days, which are the ones people care about.

Step 3: Cumulative totals and per-capita numbers

A cumulative chart shows the running total: cumsum() adds up the daily numbers. It can only go up, so the thing to read is its slope, not its height: a steep line means many new cases. Totals also depend on country size, so we divide by the population to get cases per 100,000 people, which puts a small country and a large country on the same scale.

Python Step 3: totals, absolute and per 100,000

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The left chart says that Candor has by far the most cases, Avalon comes second, and Dunmere and Borealia look like small outbreaks. The right chart says something completely different: Dunmere's burden per person is almost as high as Candor's (about 2,400 per 100,000 each), Avalon is not far behind, and Borealia is far lower (about 120). Candor is simply a big country (25 million people against 1.5 million). Neither chart is wrong; they answer different questions. When you compare places of different sizes, show the per-capita version, or both.

Step 4: Comparing countries

For the comparison itself we draw all four 7-day averages per 100,000 in one chart with the same style for every line. We annotate the peak of each country directly (a label on the line is easier to read than a legend that the eye has to match). Then we add a second chart of the same data on a logarithmic y axis.

Python Step 4: four countries, linear and log scale

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice also that for three of the four countries the highest point is the last day of the data: the second wave has not peaked yet, so the labels show the highest value so far, not a final peak. The two panels use the same numbers and tell different stories:

  • On the linear axis, the largest wave dwarfs everything else, and Borealia looks like a flat line at zero. That is fair if you care about how many cases there are.
  • On the log axis, every line is visible, and a straight line going up means constant exponential growth. You can compare the speed of growth between countries and spot where measures bent the curve. The cost is that big differences look small, and many readers misjudge log charts, so label the axis clearly and tell the audience.

Honest charts: The choice of linear or log, of absolute or per-capita, and of the start date can make the same data look alarming or calm. Say which you chose, in the title or axis label, and do not change the scale to support a conclusion you already have.

Step 5: A one-page summary

Finally, combine the best views in one figure: smoothed daily cases (per 100,000), the cumulative curve, the weekly pattern and a bar chart of each country's peak. We use layout="constrained" for the spacing and save the figure to an in-memory buffer, exactly as you would write a PNG for a report (see Saving Figures).

Python Step 5: a four-panel summary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A good summary page has one message per panel, a title that states what is shown (including the unit and the smoothing), consistent colours for each country in every panel, and a note that the data is synthetic. In a real report add the data source and the date of the last data point.

Pitfalls worth remembering

  • Reporting artefacts. Weekly rhythms, holidays and batch uploads create spikes and dips. Smooth with a window of exactly 7 days, not 5 or 10, so weekdays cancel out.
  • Corrections and negative values. Real data sometimes has a day with a negative count when a country revises its totals. Check with (wide < 0).sum() and decide how to treat them before taking a rolling mean.
  • Different start dates. Countries do not start at the same time. To compare the shape of the waves, align each series on the day it crosses a threshold, such as 1 case per 100,000.
  • The latest days. A trailing average ends on the last date, but late reports may still change the most recent values. Do not over-interpret the last few points.
  • Cumulative charts hide recovery. A running total never falls, so a cumulative chart cannot show that things are getting better. Use daily or weekly numbers for that.

Extension ideas

  • Align the countries on the day each one reached 1 case per 100,000 and plot all four on a shared "days since" axis.
  • Compute the weekly growth rate (avg7.pct_change(7)) and shade the periods when it is above zero.
  • Add a second y axis (ax.twinx()) for the cumulative total on the daily chart.
  • Draw one small chart per country with sharex=True but free y axes, and compare with a shared y axis.
  • Export the summary figure as a PDF page with PdfPages (see Saving Figures).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why use a 7-day rolling average instead of a 5-day one for this data?

    With a 7-day window each average includes one Monday, one Tuesday and so on, so the weekend dip no longer causes a zigzag.
  2. 2. What does df.pivot(index='date', columns='country', values='new_cases') do?

    pivot reshapes long data (one row per date and country) into a wide table with a column for each country, which is convenient for plotting and rolling means.
  3. 3. Why is a cumulative chart unsuitable for showing that an outbreak is receding?

    The cumulative curve can only rise or stay flat. A receding outbreak shows up as a flattening slope, which is harder to see than a falling daily line.
  4. 4. What does a straight rising line on a logarithmic y axis mean?

    On a log axis equal vertical steps represent equal ratios, so a straight line means the quantity multiplies by the same factor every day.
  5. 5. Why divide by population before comparing countries?

    Absolute counts grow with population. Cases per 100,000 people show how heavily each country is affected relative to its size.

Summary

  • Time-series data often arrives long (date, country, value); pivot makes it wide for plotting and rolling windows.
  • Daily reports have a weekly rhythm; a 7-day rolling mean (rolling(7).mean()) removes it but lags by about three days.
  • cumsum() gives running totals; read the slope, and divide by population (per 100,000) to compare places of different sizes.
  • Linear and log axes answer different questions: linear shows size, log shows growth rate. Label which one you use.
  • Annotate peaks and use consistent colours; combine views in one constrained-layout figure and save it with savefig.
  • Check for revisions, different start dates and unreliable latest days, and never present synthetic data as real.

Next: Project: Analysing Used Car Prices uses scatter plots and box plots to find what drives a price.