[26.1] You are provided a one-dimensional data set. Based on the data type (ordinal, numerical, categorical), what visualization would you use?
Solution:
To visualize numerical data, use a histogram.
To visualize ordinal data, use a bar chart.
To visualize categorical data, use a pie chart.
[26.2] You are provided a two-dimensional data set. Based on the data type (ordinal, numerical, categorical), what visualization would you use?
Solution:
To analyze numerical x numerical data, use a scatter plot.
To analyze numerical x ordinal/categorical data, use a bar chart for averages or a box-andwhiskers plot for ranges.
It is difficult to analyze ordinal/categorical x ordinal/categorical data visually; use a table instead.
[26.3] You are provided a three-dimensional data set. Based on the data type (ordinal, numerical, categorical), what visualization would you use?
Solution:
To analyze numerical x numerical x numerical data, use a bubble plot to compare all three together or a scatter plot matrix to compare all the pairs.
To analyze numerical x numerical x ordinal/categorical data, use a colored scatter plot.
[26.4] Fill in the blank for the type of chart that should be used with each scenario.
To analyze how the size of the product affects the cost of shipping, use a _______.
To analyze the weight of the animals that people own, by pet species use a _______ for averages or a __________ for ranges.
It is difficult to analyze pet species and their names visually. Use a ______ instead.
Solution:
To analyze how the size of the product affects the cost of shipping, use a scatter plot.
To analyze the weight of the animals that people own, by pet species, use a bar chart for averages or a box-and-whiskers plot for ranges.
IIt is difficult to analyze pet species and their names visually. Use a table instead.
[26.5] Fill in the blank for the type of chart that should be used with each scenario.
To analyze how many of each product type are in a data set, use a _______.
To analyze the proportion of people who have cats in a data use a _______ .
To analyze the distribution of grades in an exam use a _______ .
Solution:
To analyze how many of each product type are in a data set, use a bar chart.
To analyze the proportion of people who have cats in a data use a pie chart .
To analyze the distribution of grades in an exam use a histogram .
[26.6] Fill in the blank for each type of chart that should be used with each three-dimensional relationship.
To analyze numerical x numerical x numerical data, use a __________ to compare all three together or a __________ to compare all the pairs.
To analyze numerical x numerical x ordinal/categorical data, use a _____________.
Solution:
To analyze numerical x numerical x numerical data, use a bubble plot to compare all three together or a scatter plot matrix to compare all the pairs.
To analyze numerical x numerical x ordinal/categorical data, use a colored scatter plot.
[26.7] For each of the following prompts, determine the number of dimensions, determine the data type, then pick the best visualization to use based on the dimensions and data types.
Graph the relationship between students' study hours and their exam scores, grouped by whether they attended review sessions or not.
Show the distribution of daily step counts recorded by users of a fitness app.
Compare the salaries of employees across different departments in a company.
Visualize the relationship between temperature, ice cream sales, and whether the day is a weekday or weekend.
Compare the number of books checked out from a library by genre.
Solution:
Number of dimensions: 3
Data types: numerical, numerical, categorical
Visualization: colored scatter plot
Number of dimensions: 1
Data types: numerical
Visualization: histogram
Number of dimensions: 2
Data types: numerical, categorical
Visualization: box-and-whisker plot
Number of dimensions: 3
Data types: numerical, numerical, categorical
Visualization: colored scatter plot
Number of dimensions: 2
Data types: categorical, numerical
Visualization: bar chart
Code Writing
[26.8] In class we created the following code to find all the flavor categories in the dataset and create a list of all the #1 categories so we can find their counts easily.
data = readData("all-icecream.csv")
firstCol = data[0].index("#1 category")
numberOneData = []
flavors = []
for i in range(1, len(data)):
flavor = data[i][firstCol]
numberOneData.append(flavor)
if flavor not in flavors: # haven't seen this one yet
flavors.append(flavor)
counts = []
for flavor in flavors:
counts.append(numberOneData.count(flavor)) # get each flavor's count
Using the above example as a starting point, and the matplotlib documentation on creating a bar chart, create a visualization that shows the different types of ice cream flavors and their counts from the column "#1 category".
Solution:
import matplotlib.pyplot as plt
data = readData("all-icecream.csv")
firstCol = data[0].index("#1 category")
numberOneData = []
flavors = []
# collect the falvor categories from "#1 category"
for i in range(1, len(data)):
flavor = data[i][firstCol]
numberOneData.append(flavor)
if flavor not in flavors: # haven't seen this one yet
flavors.append(flavor)
# count how many times each flavor happens
counts = []
for flavor in flavors:
counts.append(numberOneData.count(flavor))
# now that we have the data, create bar chart
plt.bar(flavors, counts)
# optional improvements to the chart
plt.xlabel("Ice Cream Flavors")
plt.ylabel("Count")
plt.title("Favorite Ice Cream Flavors")
plt.xticks(rotation=45) # rotate labels if long
plt.tight_layout()
plt.show()
[26.9] Using the "all-icecream.csv" dataset, use matplotlib to visualize a bar chart of the number of people who chose chocolate ice-cream as their #1 choice, #2 choice, and #3 choice of flavors.
Solution:
import matplotlib.pyplot as plt
data = readData("all-icecream.csv")
choc_1 = 0
choc_2 = 0
choc_3 = 0
col1 = data[0].index("#1 category")
col2 = data[0].index("#2 category")
col3 = data[0].index("#3 category")
for i in range(1, len(data)):
if "chocolate" in data[i][col1]:
choc_1 += 1
if "chocolate" in data[i][col2]:
choc_2 += 1
if "chocolate" in data[i][col3]:
choc_3 += 1
labels = ["#1 Choice", "#2 Choice", "#3 Choice"]
counts = [choc_1, choc_2, choc_3]
plt.bar(labels, counts)
plt.ylabel("Number of People")
plt.title("Chocolate Preference by Rank")
plt.show()
[26.10] Using the code from the previous question, visualize the data so that each bar in the bar chart is a different color.
Solution:
import matplotlib.pyplot as plt
data = readData("all-icecream.csv")
choc_1 = 0
choc_2 = 0
choc_3 = 0
col1 = data[0].index("#1 category")
col2 = data[0].index("#2 category")
col3 = data[0].index("#3 category")
for i in range(1, len(data)):
if "chocolate" in data[i][col1]:
choc_1 += 1
if "chocolate" in data[i][col2]:
choc_2 += 1
if "chocolate" in data[i][col3]:
choc_3 += 1
labels = ["#1 Choice", "#2 Choice", "#3 Choice"]
counts = [choc_1, choc_2, choc_3]
colors = ["red", "green", "blue"]
plt.bar(labels, counts, color=colors)
plt.ylabel("Number of People")
plt.title("Chocolate Preference by Rank")
plt.show()