gnuplot以设定的时间间隔平均随机时间数据块

时间:2015-01-28 20:30:45

标签: plot gnuplot average stochastic

您好我正在使用gnuplot来绘制数据块中结构化模拟的数据,如下所示:

CurrentTime  CurrentState
0            2
1.234        2
1.990        1
2.462        0


CurrentTime  CurrentState
0            2
0.895        1
1.456        2
2.052        1
3.017        0

数据块的数量并不严格知道,但至少有30个块。 请注意,每个CurrentTime的间隔数不同。 我正在使用以下代码按原样绘制数据

# GNUPlot code
set multiplot layout 2,1 title "Insert title" font ",14"
set tmargin 3
set bmargin 3
set lmargin 5
set rmargin 2
plot "data.txt" every :1 using 1:2:(column(-2)) with linespoints lc variable

由于multiplot命令,我要绘制的下一个内容将放在下图中。那个图我想成为我设置的时间间隔的平均数据。在我想要的伪代码中:

# pseudo code
float start, step, stop;
assign start, step, stop;
define Interval=start, by step, to stop; typed another way Interval=start:step:stop 
array sum(size(number of data blocks,length(Interval), length(Interval)))
assign sum=0;
for every data block
    for k=0 to length(CurrentTime)
        for j=0 to length(Interval)-1
           (CurrentTime(k) < Interval(j+1) && CurrentTime(k) > Interval(j-1)) ? sum += CurrentState(k) : sum += 0
average=sum/(Number of data blocks)

我试图在gnuplot中实现它。任何帮助都会很棒!

1 个答案:

答案 0 :(得分:1)

首先是数据文件,我的一些真实数据是

CurrentTime CurrentState
0       2
4.36393     1
5.76339     2
13.752      1
13.7645     2
18.2609     1
19.9713     2
33.7285     1
33.789      0


CurrentTime CurrentState
0       2
3.27887     1
3.74072     2
3.86885     1
4.97116     0


CurrentTime CurrentState
0       2
1.19854     1
3.23982     2
7.30501     1
7.83872     0

然后我用python在我想要检查平均值的时间间隔找到数据的平均值。我选择在离散的时间步骤检查,但它们可以是任何时间步骤。以下是我的python代码

#Loading data file: Goal is to calculate average(TimeIntervals)=averageOfTimeIntervals.
import numpy as np
data=np.genfromtxt('data.txt', comments='C')
CurrentState=data[:,1]
CurrentTime=data[:,0]
numberTimeIntervals=101
TimeIntervals=np.linspace(0,numberTimeIntervals-1,numberTimeIntervals)     #gives integer values of time
stateOfTimeIntervals=np.zeros(numberTimeIntervals,dtype=np.float64)
stateOfTimeIntervals[0]=CurrentState[0]  #setting initial state
#main loop
run=0
numberSimTimes=len(CurrentTime)
for j in range(0,len(stateOfTimeIntervals)): #start at 1 b/c we know   initial state
for k in range(0,numberSimTimes-1):
    lengthThisRun=0
    if CurrentTime[k] <= TimeIntervals[j] and CurrentTime[k+1] > TimeIntervals[j]:
        lengthThisRun+=1
        #Goal is to get the length of this run up to the time we decide to check the state
        stateOfTimeIntervals[j]+=CurrentState[k]
else:
        lengthThisRun+=1
#The number of runs can be claculated using
numberRuns=len(CurrentTime) - np.count_nonzero(CurrentTime)
print "Number of Runs=%f" %(numberRuns)
#Compute the average
averageState=stateOfTimeIntervals/numberRuns
#Write to file and plot with gnuplot
np.savetxt('plot2gnu.txt',averageState)

然后使用gnuplot我绘制了&#39; plot2gnu.txt&#39;使用以下代码

# to plot everything on the same plot use "multiplot"
set multiplot layout 2,1 title "Insert title" font ",14"
set tmargin 3
set bmargin 3
set lmargin 5
set rmargin 2
plot "data.txt" every :1 using 1:2:(column(-2)) with linespoints lc variable
plot 'plot2gnu.txt' using 1:2 with linespoints

我想指出使用伪列&#39;列(-2)&#39;在第三列中指定线条颜色。 &#39;柱(-2)&#39;表示&#34;包含多个数据集的文件中当前数据集的索引号。&#34; - 来自&#39; old&#39; gnuplot 4.6文档。