''' DS2000 Spring 2020 Source code from class - matplotlib ''' import csv import matplotlib.pyplot as plt DATE = 0 TIME = 1 LAT = 2 LONG = 3 WIND = 4 PRESSURE = 5 CATEGORIES = {'Cat 1' : 95, 'Cat 2' : 110, 'Cat 3' : 129, 'Cat 4': 156, 'Cat 5': 200} def process_file(filename): ''' Function: process_file Parameter: Name of the file (a string) Returns: nested list of strings, the contents of the file Does: assumes the file to be read is a CSV file ''' data = [] counter = 0 with open(filename) as infile: csv_contents = csv.reader(infile, delimiter = ',') for row in csv_contents: if counter == 0: counter += 1 else: data.append(row) return data def speed_only(hurricane_data): ''' Function: speed_only Parameters: Nested list of strings, from CSV file Returns: list of just the speed values (ints) ''' speed = [] for row in hurricane_data: speed.append(int(row[WIND])) return speed def graph_wind(speed_list): ''' Function: graph_wind Parameters: list of speed values over time (floats) Returns: nothing, just renders a linechart of the speed ''' plt.plot(speed_list) plt.xlabel('Time') plt.ylabel('Wind Speed') plt.title('Changes in windspeed over time') plt.show() def max_speeds(hurricane_data, categories): ''' Function: max_speeds parameters: Nested list of hurricane data Returns: list of ints -- the number of times the hurricane was in each category ''' speeds = list(categories.values()) num_per_category = [0 for i in range(len(speeds))] for row in hurricane_data: for i in range(len(speeds)): if int(row[WIND]) < speeds[i]: num_per_category[i] += 1 break return num_per_category def speed_histogram(max_speeds, categories): ''' Function: speed_histogram Parameters: nested list of hurricane data dictionary of category-to-max wind speed Returns: nothing, renders a histogram of how often the hurricane was in each category ''' pos = [i for i in range(len(categories))] plt.bar(pos, max_speeds) plt.xticks(pos, categories.keys()) plt.title('Time spent in each category') plt.show() def main(): hurricane_data = process_file('irma.csv') speeds = speed_only(hurricane_data) graph_wind(speeds) speed_buckets = max_speeds(hurricane_data, CATEGORIES) speed_histogram(speed_buckets, CATEGORIES) main()