Python - Why does this code not execute properly? Code: import csv import matplotlib.pyplot as plt def read_csv(iso_code): with open('owid-covid-data.csv', encoding='utf-8-sig', mode='r') as csvFormat: csvFile = csv.reader(csvFormat) day = [] cases = [] line = 1 for row in csvFile: line += 1 if row[0] == iso_code: if line > 1: day.append(int(row[3])) cases.append(int(row[4])) return day, cases def data_charts(data1, data2, type): fig = plt.figure() ax = fig.add_subplot(1, 1, 1) if type == 'bar': ax.bar(data1, data2) elif type == 'line': ax.plot(data1, data2) elif type == 'scatter': ax.scatter(data1, data2, s=2, c='blue', marker='+') ax.set_xlabel('Day') ax.set_ylabel('New Cases') ax.set_title('New cases by day for', type) ax.grid() plt.show() iso_code = input('Enter a country ISO code like KOR, MEX, USA, CAN, NZL: ') plotData = input('Enter chart type line, bar, or scatter: ') x, y = read_csv(iso_code) data_charts(x, y, plotData) Example File: iso_code,continent,location,date,total_cases,new_cases AFG,Asia,Afghanistan,2/24/2020,1,1 AFG,Asia,Afghanistan,2/25/2020,1,0 AFG,Asia,Afghanistan,2/26/2020,1,0 AFG,Asia,Afghanistan,2/27/2020,1,0 AFG,Asia,Afghanistan,2/28/2020,1,0 AFG,Asia,Afghanistan,2/29/2020,1,0 AFG,Asia,Afghanistan,3/1/2020,1,0 AFG,Asia,Afghanistan,3/2/2020,1,0 AFG,Asia,Afghanistan,3/3/2020,2,1 AFG,Asia,Afghanistan,3/4/2020,4,2 AFG,Asia,Afghanistan,3/5/2020,4,0 AFG,Asia,Afghanistan,3/6/2020,4,0 AFG,Asia,Afghanistan,3/7/2020,4,0 AFG,Asia,Afghanistan,3/8/2020,5,1 AFG,Asia,Afghanistan,3/9/2020,7,2 AFG,Asia,Afghanistan,3/10/2020,8,1 AFG,Asia,Afghanistan,3/11/2020,11,3 AFG,Asia,Afghanistan,3/12/2020,12,1 AFG,Asia,Afghanistan,3/13/2020,13,1 AFG,Asia,Afghanistan,3/14/2020,15,2
Python - Why does this code not execute properly?
Code:
import csv import matplotlib.pyplot as plt def read_csv(iso_code): with open('owid-covid-data.csv', encoding='utf-8-sig', mode='r') as csvFormat: csvFile = csv.reader(csvFormat) day = [] cases = [] line = 1 for row in csvFile: line += 1 if row[0] == iso_code: if line > 1: day.append(int(row[3])) cases.append(int(row[4])) return day, cases def data_charts(data1, data2, type): fig = plt.figure() ax = fig.add_subplot(1, 1, 1) if type == 'bar': ax.bar(data1, data2) elif type == 'line': ax.plot(data1, data2) elif type == 'scatter': ax.scatter(data1, data2, s=2, c='blue', marker='+') ax.set_xlabel('Day') ax.set_ylabel('New Cases') ax.set_title('New cases by day for', type) ax.grid() plt.show() iso_code = input('Enter a country ISO code like KOR, MEX, USA, CAN, NZL: ') plotData = input('Enter chart type line, bar, or scatter: ') x, y = read_csv(iso_code) data_charts(x, y, plotData)
Example File:
iso_code,continent,location,date,total_cases,new_cases AFG,Asia,Afghanistan,2/24/2020,1,1 AFG,Asia,Afghanistan,2/25/2020,1,0 AFG,Asia,Afghanistan,2/26/2020,1,0 AFG,Asia,Afghanistan,2/27/2020,1,0 AFG,Asia,Afghanistan,2/28/2020,1,0 AFG,Asia,Afghanistan,2/29/2020,1,0 AFG,Asia,Afghanistan,3/1/2020,1,0 AFG,Asia,Afghanistan,3/2/2020,1,0 AFG,Asia,Afghanistan,3/3/2020,2,1 AFG,Asia,Afghanistan,3/4/2020,4,2 AFG,Asia,Afghanistan,3/5/2020,4,0 AFG,Asia,Afghanistan,3/6/2020,4,0 AFG,Asia,Afghanistan,3/7/2020,4,0 AFG,Asia,Afghanistan,3/8/2020,5,1 AFG,Asia,Afghanistan,3/9/2020,7,2 AFG,Asia,Afghanistan,3/10/2020,8,1 AFG,Asia,Afghanistan,3/11/2020,11,3 AFG,Asia,Afghanistan,3/12/2020,12,1 AFG,Asia,Afghanistan,3/13/2020,13,1 AFG,Asia,Afghanistan,3/14/2020,15,2
Step by step
Solved in 3 steps with 1 images