At My Fingertips
Rapid Playground
In this exercise you will visualize an aspect of the "animals dataset" of the "Bootstrap:DataScience" Curriculum.
The "Animals Dataset" is a table (here the Google Sheet provided by the Bootstrap team) that contains a row for each pet in a school class. It contains 32 animals in total; cats, dogs, and even a snake.
Name | Species | Sex | Age (years) | Fixed | Legs | Weight (lbs) | Time to Adoption (weeks) |
---|---|---|---|---|---|---|---|
Sasha | cat | female | 1.0 | False | 4 | 6.5 | 3 |
Snuffles | rabbit | female | 3.0 | True | 4 | 3.5 | 8 |
Mittens | cat | female | 2.0 | True | 4 | 7.4 | 1 |
Sunflower | cat | female | 5.0 | True | 4 | 8.1 | 6 |
Felix | cat | male | 16.0 | True | 4 | 9.2 | 5 |
Sheba | cat | female | 7.0 | True | 4 | 8.4 | 6 |
Billie | snail | hermaphrodite | 0.5 | False | 0 | 0.1 | 3 |
Snowcone | cat | female | 2.0 | True | 4 | 6.5 | 5 |
Wade | cat | male | 1.0 | False | 4 | 3.2 | 1 |
Hercules | cat | male | 3.0 | False | 4 | 13.4 | 2 |
Toggle | dog | female | 3.0 | True | 4 | 48.0 | 1 |
Boo-boo | dog | male | 11.0 | True | 4 | 123.0 | 24 |
Fritz | dog | male | 4.0 | True | 4 | 92.0 | 3 |
Midnight | dog | female | 5.0 | False | 4 | 112.0 | 4 |
Rex | dog | male | 1.0 | False | 4 | 28.9 | 9 |
Gir | dog | male | 8.0 | False | 4 | 88.0 | 5 |
Max | dog | male | 3.0 | False | 4 | 52.8 | 8 |
Nori | dog | female | 3.0 | True | 4 | 35.3 | 1 |
Mr. Peanutbutter | dog | male | 10.0 | False | 4 | 161.0 | 6 |
Lucky | dog | male | 3.0 | True | 3 | 45.4 | 9 |
Kujo | dog | male | 8.0 | False | 4 | 172.0 | 30 |
Buddy | lizard | male | 2.0 | False | 4 | 0.3 | 3 |
Gila | lizard | female | 3.0 | True | 4 | 1.2 | 4 |
Bo | dog | male | 8.0 | True | 4 | 76.1 | 10 |
Nibblet | rabbit | male | 6.0 | False | 4 | 4.3 | 2 |
Snuggles | tarantula | female | 2.0 | False | 8 | 0.1 | 1 |
Daisy | dog | female | 5.0 | True | 4 | 68.0 | 8 |
Ada | dog | female | 2.0 | True | 4 | 32.0 | 3 |
Miaulis | cat | male | 7.0 | False | 4 | 8.8 | 4 |
Heathcliff | cat | male | 1.0 | True | 4 | 2.1 | 2 |
Tinkles | cat | female | 1.0 | True | 4 | 1.7 | 3 |
Maple | dog | female | 3.0 | True | 4 | 51.6 | 4 |
This activity comes with a table as a CSV-File (Comma-Separated Values), animals.csv
.
This is a text file.
Each line in the table is stored as a line of text,
and the different columns are separated with a comma.
Here is the header-row (which contains the column names, "Name", "Species", ...) and the three first rows of data of the table in CSV-format:
Name,Species,Sex,Age (years),Fixed,Legs,Weight (lbs),Time to Adoption (weeks)
Sasha,cat,female,1,FALSE,4,6.5,3
Snuffles,rabbit,female,3,TRUE,4,3.5,8
Mittens,cat,female,2,TRUE,4,7.4,1
In Python we could read such a text file into a string, then split the string into lines (at the "newline" characters), and the lines into cells (at the commas).
However, doing this correctly is not trivial.
Fortunately, Python's csv
library already does this for us.
It includes, amongst other things, a "reader" (DictReader
),
that reads the file and turns each line (every animal) into a "dictionary" (dict
).
The dictionary contains for each column (for each attribute of the animal) an entry,
which one can access with line["Column Name"]
.
Now let's create a chart to represent the name, the sex, and the number of legs of each animal.
Here are our requirements:
Let's first implement a function that, given an animal, produces the color (according to the sex of the animal).
Remember that the animal is a dictioary, and you can use animal[...]
to access an entry.
We are interested about the "Sex"
entry of the animal.
But what are the possible sexes of an animal? We could open the CSV file in an editor and read it all to find out. Or we could write a short program to produce the set of all values of the Sex entries. Let's write that program!
Now let's implement our function! Let's color females red, males blue, and hermaphrodites magenta.
Now let's test your function. For this we create three test "animals" by creating three dictionaries. We only put the relevant entry--the sex--in each dictionary.
Then we print that sex entry and the computed color of each test animal.
Our sex_color
function returns a value of type Color
;
we can print such values and will see some internal information about them.
Now let's implement a bar function that takes an animal,
extracts its leg count,
and produces a Graphic
that consists of as many circles beside each other as the animal has legs.
The circles should be colored according to the animal's sex
(use your color
function to determine the color).
The diameter of the circles should correspond to the height of a bar.
You should see three leg bars, one with three blue legs, one with five red legs, and one with two magenta legs.
Now let's write a function that produces the label. The text should have a font size of 75% the bar height. Use "Roboto" as the font. The text should have the color corresponding to the animal's sex.
You should see the word "pumpkin" in red.
Now let's put everything together.
The animal_leg_chart
function receives the name of a file and the height of one bar.
It needs to open the file, create a DictReader
,
then iterate through the rows.
Note that the labels need to be right-aligned and the leg bars need to be left-aligned. There are several ways to make this possible.
One way is to pad each label and each leg bar by placing it on a background rectangle that has the width of the widest label or leg bar. For this, first produce all the labels and all the leg bars, and store them in lists. Then compute the maximum width of all labels, and the maximum width of all leg bars. Finally, iterate over the lists, and create padded versions of the labels and of the leg bars: create transparent (or white) rectangles with the maximum width, pin them to the right or left center, and compose them with the label or the leg bar, also pinned to the right or left center. Then place the padded label and the padded leg bar beside each other (and place a small gap between them). Do all of this in a loop, which accumulates the overall graphic, starting with an empty graphic, and adding one bar in each iteration.
Another way is by stacking all the labels in a right-aligned fashion, and stacking all the leg bars in a left-aligned fashion, and then placing them beside to each other (and place a small gap between them).
In this activity you learned how to read a CSV file, how to retrieve specific entries from each row, and how to turn this information into a visualization.
This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.
Animal Leg Chart
PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI
Privacy Policy • Platform Version 0d7866a (Fri, 20 Dec 2024 07:46:38 GMT)