在gnuplot中绘制点的平均曲线 [英] Plotting Average curve for points in gnuplot

查看:136
本文介绍了在gnuplot中绘制点的平均曲线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[当前]

我正在导入一个文本文件,其中第一列具有仿真时间(0〜150),第二列具有延迟(0.01〜0.02).

I am importing a text file in which the first column has simulation time (0~150) the second column has the delay (0.01~0.02).

1.000000 0.010007
1.000000 0.010010
2.000000 0.010013
2.000000 0.010016
.
.
.
149.000000 0.010045
149.000000 0.010048
150.000000 0.010052
150.000000 0.010055

这给出了我的情节:

which gives me the plot:

[所需]

我需要在其上绘制一条平均线,如下图所示,并带有红线:

I need to plot an average line on it like shown in the following image with red line:

推荐答案

以下是带有示例数据的仅gnuplot解决方案:

Here is a gnuplot only solution with sample data:

set table "test.data"
set samples 1000
plot rand(0)+sin(x)
unset table

您应在 gnuplot演示页中进行查看运行平均值.我将通过动态构建功能来概括该演示.这样可以更轻松地更改平均值中包含的点数.

You should check the gnuplot demo page for a running average. I'm going to generalize this demo in terms of dynamically building the functions. This makes it much easier to change the number of points include in the average.

这是脚本:

# number of points in moving average
n = 50

# initialize the variables
do for [i=1:n] {
    eval(sprintf("back%d=0", i))
}

# build shift function (back_n = back_n-1, ..., back1=x)
shift = "("
do for [i=n:2:-1] {
    shift = sprintf("%sback%d = back%d, ", shift, i, i-1)
} 
shift = shift."back1 = x)"
# uncomment the next line for a check
# print shift

# build sum function (back1 + ... + backn)
sum = "(back1"
do for [i=2:n] {
    sum = sprintf("%s+back%d", sum, i)
}
sum = sum.")"
# uncomment the next line for a check
# print sum

# define the functions like in the gnuplot demo
# use macro expansion for turning the strings into real functions
samples(x) = $0 > (n-1) ? n : ($0+1)
avg_n(x) = (shift_n(x), @sum/samples($0))
shift_n(x) = @shift

# the final plot command looks quite simple
set terminal pngcairo
set output "moving_average.png"
plot "test.data" using 1:2 w l notitle, \
     "test.data" using 1:(avg_n($2)) w l lc rgb "red" lw 3 title "avg\\_".n

这是结果:

平均值比算法预期的要晚很多数据点.也许50分太多了.或者,可以考虑实施居中移动平均线,但这超出了此问题的范围. 而且,我还认为您可以使用外部程序更加灵活:)

The average lags quite a bit behind the datapoints as expected from the algorithm. Maybe 50 points are too many. Alternatively, one could think about implementing a centered moving average, but this is beyond the scope of this question. And, I also think that you are more flexible with an external program :)

这篇关于在gnuplot中绘制点的平均曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆