简体   繁体   中英

Plot normal distribution in Python from a .csv file

The following script draws the Normal Distribution of a sort of data given.

import numpy as np
import scipy.stats as stats
import pylab as pl

h = sorted ([0.9, 0.6, 0.5, 0.73788,...]) #Data that I would like to change

fit = stats.norm.pdf(h, np.mean(h), np.std(h))  
pl.plot(h,fit,'-o')
pl.show()    

I would like to find how to plot the data taken from a .csv file instead of having to introduce it manually. Suppose the data wanted is in the 2nd column of a given .csv file, the way I know to do something similar to isolate the data is by creating an intermediate file, but maybe this is not even necessary.

with open('infile.csv','rb') as inf, open('outfile.csv','wb') as outf:
    incsv = csv.reader(inf, delimiter=',')
    outcsv = csv.writer(outf, delimiter=',')
    outcsv.writerows(row[1] in incsv)

Anyway, basically my two questions here are,
- Would I be writing correctly the second column of a .csv into a new .csv file?
- How could I merge those two scripts so that I can substitute the static data in the first one for the data in a column of a .csv file?

It seems very roundabout to write the data back out to a file, presumably to read it back in again later. Why not create a list of the data?

def import_data(filename):
    """Import data in the second column of the supplied filename as floats."""
    with open(filename, 'rb') as inf:
        return [float(row[1]) for row in csv.reader(inf)]

You can then call this function to get the data you want to plot

h = sorted(import_data('infile.csv'))

As to your question "Would I be writing correctly the second column of a .csv into a new .csv file?" , the answer is: test it and find out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM