|
From: <jd...@us...> - 2008-10-16 11:43:39
|
Revision: 6218
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6218&view=rev
Author: jdh2358
Date: 2008-10-16 11:43:33 +0000 (Thu, 16 Oct 2008)
Log Message:
-----------
added load and csv2rec followups to recarray demo
Added Paths:
-----------
trunk/py4science/examples/recarray_demo2.py
trunk/py4science/examples/recarray_demo3.py
Added: trunk/py4science/examples/recarray_demo2.py
===================================================================
--- trunk/py4science/examples/recarray_demo2.py (rev 0)
+++ trunk/py4science/examples/recarray_demo2.py 2008-10-16 11:43:33 UTC (rev 6218)
@@ -0,0 +1,51 @@
+"""
+parse and load ge.csv into a record array
+"""
+import time, datetime, csv
+import dateutil.parser
+import matplotlib.mlab as mlab
+import matplotlib.dates as mdates
+import matplotlib.cbook as cbook
+import numpy as np
+
+# this is how you can use the function np.loadtxt to do the same
+# JDH TODO: this isn't working in mlab.load or np.loadtxt. Fix
+#rows = np.loadtxt('data/ge.csv', skiprows=1, converters={0:mdates.date2num}, delimiter=',')
+rows = mlab.load('data/ge.csv', skiprows=1, converters={0:mdates.date2num}, delimiter=',')
+r = np.rec.fromrecords(rows, names='date,open,high,low,close,volume,adjclose')
+
+# compute the average approximate dollars traded over the last 10 days
+# hint: closing price * volume trades approx equals dollars trades
+recent = r[-10:]
+dollars = (recent.close * recent.volume).mean()
+print '$%1.2fM'%(dollars/1e6)
+
+# plot the adjusted closing price vs time since 2003 - hint, you must
+# use date2num to convert the date to a float for mpl. Make two axes,
+# one for price and one for volume. Use a bar chart for volume
+
+import matplotlib.pyplot as plt
+dates = mdates.num2date(r.date) # convert these to native datetime.date objects
+mask = dates > datetime.date(2003,1,1)
+price = r.adjclose[mask]
+volume = r.volume[mask]
+
+fig = plt.figure()
+fig.subplots_adjust(hspace=0)
+ax1 = fig.add_subplot(211)
+ax1.plot(dates, price, '-');
+
+ax1.grid()
+for label in ax1.get_xticklabels():
+ label.set_visible(False)
+
+ax2 = fig.add_subplot(212, sharex=ax1)
+ax2.bar(dates, volume);
+
+ax2.grid()
+for label in ax2.get_xticklabels():
+ label.set_rotation(30)
+ label.set_horizontalalignment('right')
+
+fig.autofmt_xdate()
+plt.show()
Added: trunk/py4science/examples/recarray_demo3.py
===================================================================
--- trunk/py4science/examples/recarray_demo3.py (rev 0)
+++ trunk/py4science/examples/recarray_demo3.py 2008-10-16 11:43:33 UTC (rev 6218)
@@ -0,0 +1,47 @@
+"""
+parse and load ge.csv into a record array
+"""
+import time, datetime, csv
+import dateutil.parser
+import matplotlib.mlab as mlab
+import matplotlib.dates as mdates
+import matplotlib.cbook as cbook
+import numpy as np
+
+# this is how you can use the function mlab.csv2rec to do the same
+r = mlab.csv2rec('data/ge.csv')
+r.sort() #sort by date, the first column
+
+# compute the average approximate dollars traded over the last 10 days
+# hint: closing price * volume trades approx equals dollars trades
+recent = r[-10:]
+dollars = (recent.close * recent.volume).mean()
+print '$%1.2fM'%(dollars/1e6)
+
+# plot the adjusted closing price vs time since 2003 - hint, you must
+# use date2num to convert the date to a float for mpl. Make two axes,
+# one for price and one for volume. Use a bar chart for volume
+
+import matplotlib.pyplot as plt
+# filter to get dates since 2003
+r = r[r.date > datetime.date(2003,1,1)]
+
+fig = plt.figure()
+fig.subplots_adjust(hspace=0)
+ax1 = fig.add_subplot(211)
+ax1.plot(r.date, r.adj_close, '-');
+
+ax1.grid()
+for label in ax1.get_xticklabels():
+ label.set_visible(False)
+
+ax2 = fig.add_subplot(212, sharex=ax1)
+ax2.bar(r.date, r.volume);
+
+ax2.grid()
+for label in ax2.get_xticklabels():
+ label.set_rotation(30)
+ label.set_horizontalalignment('right')
+
+fig.autofmt_xdate()
+plt.show()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|