I am pretty new to python, so this should be a fairly easy question. I want to create a bar graph from a csv file. The file contains two columns with the headers "Latitude" and "TempAnom". I want to be able to read in the csv file and store the data as lat and tAnom. What is the best way to accomplish this?
Latitude | TempAnom |
---|---|
-89 | 0.06997871 |
-87 | 0.06997871 |
This is what I've tried so far, but I end up getting a KeyError: 'Latitude':
filename='2016_tAnoms.csv'
lat = []
tAnom = []
with open(filename,'r') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=',')
for row in csv_reader:
lat.append(row['Latitude'])
tAnom.append(row['TempAnom'])
Then I should be able to do the following to obtain my bar graph:
import matplotlib.pyplot as plt
plt.bar(lat,tAnom)
plt.title('2016 Temperature Anomalies by Latitude Band')
plt.xlabel('Latitude')
plt.ylabel('Temperature Anomaly (°C)')
plt.show()
Attempt 2: Runs correctly, but graph is missing data
filename='2016_tAnoms.csv'
lat = []
tAnom = []
with open(filename,'r') as csvfile:
points = csv.reader(csvfile, delimiter=',')
next(points)
for row in points:
lat.append(int(row[0]))
tAnom.append(int(float(row[1])))
Produced Graph There should be data from -89 to 89, but there's a lot of blanks and the int(float(row1)) is covering the data to the nearest integer. I want to keep the decimals.