''' Honors Series - Data Science Mini-Assignment #3 sample solutions ''' import matplotlib.pyplot as plt import csv import random SEX = 6 NAME = 1 ATTACK = 11 AGE = 5 def main(): # 1A - Plot a simple line chart, where the numbers come from foreclosure data fores = [11, 10, 6, 8, 4, 13, 11, 8, 6, 10, 3, 4, 10, 10, 6, 2, 6, 8, 10, 11, 14, 6, 10, 6] plt.plot(fores) plt.xlabel('Month') plt.ylabel('Number of Foreclosures') plt.title('Foreclosures in Boston 2018-2019') plt.show() # 1B - Plot a line chart of randomly-generated values, with labels pets = [44, 50, 6, 14, 12, 14, 14, 19, 11, 19, 15, 15, 13, 10, 20, 32, 31, 16, 41, 34, 33, 37, 36, 40] plt.plot(fores, color = 'blue', marker = 'x', label = 'foreclosures') plt.plot(pets, color = 'orange', marker = 'o', label = 'petitions') plt.title('Petitions vs Foreclosures') plt.legend() plt.show() # 1C - Same values, but now combine them by quarter and show a bar chart fore_qtr = [] pet_qtr = [] total_f = 0 total_p = 0 for i in range(len(fores)): total_f += fores[i] total_p += pets[i] if (i + 1) % 3 == 0: fore_qtr.append(total_f) pet_qtr.append(total_p) total_f = 0 total_p = 0 print(fore_qtr) print(pet_qtr) pos = [i for i in range(len(fores) // 3)] plt.bar(pos, pet_qtr, color = 'purple', width = .35, label = 'petitions') pos2 = [i + .35 for i in range(len(pets) // 3)] plt.bar(pos2, fore_qtr, color = 'orange', width = .35, label = 'foreclosures') plt.xticks(pos, ['Q1-18', 'Q2-18', 'Q3-18', 'Q4-18', 'Q1-19', 'Q2-19', 'Q3-19', 'Q4-19']) plt.legend() plt.show() # Part 2 - Read from a CSV file into a nested list. # Then, get the min/max/average ages like last time data = [] with open('police_data.csv') as csvfile: reader = csv.reader(csvfile, delimiter = ',') next(reader) for row in reader: data.append(row) ages = [] for row in data: if row[AGE] != '': ages.append(int(row[AGE])) print('Youngest:', min(ages)) print('Oldest:', max(ages)) print('Average age:', sum(ages) / len(ages)) # Part 3A - Plot Under 18 vs 18 and Over age_groups = [0, 0] for age in ages: if age < 18: age_groups[0] += 1 else: age_groups[1] += 1 pos = [0, 1] plt.bar(pos, age_groups, color = ['blue', 'red'], width = .5) plt.xticks(pos, ['Under 18', '18 and Up']) plt.show() # Part 3B - Plot all the age categories age_groups = [0] * 6 pos = [0, 1, 2, 3, 4, 5] group_names = ['Under 18', '18-24', '25-34', '35-44', '45-54', '55+'] for age in ages: if age < 18: age_groups[0] += 1 elif age < 25: age_groups[1] += 1 elif age < 35: age_groups[2] += 1 elif age < 45: age_groups[3] += 1 elif age < 55: age_groups[4] += 1 else: age_groups[5] += 1 plt.bar(pos, age_groups, color = ['gray', 'gray', 'orange', 'orange', 'gray', 'gray']) plt.xticks(pos, group_names) plt.title('Incidents by Age Range') plt.show() main()