problem2 done
[BS_AY250.git] / week4 / problem2 / problem2.py
blob1ca3924909ad39313f5eed94ca89f556b43a2d52
1 #!/usr/bin/env python
3 import sys
4 import os
6 import numpy as np
7 import matplotlib
8 import matplotlib.pyplot as plt
9 import pylab
11 from matplotlib.ticker import MultipleLocator
14 googlefn = 'google_data.csv'
15 nyfn = 'ny_temps.csv'
16 yahoofn = 'yahoo_data.csv'
18 def checkexists(fn):
19 if not os.path.exists(fn):
20 print "%s not found"%fn
21 sys.exit(2)
23 checkexists(googlefn)
24 checkexists(nyfn)
25 checkexists(yahoofn)
27 def loadcsv(fn):
28 return np.loadtxt(fn,skiprows=1,unpack=True,delimiter=',')
30 google_x , google_y = loadcsv(googlefn)
31 ny_x , ny_y = loadcsv(nyfn)
32 yahoo_x ,yahoo_y = loadcsv(yahoofn)
35 lg = pylab.plot(google_x,google_y,color='blue',label='Google Stock Value',linewidth=1.5)
36 ly = pylab.plot(yahoo_x,yahoo_y,color='purple',label='Yahoo Stock Value',linewidth=1.5)
39 pylab.xlabel('Date (MJD)')
40 pylab.ylabel('Value (Dollars)')
41 pylab.suptitle('New York Temperature, Google, and Yahoo!',fontsize=20)
43 minorLocator = MultipleLocator(20)
44 ax = pylab.gca()
45 ax.yaxis.set_minor_locator(minorLocator)
48 ax2 = pylab.twinx()
49 lny = pylab.plot(ny_x,ny_y,color='red',linestyle='--',label='NY Mon. High Temp',linewidth=1.5)
50 pylab.ylim(-150,100)
52 pylab.legend((ly,lg,lny),('Yahoo! Stock Value','Google Stock Value','NY Mon. High Temp'),loc=6,frameon=False,prop={'size':10})
54 minorLocator = MultipleLocator(200)
55 ax.xaxis.set_minor_locator(minorLocator)
56 minorLocator = MultipleLocator(10)
57 ax2.yaxis.set_minor_locator(minorLocator)
59 pylab.xlim(48800,55600)
61 pylab.ylabel('Temperature ($^\circ$F)')
63 pylab.savefig('student-stocks.png',dpi=200)