THINK FIRST·CODE LATER

← All labs

Brushing and linking: predict the linked view

Problem

Input: a line brush FROM TO (dates), then a CSV date,district,pm25.

Build in Altair: an interval selection named brush on the x encoding, added to a line chart (x = date, y = pm25, colour = district); and a bar chart of mean pm25 per district filtered by the brush; combine them with &.

From the specification print selection 'brush': interval on ['x'], defined in ['view_1'] and, for each view of the vconcat, view 1 (line): defines the brush or view 2 (bar): filtered by brush. Then simulate the interaction with pandas: brushed 2026-03-02..2026-03-03: 6 of 12 rows, bars: Longwan 28.0, Lucheng 58.0, Ouhai 39.0 (means of the brushed rows, alphabetical) and without brush: … (means of all rows).

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

v8-1-data-1.txt

brush 2026-03-02 2026-03-03
date,district,pm25
2026-03-01,Lucheng,42
2026-03-01,Ouhai,35
2026-03-01,Longwan,28
2026-03-02,Lucheng,55
2026-03-02,Ouhai,40
2026-03-02,Longwan,31
2026-03-03,Lucheng,61
2026-03-03,Ouhai,38
2026-03-03,Longwan,25
2026-03-04,Lucheng,48
2026-03-04,Ouhai,39
2026-03-04,Longwan,30
What your program must print for v8-1-data-1.txt
selection 'brush': interval on ['x'], defined in ['view_1']
view 1 (line): defines the brush
view 2 (bar): filtered by brush
brushed 2026-03-02..2026-03-03: 6 of 12 rows
bars: Longwan 28.0, Lucheng 58.0, Ouhai 39.0
without brush: Longwan 28.5, Lucheng 51.5, Ouhai 38.0

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), in the folder that holds both files:

python my_lab.py < v8-1-data-1.txt

Windows PowerShell (the default terminal of VS Code on Windows) does not understand <:

Get-Content v8-1-data-1.txt | python my_lab.py

PyCharm: Run → Edit Configurations → tick Redirect input from and choose the file.

Jupyter: put this at the top of the cell that holds your program:

import sys
sys.stdin = open("v8-1-data-1.txt", encoding="utf-8")

Write it here, in Jupyter or in your IDE, then paste it. Run it on the sample input yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.