2

这是我正在使用的代码的最小工作示例:

#!/bin/bash
gnuplot << EOF

set term postscript portrait color enhanced 
set encoding iso_8859_1
set output 'temp.ps'
set grid noxtics noytics noztics front
set size ratio 1
set multiplot
set lmargin 9; set bmargin 3; set rmargin 2; set tmargin 1

n=32    #number of intervals
max=13. #max value
min=-3.0    #min value
width=(max-min)/n        #interval width
hist(x,width)=width*floor(x/width)+width/2.0

set boxwidth width
set style fill solid 0.25 noborder

plot "< awk '{if (3.544068>=\$1) {print \$0}}' /data_file" u (hist(\$2,width)):(1.0) smooth freq w boxes lc rgb "red" lt 1 lw 1.5 notitle


EOF

这让我明白了:

盒子

我需要的是histeps改用,但是当我在上面的命令中更改boxes时,我得到:histepsplot

历史

这里发生了什么??

这是data_file。谢谢!


编辑:如果histeps遵循实际的外条限制而不是在两者之间插入值(就像这样boxes做)是不可能的,那么我怎么能画出用 制作的直方图的轮廓boxes


EDIT2:像往常一样,您的回答非常有用。虽然有一个小故障,这是我得到的输出,当我将两个图与命令结合起来时:

plot "< awk '{if (3.544068>=\$1) {print \$0}}' data_file" u (hist(\$2,width)):(1.0) smooth freq w boxes lc rgb "red" lt 1 lw 1.5 notitle, \
"<python pyscript.py data_file" u 1:2 w histeps lc rgb "red" lt 1 lw 1.5 notitle

histo_shifted

似乎有些东西正在改变python脚本的输出,我无法弄清楚它可能是什么。(在评论中修复)

4

1 回答 1

2

如果你有 python + numpy,分箱非常容易。这是一个非常流行的包,所以如果你在 Linux 上,你应该能够在你的发行版的存储库中找到它。

#Call this script as:
#python this_script_name.py 3.14159 data_file.dat

import numpy as np
import sys

n=32         #number of intervals
dmax=13.     #max value
dmin=-3.0    #min value


#primitive commandline parsing
limit = float(sys.argv[1])   #first argument is the limit
datafile = sys.argv[2]       #second argument is the datafile to read

data = []    #empty list
with open(datafile) as f:  #Open first commandline arguement for reading.
    for line in f:            #iterate through file returning 1 line at a time
        line = line.strip()   #remove whitespace at start/end of line
        if line.startswith('#'): #ignore comment lines.
            continue
        c1,c2 = [float(x) for x in line.split()] #convert line into 2 floats and unpack
        if limit >= c1:  #Check to make sure first one is bigger than your 3.544...
            data.append(c2) #If c1 is big enough, then c2 is part of the data

counts, edges = np.histogram(data,               #data to bin
                             bins=n,             #number of bins
                             range=(dmin,dmax),  #bin range
                             normed=False        #numpy2.0 -- use `density` instead
                             )
centers = (edges[1:] + edges[:-1])/2.  #average the bin edges to the center. 

for center,count in zip(centers,counts):  #iterate through centers and counts at same time
    print center,count                    #write 'em out for gnuplot to read.

gnuplot 脚本如下所示:

set term postscript portrait color enhanced 
set output 'temp.ps'
set grid noxtics noytics noztics front
set size ratio 1
set multiplot
set lmargin 9 
set bmargin 3
set rmargin 2 
set tmargin 1

set style fill solid 0.25 noborder

plot "<python pyscript.py 3.445 data_file" u 1:2 w histeps lc rgb "red" lt 1 lw 1.5 notitle

当我有更多空闲时间时,我会解释更多......

于 2012-10-23T00:15:35.710 回答