THINK FIRST·CODE LATER

← All labs

A scatter plot from scratch: scales and marks in text

Problem

To understand what every plotting library does internally, draw a scatter plot as text.

Input: first line W H (grid width and height in characters), then a CSV with columns x,y.

  • Map each point with linear scales: column = ⌊(x − xmin)/(xmax − xmin) × (W − 1) + 0.5⌋, row = ⌊(y − ymin)/(ymax − ymin) × (H − 1) + 0.5⌋ (round half up; if max = min the index is 0). Row 0 is the bottom.
  • A cell with one point shows *, with 2–9 points the count, with more 9; empty cells are spaces.
  • Print each row from top to bottom as | followed by the cells, without trailing spaces; then + followed by W dashes; then x: [4, 14] y: [3.1, 9.26] points: 11 (values in Python's :g format).

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.

v1-4-data-1.txt

21 8
x,y
10,9.14
8,8.14
13,8.74
9,8.77
11,9.26
14,8.1
6,6.13
4,3.1
12,9.13
7,7.26
5,4.74
What your program must print for v1-4-data-1.txt
|            * * *
|        * *       * *
|      *
|
|    *
|  *
|
|*
+---------------------
x: [4, 14]  y: [3.1, 9.26]  points: 11

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 < v1-4-data-1.txt

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

Get-Content v1-4-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("v1-4-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.