gnuplot - 有没有办法在 gnuplot 中绘制平均值?

标签 gnuplot numerical-analysis

假设我有一个包含两列数据的文件file.dat。 我通常会用

绘制它
plot "file.dat" u 1:2

我想平均超过 10 个(例如)前面的点和 10 个后面的点,并将其绘制在同一个图上。 我可以使用一些外部脚本轻松做到这一点,并在其中创建另一列:

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

但是,我想知道在 gnuplot 中执行此操作的方法。 我的下一步是进行高斯平均。

最佳答案

也许令人惊讶的是,这不是 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

关于gnuplot - 有没有办法在 gnuplot 中绘制平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11586121/

相关文章:

graph - gnuplot 绘制离散(?)时间数据

numerical-analysis - 编程和数值分析

从数据文件绘制 3D 曲线

Linux GUI : gnuplot, bash 脚本,octave

gnuplot - 绘制多天的相同时间段

java - 是否可以在Java中提取样条函数的公式?

gnuplot - 如何在gnuplot中的xtics之间创建一个等间距的图形?

floating-point - 使用浮点计算极坐标中两点之间的距离

c++ - 使用 Eigen3 的稀疏矩阵的特征值