Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Reading CSV file and plotting a scatter

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.

like image 720
Daniyal Avatar asked Jun 05 '26 22:06

Daniyal


2 Answers

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)
like image 179
kechap Avatar answered Jun 07 '26 11:06

kechap


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.

like image 43
dm03514 Avatar answered Jun 07 '26 10:06

dm03514



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!