๐ป Week 05 Lab
Matplotlib, Seaborn & Your First Visualisations
By the end of this lab, you should be able to: i) Set up matplotlib and seaborn with proper imports and configuration, ii) Understand the role of figure and axes objects in matplotlib, iii) Create bar plots, line charts, and scatter plots with seaborn, iv) Apply the plot_df pattern and write narrative titles, v) Export plots as PNG and SVG files.
This lab builds on ๐ฅ๏ธ W05 Lecture where you learned custom functions, .apply(), .groupby(), and pandas Styler. Today youโll learn the other presentation path for your Mini-Project 1: creating plots with matplotlib and seaborn.
๐ Preparation
- Attend or at least watch the ๐ฅ๏ธ W05 Lecture
- Find a partner for pair programming. Youโll swap roles for Task 03 (your class teacher will mediate this)
๐ฃ๏ธ Lab Roadmap
| Task | Activity Type | Focus | Time | Outcome |
|---|---|---|---|---|
| Task 01 | ๐ฃ๏ธ TEACHING MOMENT | Matplotlib Scaffolding | 20 min | Imports, config, figure/axes understanding |
| Task 02 | ๐ฃ๏ธ TEACHING MOMENT | Seaborn Bar Plot | 10 min | Follow-along: the plot_df pattern |
| Task 03 | ๐ฏ ACTION POINTS | Pilot/Copilot Plots | 40 min | Line chart + scatter plot + narrative titles |
| Task 04 | ๐ฏ ACTION POINTS | Challenge Plot | 20 min | London vs Paris comparison + explanation |
| Wrap-Up | ๐ฃ๏ธ TEACHING MOMENT | MP1 Connection | 10 min | Connect lab skills to NB03 |
๐ก Todayโs Design: Tasks 01-02 build mechanical comfort with the plotting infrastructure. Tasks 03-04 are pair programming exercises where you apply everything together.
Task 01: Matplotlib Scaffolding (20 min)
Note to class teachers: Students work through this individually. The goal is mechanical comfort with the plotting infrastructure so Task 02 and 03 feel smooth. Circulate to check imports are working.
Setup for Today:
Check that you can see the notebook and data file
These should be in your Nuvolos folder already. If not, use the buttons below to download the materials and save them to
/files/week05/on Nuvolos.
๐ Optional: If you have been following my suggestions from the W03 Lecture
If you have been following my suggestions from the W03 Lecture, you should already have a my-ds105-notes repository!
You can copy these files there for backup:
# Copy notebook and data to your Git repository
cp /files/week05/W05-NB02-Lab.ipynb /files/my-ds105-notes/week05/
cp /files/week05/data/london_paris_weather.csv /files/my-ds105-notes/week05/data/Then work from /files/my-ds105-notes/week05/ instead. This way your work is version-controlled!
- Open the notebook and work through the Task 01 cells
Youโll learn:
- Installation check: Verify matplotlib and seaborn are available
- The import block:
import matplotlib.pyplot as pltandimport seaborn as sns - SVG configuration: Crisp rendering with
%config InlineBackend.figure_formats = ['svg'] plt.figure(): Creating a canvas with configurable sizeplt.tight_layout(): Preventing label clippingplt.savefig(): Exporting plots as PNG and SVG files
๐ฏ ACTION POINTS
- Run the installation check cell
- Run the import and config cells
- Try changing
figsizeto(4, 4)and(12, 3)to see how it affects the canvas - Save a test plot as both PNG and SVG. Open them in your file browser and compare zoom quality
Task 02: Seaborn Bar Plot (10 min)
Note to class teachers: This is a follow-along teaching moment. Walk through the
plot_dfpattern step by step. Emphasise: prepare data first, inspect it, then plot. If students rush tosns.barplot()without preparingplot_dffirst, slow them down.
Follow along as we build a bar plot using the london_paris_weather.csv dataset. Every seaborn plot follows the same workflow:
- Prepare a
plot_dfwith exactly the columns you need - Inspect it with
print(plot_df) - Plot with seaborn
- Title with a narrative that states your finding
- Save to
figures/
The notebook walks through each step for a bar plot of Londonโs monthly rainfall. After completing it, review the Seaborn Quick Reference table before moving on.
Task 03: Pilot/Copilot Plots (40 min)
Note to class teachers: Students swap roles and work through two plots independently. The notebook provides hints but not complete code. Circulate and check that pairs are discussing before coding, and that narrative titles state findings rather than describe the chart.
โ ๏ธ SWAP ROLES! The person who was NOT typing becomes the ๐งโโ๏ธ Pilot.
Apply the same workflow you just saw. The notebook provides hints for each plot.
๐ฏ ACTION POINTS
๐งโโ๏ธ Pilot Plot A: Line chart of Londonโs yearly temperature trend. Write a narrative title and save to
figures/.๐งโโ๏ธ Pilot Plot B: Scatter plot of temperature vs rainfall. What pattern do you see? Write a narrative title and save to
figures/.๐ฅ Together Discuss: For each plot, does your title state a finding or just describe the chart?
Task 04: Challenge Plot (20 min)
Note to class teachers: Students stay in their pairs. The key teaching moment is the choice of plot type and what to aggregate. Push students to discuss before coding.
Still in your pairs, create ONE visualisation that answers:
โHow do London and Paris compare in their seasonal temperature patterns?โ
๐ฏ ACTION POINTS
๐ Copilot Guide what to aggregate and which plot type to use (discuss before coding!)
๐งโโ๏ธ Pilot Prepare
plot_df, create the plot with a narrative title, export as PNG and SVG tofigures/๐ฅ Together Write 2-3 sentences explaining what plot type you chose, why, and what your title communicates
Optional stretch goal: If you finish early, try using seabornโs FacetGrid to create side-by-side plots. Attempt it based on the docs alone before consulting other sources.
Wrap-Up & Next Steps (10 min)
Note to class teachers: Connect todayโs skills to MP1. Remind students of the Reading Week timeline. Emphasise that students now have TWO paths for their NB03 insights.
Class Discussion:
You now have two paths for Mini-Project 1 NB03 insights:
- Styled DataFrames (learned in yesterdayโs lecture):
.style.format(),.background_gradient(),.set_caption() - Seaborn plots (learned today):
sns.barplot(),sns.lineplot(),sns.scatterplot() - You can mix: one styled DataFrame + one seaborn plot
Looking Ahead:
- โ๏ธ Mini-Project 1 deadline: Thursday 26 Feb at 8pm
- Your loop-based NB02 code should now be translated to vectorised operations (using
.apply()and.groupby()from yesterdayโs lecture) - Reading Week drop-in sessions available (check โ Contact Hours)
- Your loop-based NB02 code should now be translated to vectorised operations (using
- Week 06: Reading Week, dedicated time to complete Mini-Project 1
๐ Useful Resources
๐ Essential Guides
- 3๏ธโฃ Data Science Workflow: Complete workflow stages
- 4๏ธโฃ Git & GitHub Guide: Version control commands
๐ป Course Materials
- ๐ฅ๏ธ W05 Lecture: Custom functions, groupby, pandas Styler
- ๐ฅ๏ธ W04 Lecture: Vectorisation with NumPy and Pandas
- โ๏ธ Mini-Project 1: Your first assessed project (due Week 06)
๐ Getting Help
- Slack: Post questions to
#helpchannel - Office Hours: Book via StudentHub
- Check staff availability on โ Contact Hours
๐ External Resources
- Seaborn Tutorial: Official seaborn documentation
- Matplotlib Pyplot: Matplotlib reference
- Pandas Styler: Styling DataFrames
