将函数拟合到gnuplot中使用频率创建的直方图 简介

将函数拟合到gnuplot中使用频率创建的直方图 简介,plot,gnuplot,histogram,data-fitting,binning,Plot,Gnuplot,Histogram,Data Fitting,Binning,在gnuplot中,有一个解决方案可以从名为hist.dat的文件中创建直方图 1 2 2 2 3 通过使用命令 binwidth=1 set boxwidth binwidth bin(x,width)=width*floor(x/width) + binwidth/2.0 plot [0:5][0:*] "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq with boxes 这将生成一个直方图 问题: 如何使我的函数与此直方图相匹配?我定

在gnuplot中,有一个解决方案可以从名为
hist.dat的文件中创建直方图

1
2
2
2
3
通过使用命令

binwidth=1
set boxwidth binwidth
bin(x,width)=width*floor(x/width) + binwidth/2.0
plot [0:5][0:*] "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq with boxes
这将生成一个直方图

问题: 如何使我的函数与此直方图相匹配?我定义了一个高斯函数,并用

f(x) = a*exp(-((x-m)/s)**2)
a=3; m=2.5; s=1
在输出中,函数很好地遵循直方图

不幸的是,我无法使用命令适应此直方图

fit f(x) "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq via a,m,s  
                                                      ^
         Need via and either parameter list or file

那么,如何在不创建包含装箱值的新文件的情况下适应函数呢?

我面临着类似的问题,我找到了一种不太完美的解决方案

binwidth=1
set boxwidth binwidth
bin(x,width)=width*floor(x/width) + binwidth/2.0
set table 'hist.temp'
plot [0:5][0:*] "hist.dat" u (bin($1,binwidth)):(1.0) smooth freq with boxes
unset table
然后,您可以根据自己的喜好对文件进行拟合。我知道可能有更好的方法,但对我来说,这是一个快速有效的解决方案。我希望这会对你有所帮助


干杯

我用了这个,它起作用了:

gauss(x)=a/(sqrt(2*pi)sigma)*exp(-(x-mean)**2/(2*sigma**2))

fit gauss(x) 'data.txt' via a,sigma,mean

经过83次迭代后,GNUplot计算了我的a、sigma和mean

这是我实际做的,但我想避免创建临时文件。尽管问题已经很老了:为什么不想要一个新文件?您可以在脚本末尾删除该文件:
system('del hist.dat')
system('rm hist.dat')
。效率低下且特定于操作系统。如果我想将我的解决方案推广到更大的数据,那么编写和读取文件可能会成为瓶颈,并使源代码更长。如果我想向同事展示脚本,使用特定于操作系统的解决方案绝对不是一个好主意。现在,我认为python在很多情况下都更适合。我认为您没有抓住要点:Daniel希望将函数适配到的不是原始数据文件,而是使用
平滑频率后获得的数据。