Last active
April 18, 2016 02:28
-
-
Save Echocage/99e8b97bc95c85a4f8de83d37b64d337 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import datetime | |
import matplotlib.pyplot as plt | |
with open('brentdata.csv') as f: | |
csv_data = csv.DictReader(f) | |
data = list(csv_data) | |
# Data now contains a list of dictionaries, each dictionary containing a line of the csv | |
# Here's what the dictionary equalling one line looks like | |
# {'Date': '1987-05-20', 'Brent Spot Price': '18.63'} | |
# This function converts the string date into a datetime object so that matplotlib can use it as the x axes | |
def convert_date(string): | |
return datetime.datetime.strptime(string, "%Y-%m-%d") | |
# We then extract all the y and x values from the list of dictionaries and put them in seperate lists to feed into matplotlib | |
y = [item['Brent Spot Price'] for item in data] | |
x = [convert_date(item['Date']) for item in data] | |
plt.plot(x, y, 'ro') | |
plt.autoscale = True | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment