我知道如何在gnuplot中创建直方图(只是使用“带框”),如果我的.dat文件已经有正确的二进制数据。是否有一种方法可以获取数字列表,并让gnuplot根据用户提供的范围和bin大小提供一个直方图?


当前回答

是的,它快速而简单,但非常隐蔽:

binwidth=5
bin(x,width)=width*floor(x/width)

plot 'datafile' using (bin($1,binwidth)):(1.0) smooth freq with boxes

查看帮助平滑频率,看看为什么上面做了一个直方图

要处理范围,只需设置xrange变量。

其他回答

As usual, Gnuplot is a fantastic tool for plotting sweet looking graphs and it can be made to perform all sorts of calculations. However, it is intended to plot data rather than to serve as a calculator and it is often easier to use an external programme (e.g. Octave) to do the more "complicated" calculations, save this data in a file, then use Gnuplot to produce the graph. For the above problem, check out the "hist" function is Octave using [freq,bins]=hist(data), then plot this in Gnuplot using

set style histogram rowstacked gap 0
set style fill solid 0.5 border lt -1
plot "./data.dat" smooth freq with boxes

是的,它快速而简单,但非常隐蔽:

binwidth=5
bin(x,width)=width*floor(x/width)

plot 'datafile' using (bin($1,binwidth)):(1.0) smooth freq with boxes

查看帮助平滑频率,看看为什么上面做了一个直方图

要处理范围,只需设置xrange变量。

同一数据集上不同数量的箱子可以揭示数据的不同特征。

不幸的是,没有通用的最佳方法可以确定箱子的数量。

其中一个强大的方法是Freedman-Diaconis规则,它根据给定数据集的统计数据自动确定箱子的数量,还有许多其他的替代方法。

因此,下面的代码可以在gnuplot脚本中使用Freedman-Diaconis规则:

假设你有一个文件,它只包含一列样本,samplesFile:

# samples
0.12345
1.23232
...

以下(基于ChrisW的回答)可以嵌入到现有的gnuplot脚本中:

...
## preceeding gnuplot commands
...

#
samples="$samplesFile"
stats samples nooutput
N = floor(STATS_records)
samplesMin = STATS_min
samplesMax = STATS_max
# Freedman–Diaconis formula for bin-width size estimation
    lowQuartile = STATS_lo_quartile
    upQuartile = STATS_up_quartile
    IQR = upQuartile - lowQuartile
    width = 2*IQR/(N**(1.0/3.0))
    bin(x) = width*(floor((x-samplesMin)/width)+0.5) + samplesMin

plot \
    samples u (bin(\$1)):(1.0/(N*width)) t "Output" w l lw 1 smooth freq 

我对Born2Smile的解决方案做了一些修改。

我知道这不太合理,但以防万一,你可能需要它。如果您的数据是整数,并且您需要一个浮动容器大小(可能是为了与另一组数据进行比较,或在更细的网格中绘制密度),您将需要在floor内添加一个0到1之间的随机数。否则,由于四舍五入误差会出现尖峰。地板(x/width+0.5)是不行的,因为它会创建与原始数据不相符的模式。

binwidth=0.3
bin(x,width)=width*floor(x/width+rand(0))

我发现这个讨论非常有用,但我也遇到过一些“四舍五入”的问题。

更准确地说,使用0.05的binwidth,我注意到,使用上面介绍的技术,读取0.1和0.15的数据点落在同一个bin中。这(显然是不想要的行为)很可能是由于“地板”功能。

下面是我的小贡献,试图规避这一点。

bin(x,width,n)=x<=n*width? width*(n-1) + 0.5*binwidth:bin(x,width,n+1)
binwidth = 0.05
set boxwidth binwidth
plot "data.dat" u (bin($1,binwidth,1)):(1.0) smooth freq with boxes

这个递归方法适用于x >=0;我们可以用更多的条件语句来概括它,从而得到更一般的结果。