2010-03-18 149 views
187

我知道如何在gnuplot中创建直方图(只需使用“with boxes”),如果我的.dat文件已经有合适的分级数据。有没有一种方法来获取数字列表,并让gnuplot根据用户提供的范围和容器大小提供直方图?使用gnuplot的直方图?

+2

如果您没有得到答案,还有其他工具可以做这种事情。我使用Root(http://root.cern.ch/),其中许多人使用R,至少还有其他一些选项。 – dmckee 2010-03-18 17:15:17

+0

什么是垃圾桶和什么是垃圾箱? – 2015-05-19 13:54:26

回答

208

是,它的快速和简单的,虽然很隐蔽:

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

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

退房help smooth freq明白为什么上面做一个直方图

处理范围只设置x范围变量。

+8

我认为@ ChrisW的回答下面给任何想要在Gnuplot中制作柱状图的人留下了一个重要的观点。 – Abhinav 2013-10-26 02:49:33

+1

要非常小心,只有在集合中没有“丢失”元素时才能使用...此函数将缺少元素的y值固定为之前非缺失元素的y值。这可能是非常误导! – PinkFloyd 2017-11-05 21:06:21

77

我有一对夫妇更正/补充Born2Smile的非常有用的答案:

  1. 空箱引起了框邻近区间错误地延伸到它的空间;避免使用set boxwidth binwidth
  2. 在Born2Smile的版本中,垃圾箱呈现为居中的下限。严格地说,他们应该从下界延伸到上界。这可以通过修改bin函数来纠正:bin(x,width)=width*floor(x/width) + binwidth/2.0
+10

其实第二部分应该是'bin(x,width)= width * floor(x/width)+ binwidth/2.0'(浮点运算) – bgw 2010-08-29 18:33:49

+7

您的意思是'bin(x,width)= width * floor(x /宽度)+宽度/2.0'。如果我们通过'width'作为参数,然后使用它。 :-) – Mitar 2013-05-15 23:49:08

+2

@mgilson我认为ChrisW的回答给这个答案带来了一个重要的更正。 – Abhinav 2013-10-26 03:07:15

40

是否要绘制一个类似于此图的图? enter image description here 是吗?然后,你可以看看我的博客文章:http://gnuplot-surprising.blogspot.com/2011/09/statistic-analysis-and-histogram.html

从代码重点线:

n=100 #number of intervals 
max=3. #max value 
min=-3. #min value 
width=(max-min)/n #interval width 
#function used to map a value to the intervals 
hist(x,width)=width*floor(x/width)+width/2.0 
set boxwidth width*0.9 
set style fill solid 0.5 # fill style 

#count and plot 
plot "data.dat" u (hist($1,width)):(1.0) smooth freq w boxes lc rgb"green" notitle 
5

我发现这个讨论是非常有用的,但我已经经历了一些“四舍五入”的问题。

更准确地说,使用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;人们可以用更多的条件语句来概括它,以获得更一般的东西。

4

我们不需要使用递归方法,它可能会很慢。我的解决方案是使用用户定义的函数rint ins intd int int或floor。

rint(x)=(x-int(x)>0.9999)?int(x)+1:int(x) 

此功能将给予rint(0.0003/0.0001)=3,而int(0.0003/0.0001)=floor(0.0003/0.0001)=2

为什么?请注意0​​

67

要非常小心:本页面上的所有答案都隐含地决定了分档开始的位置 - 如果您愿意,最左边的分栏的左侧边缘 - 用户的手中。如果用户将这些用于装箱数据的功能与他/她自己的关于装箱开始的决定(如在上面链接的博客上所做的)相结合,则上述功能都是不正确的。凭借一个任意起点分档“民”,正确的功能是:

bin(x) = width*(floor((x-Min)/width)+0.5) + Min 

你可以看到为什么这是正确的顺序(它有助于在其中的一个地方画了几箱和一个点)。从您的数据点减去最小值以查看离它的分档范围有多远。然后除以binwidth,这样你就可以有效地以“bin”为单位进行工作。然后,将结果“落地”到该垃圾箱的左侧边缘,加0.5到垃圾箱的中间,乘以宽度,这样就不再以垃圾箱为单位工作,而是以绝对比例再次,然后最后加回你在开始时减去的最小偏移量。

考虑动作这样的功能:

Min = 0.25 # where binning starts 
Max = 2.25 # where binning ends 
n = 2 # the number of bins 
width = (Max-Min)/n # binwidth; evaluates to 1.0 
bin(x) = width*(floor((x-Min)/width)+0.5) + Min 

例如值1.1确实落在左边的bin中:

  • 这个函数正确地将它映射到左边bin(0.75)的中心;
  • Born2Smile的答案bin(x)= width * floor(x/width),将其错误地映射为1;
  • mas90的答案bin(x)= width * floor(x/width)+ binwidth/2.0,将其错误地映射为1.5。

Born2Smile的答案只有在bin边界出现在(n + 0.5)* binwidth(其中n以整数运行)时才是正确的。 mas90的答案只有在bin边界出现在n * binwidth时才是正确的。

+0

+1注意细节......谢谢 – Abhinav 2013-10-26 01:42:17

2

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

我知道这没什么意义,但你可能需要它以防万一。如果您的数据是整数,并且您需要一个浮动块大小(可能与另一组数据比较,或以更精细的网格绘制密度),则需要在楼层内部0和1之间添加一个随机数。否则,由于四舍五入错误会出现尖峰。 floor(x/width+0.5)不会这样做,因为它会创建不符合原始数据的模式。

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

这使得绝对没有意义! – Christoph 2013-12-02 16:54:24

+1

你还没有遇到过这种情况,但你可能会迟一些。你可以用正态分布的整数用float sd来测试它,并用bin = 1和bin = s.d绘制直方图。看看你有没有rand(0)技巧。在审阅他的手稿时,我发现了一位合作者的错误。他的结果从绝对无稽之谈变成了一个美丽的人物。 – path4 2014-02-27 05:07:09

+0

好吧,也许解释是如此之短,没有一个更具体的测试案例,人们不能理解它。我会对你的答案做一个简短的编辑,这样我就可以撤销downvote;) – Christoph 2014-02-27 08:14:09

6

像往常一样,Gnuplot是绘制甜美外观图的绝佳工具,它可以用来执行各种计算。 但是,它的目的是绘制数据而不是用作计算器,而且使用外部程序(例如八度)更容易执行更复杂的计算,将这些数据保存在文件中,然后使用Gnuplot生成图表。针对上述问题,检查出的“历史”功能是使用八音[freq,bins]=hist(data),然后在gnuplot的使用

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

对于合并函数绘制这个,我没想到至今所提供的功能的结果。也就是说,如果我的binwidth为0.001,那么这些函数将会将这些箱子居中放置在0.0005个点上,而我觉得让这些箱子以0.001个边界为中心更直观。

换句话说,我想有

Bin 0.001 contain data from 0.0005 to 0.0014 
Bin 0.002 contain data from 0.0015 to 0.0024 
... 

我想出了装箱函数是

my_bin(x,width)  = width*(floor(x/width+0.5)) 

这里有一个脚本来一些提供斌功能比较这一个:

rint(x) = (x-int(x)>0.9999)?int(x)+1:int(x) 
bin(x,width)  = width*rint(x/width) + width/2.0 
binc(x,width)  = width*(int(x/width)+0.5) 
mitar_bin(x,width) = width*floor(x/width) + width/2.0 
my_bin(x,width)  = width*(floor(x/width+0.5)) 

binwidth = 0.001 

data_list = "-0.1386 -0.1383 -0.1375 -0.0015 -0.0005 0.0005 0.0015 0.1375 0.1383 0.1386" 

my_line = sprintf("%7s %7s %7s %7s %7s","data","bin()","binc()","mitar()","my_bin()") 
print my_line 
do for [i in data_list] { 
    iN = i + 0 
    my_line = sprintf("%+.4f %+.4f %+.4f %+.4f %+.4f",iN,bin(iN,binwidth),binc(iN,binwidth),mitar_bin(iN,binwidth),my_bin(iN,binwidth)) 
    print my_line 
} 

和这里的输出

data bin() binc() mitar() my_bin() 
-0.1386 -0.1375 -0.1375 -0.1385 -0.1390 
-0.1383 -0.1375 -0.1375 -0.1385 -0.1380 
-0.1375 -0.1365 -0.1365 -0.1375 -0.1380 
-0.0015 -0.0005 -0.0005 -0.0015 -0.0010 
-0.0005 +0.0005 +0.0005 -0.0005 +0.0000 
+0.0005 +0.0005 +0.0005 +0.0005 +0.0010 
+0.0015 +0.0015 +0.0015 +0.0015 +0.0020 
+0.1375 +0.1375 +0.1375 +0.1375 +0.1380 
+0.1383 +0.1385 +0.1385 +0.1385 +0.1380 
+0.1386 +0.1385 +0.1385 +0.1385 +0.1390