THINK FIRST·CODE LATER

← All labs

Overplotting and 2D binning

Problem

Input: a line grid NX NY, then a CSV x,y.

Compute a 2D histogram with np.histogram2d(x, y, bins=[NX, NY]) and print 11 points, x [0, 8], y [0, 9], grid 4x3, then the counts as a grid with the top row (largest y) first, each count right-aligned in 3 characters and separated by spaces, then densest cell: x [0, 2), y [0, 3) with 4 points (36%) (the first maximum in x-then-y order), empty cells: 7 of 12, and suggested idiom for this size: … — plain scatter plot (maybe with transparency) for fewer than 1,000 points, scatter with transparency, smaller marks or jitter up to 10,000, otherwise 2D binning (heat map / hexbin) or datashader.

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.

v11-1-data-1.txt

grid 4 3
x,y
0,0
1,1
1,1
1,2
2,1
3,3
4,6
4,6
4,6
4,6
8,9
What your program must print for v11-1-data-1.txt
11 points, x [0, 8], y [0, 9], grid 4x3
  0   0   4   1
  0   1   0   0
  4   1   0   0
densest cell: x [0, 2), y [0, 3) with 4 points (36%)
empty cells: 7 of 12
suggested idiom for this size: plain scatter plot (maybe with transparency)

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

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

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