''' Honors Series - Data Science Sample code - matplotlib and CSV files ''' import matplotlib.pyplot as plt import csv AGE = 5 FLEE = 12 def main(): # Plot a simple line chart (Mass. data) ma_data = [9, 12, 3, 3, 5] plt.plot(ma_data, color = 'orange') plt.title('Number of shootings per year in Mass.') plt.xticks([0, 1, 2, 3, 4], ['2015', '2016', '2017', '2018', '2019']) plt.show() # Add the Wash. state and Tenn. data to the same line chart wa_data = [16, 27, 39, 20, 45] tn_data = [19, 21, 26, 24, 30] plt.plot(ma_data, color = 'orange', label = 'Mass.') plt.plot(wa_data, color = 'blue', label = 'Wash') plt.plot(tn_data, color = 'red', label = 'Tenn') plt.title('Number of shootings per year') plt.xticks([0, 1, 2, 3, 4], ['2015', '2016', '2017', '2018', '2019']) plt.legend() plt.show() # Read the data from a CSV file (smaller version of police data) data = [] with open('small_data.csv') as csvfile: reader = csv.reader(csvfile, delimiter = ',') next(reader) for row in reader: data.append(row) print(data) # Get one column from our nested list into a single list by itself flee = [] for row in data: flee.append(row[FLEE]) print(flee) # Count up the different type of fleeing (Not, Car, Foot) flee_type = [0, 0, 0] for f in flee: if f == 'Not fleeing': flee_type[0] += 1 elif f == 'Car': flee_type[1] += 1 else: flee_type[2] += 1 print(flee_type) # Plot the different types of fleeing pos = [0, 1, 2] plt.bar(pos, flee_type, color = 'orange') plt.xticks(pos, ['Not Fleeing', 'Car', 'Foot']) plt.title('Action of Victim') plt.show() main()