3

I've written a script to compute large csv files in dimensions: 27000 rows x 22 column. How can I read in the CSV file in order to use it in matplotlib in a scattered plot like the one in this thread?

axis range in scatter graphs

The concept of generating a scatter plot is understood. Attempts have been made to parse csv file by e.g.:

data=csv.reader(open('some_file.csv, 'rb'), delimiter='|', quotechar='"')

but without success.

Community
  • 1
  • 1
Daniyal
  • 885
  • 3
  • 16
  • 28
  • 6
    What -- specifically -- does "but without success" mean? Can you provide error messages or other indications of what's wrong? We can't really guess. – S.Lott Feb 27 '12 at 22:46
  • 1
    We do need more information, but there's also a syntax error in your code as posted here: `'somefile.csv` needs a closing single quote. Was that just a typo? – Mike Feb 27 '12 at 22:55
  • 2
    @Mike: Without an **actual** error message, that could very well the problem. Or. The problem could be that data is only a reader, not the actual data that's desired. – S.Lott Feb 27 '12 at 23:37
  • apologies for the late response. I did two massive faults: 1st-I've selected the wrong delimiter, the 2nd mistake was marcus and dm pointed to it: I've not had in mind that data is a file-like object...and probably a 3rd mistake: I haven't been any precise about the problems I had *blush*. – Daniyal Feb 28 '12 at 00:21

3 Answers3

9

Here is a quick solution

def getColumn(filename, column):
    results = csv.reader(open(filename), delimiter="\t")
    return [result[column] for result in results]

and then you can use it like this

time = getColumn("filename",0)
volt = getColumn("filaname",1)

plt.figure("Time/Volt")
plt.xlabel("Time(ms)")
plt.ylabel("Volt(mV)")
plt.plot(time,volt)
kechap
  • 2,077
  • 6
  • 28
  • 50
2

As a general alternative, you might be interested in the pandas python package by Wes McKinney: http://pandas.pydata.org/

It has literally changed my life for data analysis with python. It provides python with a data structure that is like R's data.frame, but even more powerful. And it's build on top of numpy.

And it will read csv files very easily, loading the data into a DataFrame (numpy array subclass) that can be easily sliced and manipulated.

Uri Laserson
  • 2,391
  • 5
  • 30
  • 39
1

Is that the correct delimiter? Did you read the documentation? http://docs.python.org/library/csv.html

data is a file-like object. you must iterate over it to access the data. each line is a list as marcus points out in his example.

dm03514
  • 54,664
  • 18
  • 108
  • 145