class rawDataObj:
pointCount = 0
colCount = 0
columns = []
colLabels = []
title = ""
# ===========================================
class Series:
def __init__(self):
self.x = []
self.y = []
#~ def __init__(self, xIn, yIn):
#~ if (len(xIn) != len(yIn)):
#~ raise lengthInequalityError, "length inequality"
#~ for zidx in range(len(xIn)):
#~ self.x.append(xIn[zidx])
#~ self.y.append(yIn[zidx])
# ================================
def DbyDX(self, xIn, yIn):
if (len(xIn) != len(yIn)):
raise setSizeInequalityError, 'set size inequality'
else:
for zidx in range(len(xIn) - 1):
xVal = (xIn[zidx + 1] + xIn[zidx]) / 2.0
self.x.append(xVal)
yVal = (yIn[zidx + 1] - yIn[zidx]) / (xIn[zidx + 1] - xIn[zidx])
self.y.append(yVal)
# ===========================================
import re
def loadrawDataObjFromFile(filePathAndName="/home/david/Desktop/ptpdrh.csv", hasHeader=False):
try:
f = open(filePathAndName, 'r')
one = []
two = []
eof = False
retDataObj = rawDataObj()
lineNum = 0
headerLineCount = 2
while (eof == False):
line = f.readline()
eof = (line == "")
if (line != ""):
lineNum = lineNum + 1
elements = re.split(',', line)
# strip newline (\n escape sequence) from last field of line
elements[len(elements) - 1] = elements[len(elements) - 1].rstrip("\n")
# validate column count
if (lineNum > headerLineCount):
if (len(elements) != retDataObj.colCount):
raise inconsistentColumnCountError, ('column count inconsistent at data line ' + (retDataOb.pointCount + 1))
if (lineNum <= headerLineCount):
if (lineNum == 1):
retDataObj.title = line
elif (lineNum == 2):
retDataObj.colCount = len(elements)
for num in range(retDataObj.colCount):
retDataObj.columns.append([])
for label in elements:
retDataObj.colLabels.append(label)
else: # if (lineNum > headerLineCount)
string = ""
for zidx in range(retDataObj.colCount):
string = elements[zidx]
if (len(string) == 0):
retDataObj.columns[zidx].append(None)
else:
retDataObj.columns[zidx].append(float(string))
retDataObj.pointCount = retDataObj.pointCount + 1
f.close()
except IOError:
print "io exception while attempting to load raw data"
print "from file:"
print filePathAndName
return retDataObj
# ===========================================
# ===========================================
from pylab import *
filePath = "/home/david/Desktop/FinData/"
fileName = "RSA_CPI_1981-2008.csv"
filePathAndName = filePath + fileName
dataObj = loadrawDataObjFromFile(filePathAndName, True)
#for y in range(dataObj.colCount - 1):
# plot(dataObj.columns[0], dataObj.columns[y + 1], label=str(dataObj.colLabels[y + 1]))
fig = figure()
fig.canvas.set_window_title(dataObj.title)
# BASE SERIES
axBase = subplot(311)
baseLabel = label=str(dataObj.colLabels[dataObj.colCount - 1])
plot(dataObj.columns[0], dataObj.columns[dataObj.colCount - 1], label="base")
legend(numpoints=1, loc="upper left")
# FIRST DERIVATIVE
d1 = Series()
d1.DbyDX(dataObj.columns[0], dataObj.columns[dataObj.colCount - 1])
axD1 = subplot(312, sharex=axBase)
plot(d1.x, d1.y, label="d/dt")
legend(numpoints=1, loc="upper left")
#SECOND DERIVATIVE
d2 = Series()
d2.DbyDX(d1.x, d1.y)
axD2 = subplot(313, sharex=axBase)
plot(d2.x, d2.y, label="d2/dt2")
legend(numpoints=1, loc="upper left")
#xlabel(dataObj.colLabels[0])
show()
Tuesday, June 16, 2009
plot f, f', f'' from csv with python & matplotlib
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
comment: