使用 gnuplot 的累积数据和外推
Cumulative data and extrapolation with gnuplot
有一个不一定按日期排序的日期和事件列表
例如喜欢
# Date Event
04.12.2018 -4
23.06.2018 5
04.10.2018 3
11.11.2018 -9
08.03.2018 -4
08.03.2018 2
11.11.2018 -3
我想总结事件并进行(例如线性)外推,例如当数据达到某个阈值(例如零)时。
看起来smooth frequency
和smooth cumulative
似乎就是为此而生的。
但我正在努力解决以下问题:
a) 如何添加起始值(偏移量),例如StartValue = 500
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):(+StartValue) smooth cumulative w l t "Cumulated Events"
不做。
b) 如何获取累计数据?特别是如果数据没有按日期排序?
set table "DataCumulative.dat"
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth cumulative with table
unset table
这看起来类似于这个问题 (GNUPLOT: saving data from smooth cumulative),但我没有得到预期的数字。在我下面的文件 "DataCumulative.dat"
中的示例中,我期望唯一的日期和基本上来自下图的数据。如何获得?
代码:
### start code
reset session
set colorsequence classic
# function for creating a random date between two dates
t(date_str) = strptime("%d.%m.%Y", date_str)
date_random(d0,d1) = strftime("%d.%m.%Y",rand(0)*(t(d1)-t(d0)) + t(d0))
# create some random date data
date_start = "01.01.2018"
date_end = "30.06.2018"
set print $Data
do for [i=1:1000] {
print sprintf("%s\t%g", date_random(date_start,date_end), floor(rand(0)*10-6))
}
set print
set xdata time
set timefmt "%d.%m.%Y"
set xtics format "%b"
set xrange[date_start:"31.12.2018"]
set multiplot layout 2,1
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth frequency with impulses t "Events"
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth cumulative w l t "Cumulated Events"
unset multiplot
# attempt to get cumulative data into datablock
set table "DataCumulative.dat"
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth cumulative with table
unset table
### end of code
情节:
我想,我现在终于明白了。但是,有一些知识我还没有完全理解。
1。
为了获得累计数据你不应该设置
set table $DataCumulative
plot $Data u (stringcolumn(1)):2 smooth cumulative with table
unset table
而是:
set table $DataCumulative
plot $Data u (stringcolumn(1)):2 smooth cumulative
unset table
注意绘图命令中缺少的“with table
”。
第一个版本为您提供原始数据,第二个版本为您提供所需的累积数据。但是我还不明白为什么。
2。
默认数据文件分隔符设置
这是
set datafile separator whitespace
好像不行。它会给出类似 line xxx: No data to fit
的错误消息
相反,您必须设置
set datafile separator " \t" # space and TAB
但是我不明白为什么。
3。
试衣时间日期
f_lin(x) = m*x + c
根本不合身。显然,您必须减去开始日期并进行拟合。
f_lin(x) = m*(x-strptime("%d.%m.%Y", Date_Start)) + c
我记得很久以前在 gnuplot 文档中读过这篇文章,但现在找不到了。
目前,我对以下内容感到满意。
修改后的代码:
### generate random date between two dates
reset session
# function for creating a random date between two dates
t(date_str) = strptime("%d.%m.%Y", date_str)
date_random(d0,d1) = strftime("%d.%m.%Y",rand(0)*(t(d1)-t(d0)) + t(d0))
# create some random date data
Date_Start = "01.01.2018"
Date_End = "30.06.2018"
set print $Data
do for [i=1:100] {
print sprintf("%s\t%g", date_random(Date_Start,Date_End), floor(rand(0)*10-6))
}
set print
set xdata time
set timefmt "%d.%m.%Y"
# get cumulative data into datablock
set xtics format "%d.%m.%Y"
set table $DataCumulative
plot $Data u (stringcolumn(1)):2 smooth cumulative
unset table
set xtics format "%b"
set datafile separator " \t" # space and TAB
# linear function and fitting
f_lin(x) = m*(x-strptime("%d.%m.%Y", Date_Start)) + c
set fit nolog quiet
fit f_lin(x) $DataCumulative u 1:2 via m,c
Level_Start = 500
Level_End = 0
x0 = (Level_End - Level_Start - c)/m + strptime("%d.%m.%Y", Date_Start)
set multiplot layout 3,1
# event plot & cumulative plot
set xrange[Date_Start:"31.12.2018"]
set xtics format ""
set lmargin 7
set bmargin 0
plot $Data u (timecolumn(1,"%d.%m.%Y")):2 smooth frequency with impulses lc rgb "red" t "Events 2018"
set xtics format "%b"
set bmargin
plot $Data u (timecolumn(1,"%d.%m.%Y")):2 smooth cumulative w l lc rgb "web-green" t "Cumulated Events 2018"
# fit & extrapolation plot
set label 1 at x0, graph 0.8 strftime("%d.%m.%Y",x0) center
set arrow 1 from x0, graph 0.7 to x0, Level_End
set key at graph 0.30, graph 0.55
set xrange[Date_Start:x0+3600*24*50] # end range = extrapolated date + 50 days
set xtics format "%m.%y"
set yrange [-90:]
plot $DataCumulative u (timecolumn(1,"%d.%m.%Y")):(+Level_Start) w l lc rgb "blue" t "Cumulated Events",\
Level_End w l lc rgb "red" not,\
f_lin(x)+Level_Start w l ls 0 t "Fitting \& Extrapolation"
unset multiplot
### end of code
将导致:
有一个不一定按日期排序的日期和事件列表 例如喜欢
# Date Event
04.12.2018 -4
23.06.2018 5
04.10.2018 3
11.11.2018 -9
08.03.2018 -4
08.03.2018 2
11.11.2018 -3
我想总结事件并进行(例如线性)外推,例如当数据达到某个阈值(例如零)时。
看起来smooth frequency
和smooth cumulative
似乎就是为此而生的。
但我正在努力解决以下问题:
a) 如何添加起始值(偏移量),例如StartValue = 500
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):(+StartValue) smooth cumulative w l t "Cumulated Events"
不做。
b) 如何获取累计数据?特别是如果数据没有按日期排序?
set table "DataCumulative.dat"
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth cumulative with table
unset table
这看起来类似于这个问题 (GNUPLOT: saving data from smooth cumulative),但我没有得到预期的数字。在我下面的文件 "DataCumulative.dat"
中的示例中,我期望唯一的日期和基本上来自下图的数据。如何获得?
代码:
### start code
reset session
set colorsequence classic
# function for creating a random date between two dates
t(date_str) = strptime("%d.%m.%Y", date_str)
date_random(d0,d1) = strftime("%d.%m.%Y",rand(0)*(t(d1)-t(d0)) + t(d0))
# create some random date data
date_start = "01.01.2018"
date_end = "30.06.2018"
set print $Data
do for [i=1:1000] {
print sprintf("%s\t%g", date_random(date_start,date_end), floor(rand(0)*10-6))
}
set print
set xdata time
set timefmt "%d.%m.%Y"
set xtics format "%b"
set xrange[date_start:"31.12.2018"]
set multiplot layout 2,1
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth frequency with impulses t "Events"
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth cumulative w l t "Cumulated Events"
unset multiplot
# attempt to get cumulative data into datablock
set table "DataCumulative.dat"
plot $Data u (strftime("%d.%m.%Y",timecolumn(1,"%d.%m.%Y"))):2 smooth cumulative with table
unset table
### end of code
情节:
我想,我现在终于明白了。但是,有一些知识我还没有完全理解。
1。 为了获得累计数据你不应该设置
set table $DataCumulative
plot $Data u (stringcolumn(1)):2 smooth cumulative with table
unset table
而是:
set table $DataCumulative
plot $Data u (stringcolumn(1)):2 smooth cumulative
unset table
注意绘图命令中缺少的“with table
”。
第一个版本为您提供原始数据,第二个版本为您提供所需的累积数据。但是我还不明白为什么。
2。 默认数据文件分隔符设置 这是
set datafile separator whitespace
好像不行。它会给出类似 line xxx: No data to fit
相反,您必须设置
set datafile separator " \t" # space and TAB
但是我不明白为什么。
3。 试衣时间日期
f_lin(x) = m*x + c
根本不合身。显然,您必须减去开始日期并进行拟合。
f_lin(x) = m*(x-strptime("%d.%m.%Y", Date_Start)) + c
我记得很久以前在 gnuplot 文档中读过这篇文章,但现在找不到了。
目前,我对以下内容感到满意。
修改后的代码:
### generate random date between two dates
reset session
# function for creating a random date between two dates
t(date_str) = strptime("%d.%m.%Y", date_str)
date_random(d0,d1) = strftime("%d.%m.%Y",rand(0)*(t(d1)-t(d0)) + t(d0))
# create some random date data
Date_Start = "01.01.2018"
Date_End = "30.06.2018"
set print $Data
do for [i=1:100] {
print sprintf("%s\t%g", date_random(Date_Start,Date_End), floor(rand(0)*10-6))
}
set print
set xdata time
set timefmt "%d.%m.%Y"
# get cumulative data into datablock
set xtics format "%d.%m.%Y"
set table $DataCumulative
plot $Data u (stringcolumn(1)):2 smooth cumulative
unset table
set xtics format "%b"
set datafile separator " \t" # space and TAB
# linear function and fitting
f_lin(x) = m*(x-strptime("%d.%m.%Y", Date_Start)) + c
set fit nolog quiet
fit f_lin(x) $DataCumulative u 1:2 via m,c
Level_Start = 500
Level_End = 0
x0 = (Level_End - Level_Start - c)/m + strptime("%d.%m.%Y", Date_Start)
set multiplot layout 3,1
# event plot & cumulative plot
set xrange[Date_Start:"31.12.2018"]
set xtics format ""
set lmargin 7
set bmargin 0
plot $Data u (timecolumn(1,"%d.%m.%Y")):2 smooth frequency with impulses lc rgb "red" t "Events 2018"
set xtics format "%b"
set bmargin
plot $Data u (timecolumn(1,"%d.%m.%Y")):2 smooth cumulative w l lc rgb "web-green" t "Cumulated Events 2018"
# fit & extrapolation plot
set label 1 at x0, graph 0.8 strftime("%d.%m.%Y",x0) center
set arrow 1 from x0, graph 0.7 to x0, Level_End
set key at graph 0.30, graph 0.55
set xrange[Date_Start:x0+3600*24*50] # end range = extrapolated date + 50 days
set xtics format "%m.%y"
set yrange [-90:]
plot $DataCumulative u (timecolumn(1,"%d.%m.%Y")):(+Level_Start) w l lc rgb "blue" t "Cumulated Events",\
Level_End w l lc rgb "red" not,\
f_lin(x)+Level_Start w l ls 0 t "Fitting \& Extrapolation"
unset multiplot
### end of code
将导致: