ME204 2026 Icon

💻 Week 02, Day 01 - Lab

Make a Chart That Makes Your Point

Author

Dr Jon Cardoso-Silva

Last updated

20 July 2026

🥅 Learning Objectives

By the end of this lab, you should be able to: i) Take a tidy table to a plotly.express chart by naming DataFrame columns in the call, ii) Build the table a chart needs: a new column from a date string, then a groupby on two columns, iii) Write a chart title that states a finding, iv) Walk a nested JSON structure by hand and rebuild the same table from it.

Monday, 20 July 2026 | Check your timetable for your class time 📍 Check your timetable for the location of your class

This morning’s lecture was about making a chart that makes your point. This afternoon you build one from data, then rebuild it from the messier file that data came from.

The two files you need are already under /files/data/.

🛣️ Lab Roadmap

Part Time What you do
Part I First 30 min Take a tidy CSV to a plotly.express line chart
Part II 30–60 min Rebuild the same chart starting from nested JSON
Part III Remaining time Midterm support

How today connects to your midterm. The JSON in Part II is shaped like a nested weather response, close to what you are working with in the midterm. The exact keys are different, of course, so you cannot copy today’s code into your project!

Before you start

Open the starter notebook ME204_W02D01_Lab.ipynb in VS Code on Nuvolos (/files/notebooks/). The two data files are already under /files/data/:

  • /files/data/city_temps.csv, a tidy table of daily temperatures for three cities across 2025.
  • /files/data/city_temps.json, the same data in a nested JSON layout like a weather API response.

This data came from OpenWeather, but I rearranged the JSON slightly so it is not the exact same preprocessing you do in your midterm.

If you are working on your own machine, download a copy here (unpack so notebooks/ and data/ are next to each other):

Part I: From a tidy table to a chart (30 min)

You start with a table that is already tidy and take it to this chart:

Monthly mean temperature for three cities across 2025. Hover a point for the exact value.

  • df has one row per city per day, but the chart needs one row per city per month. Build a month column from the date string, then group to collapse the days into monthly means. That grouped table, plot_df, is what the plotting cell draws.

  • The plotting cell is written for you and marked do not edit. As in this morning’s lecture, the plotly.express call names the columns it wants (x="month", y="temp", color="city"), and you need to hand it a DataFrame with those columns. If the cell errors, the fix is in the cells above it.

  • The last step is the title. Replace the placeholder with a sentence that states what the three lines show (your main key takeaway), not one that only labels the horizontal and vertical scales.

Part II: The same chart, from nested JSON (30–60 min)

That tidy CSV was built from a messier file. In Part II you open that file, /files/data/city_temps.json, and rebuild the same table from it.

  • The structure now is a dictionary of cities, and each city holds two parallel lists: the dates and the readings, lined up so the reading at one position belongs with the date at the same position. You explore it first with the tools from the W01D03 lab (type(), .keys(), indexing), then loop over the positions in those lists to collect one row per day. Once the table is tidy again, you reuse the same steps as Part I, and the same do not edit plotting cell produces the same chart.

Your class teacher can help with specific technical questions (“why is this groupby giving me an error?”, “how do I read this part of the JSON?”). They cannot evaluate your analytical choices (“is this title good enough?”, “which cities should I compare in my midterm?”). Those decisions are yours.

If you finish early. Near the end of the notebook there is a box titled “Ahead of the room? Read on.” Open it if you want a short look ahead. None of it is needed for today. Otherwise move into Part III and use the time on your midterm.

Part III: Midterm support (remaining time)

Use whatever time is left on your midterm. Your class teacher can help with technical blockers in NB02 or NB03. Analytical choices stay yours.

Bonus: show the spread, not just the mean

This morning’s lecture had a rule: do not hide the distribution behind a mean. Your Part I chart draws the monthly mean, so a steady city and a wildly swinging city can produce the same line. If you have time after Part II, rebuild the chart so it keeps the spread visible:

Median daytime temperature per city per month, with a bar from the 25th to the 75th percentile. Hover a point for the median and the quartile range.

🤔 Think about it: Is this chart clear enough? Or did the error bars and spreading the cities side by side within each month make it too confusing?

Each point is the median daytime temperature for that city and month, and the bar runs from the 25th to the 75th percentile, the middle half of the days. Rebuild it from the same df: group by city and month, compute the median and the two quartiles, work out how far the bar extends above and below the median, and attach the bars with the error_y arguments in plotly.express. The notebook’s bonus cell lists the steps.

After the lab

A groupby on two columns and walking a nested structure into a table are both on the midterm, so keep this notebook to hand while you build NB02 and NB03.

💬 Questions after class go to the Discussion Forum on Moodle.