views:

1874

answers:

3

I'm trying to create a simple probability density function(pdf) graph using data from one column of a csv file using csv dictreader, matplotlib and numpy...

Is there an easy way to use CSV DictReader combined with numpy arrays? Below is code that doesn't work. The error message is TypeError: len() of unsized object, which I'm guessing is related to the fact that my data is not in numpy array format? Also my data has negative and positive numbers. Thanks in advance!

import easygui
import csv
import scipy.stats
from numpy import*
from pylab import*


filename= easygui.fileopenbox(msg='Altitude outlier graph', title='select file',  filetypes=['*.csv'], default='X:\\')
alt_file=open(filename)    

x=[]
for row in csv.DictReader(alt_file):
    x.append(float(row['Dist_90m(nmi)']))

a=scipy.stats.pdf_moments(x)

prob, bins, patches= hist(a, 10,align='left',facecolor='green')

ylabel('probability density function')
show()
+4  A: 
Mark Rushakoff
Good, but use numpy.arange for the last issue you mention -- it does just fine with floating point numbers too!-)
Alex Martelli
You can also use `numpy.r_` e.g., `r_[2:3:5j]` -> `array([ 2. , 2.25, 2.5 , 2.75, 3. ])`, so in your case `r_[:1:100j]`.
J.F. Sebastian
A: 

Thanks for all the help!! The following code produces a graph of the probability density function: I'm still having some issues formating it but I think this is a good start.

import easygui
import csv
import scipy.stats
import numpy
from pylab import*

filename= easygui.fileopenbox(msg='Altitude outlier graph', title='select file', filetypes=['*.csv'], default='X:\\herring_schools\\')
alt_file=open(filename)    

a=[]
for row in csv.DictReader(alt_file):
    a.append(row['Dist_90m(nmi)'])
y= numpy.array(a, float)    

pdf, bins, patches=hist(y, bins=6, align='left',range=None, normed=True)
ylabel('probability density function')
xlabel('Distance from 90m contour line(nm)')
ylim([0,1])
show()
FS
A: 

Thanks for this post!

This help me a lot!

But, I modify this to open and load txt file with 2 columns (n,t) and arbitrary rows, see code below:

filename= easygui.fileopenbox(msg='Altitude outlier graph', title='select file', filetypes=['*.txt']); data = loadtxt(filename, skiprows = 1); n,t = data[:,0], data[:,1];

Marcos