0

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.

2 Answers 2

1

You are going to want to import csv and declare your variables as lists. Since plt will be able to use the list to display the data.

import matplotlib.pyplot as plt
import csv

lat = []
tAnon = []

From there you can go ahead and open the csv file you have and append your lists with the data in those columns.

with open('filename', 'r') as csvfile:
    points = csv.reader(csvfile, delimiter=',')
    for row in points:
        lat.append(flaot(row[0]))
        tAnon.append(float(row[1]))

Then you can plot your points like you have above. check this out if you are still a bit confused on reading and writing csv. https://realpython.com/python-csv/

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry about that, try changing the appending tAnon to a float instead of an int. You may need to do the same for the lat append as well.
Thanks. Any idea why my produced graph above contains a lot of missing blanks and seems to be rounding the values to the nearest tenth? Many of the values are 7-8 decimal places to the right of the decimal.
Without knowing too much about your data I can't help you much there. Found this, hope it works for you stackoverflow.com/a/29188910/4797904
0

My bar graph was not displaying the way I wanted it, because I had my data converted to integers when they needed to be decimals. I added "from decimal import Decimal" and changed the int(float(row[1])) to Decimal(row[1]).

    from decimal import Decimal

    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(Decimal(row[1]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.