This project demonstrates the concept of pixels using a 2D grid of colored squares. It also demonstrates data visualization using a heat map. Students are given a 2D array (or they can create or import one). They use two nested loops (one for x and one for y) to iterate through the array. They then use a set of if-elif-else statements to set the color of each pixel based on the value of that data point.

my_data = [ [62,64,66,68,65,66,67,71,73,74,76,78,81,82,84], [67,67,65,63,65,64,68,71,75,77,77,81,84,86,85], [66,64,65,65,63,66,66,71,74,79,82,83,84,83,84], [64,65,69,64,65,68,71,73,76,78,81,82,82,84,84], [66,63,65,65,68,71,73,75,77,81,82,82,83,83,84], [66,66,65,65,66,71,75,78,79,82,81,83,86,84,85], [65,64,65,68,71,74,77,82,83,83,84,81,83,84,82], [65,66,65,66,71,73,78,81,82,80,83,84,82,85,81], [64,64,67,71,76,75,76,79,83,84,82,83,84,83,82], [66,65,69,71,72,76,78,81,82,83,85,84,83,82,83], [65,68,71,75,72,76,81,82,83,86,84,85,84,83,81], [66,71,76,77,75,80,82,83,85,84,84,84,82,83,79], [71,74,75,78,82,83,83,85,86,85,83,82,83,84,82], [73,76,78,81,81,84,84,83,84,83,84,85,83,82,83], [75,77,76,75,82,82,84,81,83,85,84,83,86,82,86], [78,77,76,81,81,83,83,82,81,80,83,81,83,84,85], ] # Determine the size of each "pixel" size = 500./len(my_data) # Set the initial y-location to the top of the canvas y = 250-size/2. # Iterate (loop) through the rows in the list for row in my_data: # Set the initial x-location to the left side # Must be done for each row x = -250+size/2. # Iterate (loop) through the data points in each row for value in row: # Set the pixel color based on the value of the data if value <= 65: color = "blue" elif value <= 70: color = "green" elif value <= 75: color = "yellow" elif value <= 80: color = "orange" elif value <= 85: color = "red" else: color = "purple" # Create a square for each "pixel" pixel = codesters.Square(x, y, size, color, "grey") # Move right 1 square for the next data point x += size # The wait command is optional, # but it helps students to see the process stage.wait(.01) # Move down 1 square for the next row y -= size
Support