Monday, June 15, 2009

Import & Display CSV Scatter with Python & MatPlotLib

  

class dataObject:

pointCount = 0

colCount = 0
columns = []
colLabels = []

# ===========================================

import re

def loadDataObjectFromFile():

filePath = "/home/david/Desktop/"
fileName = "ptpdrh.csv"
filePathAndName = filePath + fileName

firstLineIsLabels = True

try:
f = open(filePathAndName, 'r')

one = []
two = []

eof = False

retDataObj = dataObject()

isFirstLine = True

while (eof == False):

line = f.readline()
eof = (line == "")

if (line != ""):

elements = re.split(',', line)

elements[len(elements) - 1] = elements[len(elements) - 1].rstrip("\n")

if (isFirstLine == False):
if (len(elements) != retDataObj.colCount):
raise inconsistentColumnCountError, ('column count inconsistent at data line ' + (retDataOb.pointCount + 1))

if (isFirstLine == True):

isFirstLine = False

retDataObj.colCount = len(elements)

for num in range(retDataObj.colCount):
retDataObj.columns.append([])

if (firstLineIsLabels == True):

for label in elements:
retDataObj.colLabels.append(label)

else:

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 *

dataObj = loadDataObjectFromFile()

titleText = "title"

for y in range(dataObj.colCount - 1):
plot(dataObj.columns[0], dataObj.columns[y + 1], label=str(dataObj.colLabels[y + 1]))

legend(numpoints=1)
xlabel(dataObj.colLabels[0])
ylabel("")
title(titleText)
show()

No comments:

Post a Comment

comment: