2012-07-20 148 views
3

假設我有一個包含兩列數據file.dat的文件。 我通常與有沒有辦法在gnuplot中繪製一個平均值?

plot "file.dat" u 1:2 

我要平均超過10(例如)前點和10點以下繪製它,並繪製其在相同的情節。 我可以輕鬆地用一些外部腳本,我在那裏做另一列:

for(i=-10;i<=10;++i) 
    $3[j] += $2[j-i] 

不過,我想知道做這件事的gnuplot的方式。 我的下一步將是進行高斯平均。

+0

看起來像Bézier曲線內置並可以用作'plot「file.dat」u 1:2 s b'。有一些平均值是很好的,但定製的會更好,所以問題仍然是開放的。 – 2012-07-20 20:51:01

回答

8

這可能令人驚訝的是,沒有內置到gnuplot中。由於gnuplot如何將數據作爲流處理,因此沒有好的方法來處理gnuplot中的單個數據點以及數據點的範圍。

關於gnuplot的一件大事就是調用外部腳本和工具是多麼容易。如果你想使用一個外部腳本來處理來自gnuplot的內的數據,你可以做這樣的:

plot "<script.py data.dat" u 1:2 

舉個例子,你可以用下面的python腳本。這有點矯枉過正,但您可以在腳本或命令行中將參數值設置爲硬編碼。

#!/usr/bin/python2.7 

import sys 

if (len(sys.argv) > 6): 
print "" 
print "This script takes one mandatory argument, the name of a file containing" 
print "data to be plotted. It takes up to four optional arguments as follows:" 
print " 1) the number of points before a data point to add into average." 
print " 2) the number of points after a data point to add into average." 
print " 3) the column number of y data (first column is column 1)" 
print " 4) the column number of x data (first column is column 1)" 
print "" 
exit() 

# set variable defaults 
box_back = 10 # number of points before current point to add into average 
box_front = 10 # number of points after current point to add into average 
y_col = 2  # column number of y data (first column is column 1) 
x_col = 1  # column number of x data (first column is column 1) 

# assign variables from command line arguments 
inputFileName = str(sys.argv[1]) 
if (len(sys.argv) > 2): 
box_back = int(sys.argv[2]) 
if (len(sys.argv) > 3): 
box_front = int(sys.argv[3]) 
if (len(sys.argv) > 4): 
y_col = int(sys.argv[4]) 
if (len(sys.argv) > 5): 
x_col = int(sys.argv[5]) 

# open input file 
f = open(inputFileName) 

# make list from lines in file 
lines = f.readlines() 

# make sure boxcar average will work 
if ((box_back + box_front + 1) > len(lines)): 
print "" 
print "ERROR: too many points for boxcar averaging." 
print "" 
exit() 

# this is the number of points encompassed in the boxcar average 
num_points = box_back + box_front + 1 

# this variable is the running sum. 
sum_vals = 0 

# add up values for first boxcar average 
for i_ in range(0,num_points): 
sum_vals += float(lines[i_].split()[y_col-1]) 
print float(lines[box_back].split()[x_col-1]),sum_vals/num_points 

# each subsequent average differs only in the first and last points from the 
# previous average. 
for i_ in range(box_back+1,len(lines)-box_front): 
sum_vals += float(lines[i_+box_front].split()[y_col-1]) 
sum_vals -= float(lines[i_-box_back-1].split()[y_col-1]) 
print float(lines[i_].split()[x_col-1]),sum_vals/num_points 
相關問題