[英]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. 我想找到如何绘制从.csv文件获取的数据的方法,而不必手动进行介绍。 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.
假设所需数据在给定的.csv文件的第二列中,我知道做类似的事情来隔离数据的方式是创建一个中间文件,但这也许不是必需的。
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? -是否可以将.csv的第二列正确写入新的.csv文件?
- 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? -如何合并这两个脚本,以便可以将第一个脚本中的静态数据替换为.csv文件列中的数据?
It seems very roundabout to write the data back out to a file, presumably to read it back in again later. 将数据写回文件似乎很round回,大概以后再读回去。 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?" 关于您的问题“我是否可以将.csv的第二列正确写入新的.csv文件?” , the answer is: test it and find out.
,答案是:对其进行测试并找出答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.