Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Gnuplot 具有unix时间戳的csv中多天内同一分钟的最大值_Gnuplot - Fatal编程技术网

Gnuplot 具有unix时间戳的csv中多天内同一分钟的最大值

Gnuplot 具有unix时间戳的csv中多天内同一分钟的最大值,gnuplot,Gnuplot,我有一个带有unix时间戳列的CSV,它是在几天内收集的,每5分钟有一行数据(我的光伏屋顶发电厂的输出日志) 我想创建一个24小时的绘图,显示所有天中每一分钟(第五分钟)的最大值 这可以通过gnuplot自己的功能来完成,还是必须通过scrips在gnuplot之外进行处理 您没有显示您的确切数据结构是什么样子,-theozh 这些文件相当大。我在这里举了一个例子: (300kB) 我对第4列(dc1p)和第9列(dc2p)特别感兴趣。 第1列(Zeit)保存unix时间戳 最终的目标是为dc

我有一个带有unix时间戳列的CSV,它是在几天内收集的,每5分钟有一行数据(我的光伏屋顶发电厂的输出日志)

我想创建一个24小时的绘图,显示所有天中每一分钟(第五分钟)的最大值

这可以通过
gnuplot
自己的功能来完成,还是必须通过scrips在
gnuplot
之外进行处理



您没有显示您的确切数据结构是什么样子,-theozh

这些文件相当大。我在这里举了一个例子:
(300kB)

我对第4列(
dc1p
)和第9列(
dc2p
)特别感兴趣。 第1列(
Zeit
)保存unix时间戳


最终的目标是为
dc1p
dc2p
提供单独的图形(颜色),但这是一个不同的问题;o)

有关时间格式,请参见

如果不关心格式随时间的变化,可以使用
every
命令,请参阅,但这并不需要最大值或其他值


对于给定时间间隔内的最大值,我建议使用
awk
脚本,例如参见gnuplot 5.2中的,您可以使用新的数组数据类型来计算每5分钟插槽的最大值。我不是gnuplot专家,因此下面的示例需要更多的工作,但显示了潜力

假设数据与这些行相似,格式中有日期
yyyy.mm.dd.HH:mm
,逗号和y值:

2018.02.03.18:23,4
2018.02.03.19:23,7
2018.02.04.18:23,8
2018.02.05.19:23,11
我们不使用gnuplot的内置时间解析,因为我们想忽略日期,所以我们创建了一个函数
fsecs
,使用
substr(stringcolumn(…),12,16)
从数据列1获取小时和分钟,并
strptime(%H:%M,…)
将其转换为秒:

set datafile separator ","
fsecs(v) = strptime("%H:%M",substr(stringcolumn(v),12,16))
我们创建一个数组
Max
,以“5分钟时隙”为索引,其中每天有
24*60/5
。它被初始化为
NaN
,而不是一个数字

Nitems = int(24*60/5)+1
array Max[Nitems]
do for [i=1:Nitems] {
    Max[i] = NaN
}
然后,我们将数据文件
data.csv
绘制到一个虚拟表中,而不是生成任何输出。在浏览数据时,我们通过数据x值(第1列)索引
Max
,数据x值通过
fsecs(1)
转换为秒,然后通过
findex()
索引到插槽。这是
Max[findex(fsecs(1))]
。 我们调用函数
fmax()
,返回要在数组中设置的新最大值

findex(x) = int(((x)/60)/5)
fmax(a,b) = ((a>=b)?a:b)
set table $Dummy
  plot 'data.csv' using \
    (Max[findex(fsecs(1))] = fmax(Max[findex(fsecs(1))],$2)):2
unset table
最后,我们绘制数组,该数组是插槽号与该插槽号中保存的值的对比

plot Max using 1:(Max[$1]) with points lw 2 title "max day"

这在5.2上对我有效。您仍然需要使用HH:MM标记x轴,并更改日期解析以满足您的需要。

您没有显示确切的数据结构,因此我假设了一些情况。为了说明一些事情,我创建了一些测试数据,在第一列中有时间戳,在第二列中有一些值。此外,我假设你的数据在每一天开始前都没有空行,这会让生活变得更轻松。无论如何,您仍然可以使用gnuplot完成这一切,而无需外部脚本

首先,您必须设置开始日期,例如
“29.08.2019 00:00:00”
。通过使用模
%86400
(每天86400秒),您将每隔24小时在彼此上方绘制一次。通过对虚拟数据块的单独绘图命令
$dummy
,您将提取每天的最大值,并将最大值放入数组
DailyMax
。然后创建一个显示这些值的标签。最后,绘制所有曲线。如果您需要更多关于某些细节的解释,请毫不犹豫地询问。 我希望,我正确理解了您的问题,您可以根据自己的需要修改建议。使用gnuplot 5.2.6进行测试

代码:

### timestamp plot for 24h with extraction of daily maximum 
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

# extract the maximum of each day and store it in an array
array DailyMax[NoOfDays]
DayPrev = NaN
set table $Dummy
   plot $Data u (DayPrev!=DayNo($1) ? Comp=$2 : 0, DayPrev=DayNo($1), \
       $2>=Comp ? (Comp=$2, DailyMax[DayNo($1)+1]=Comp) : 0) with table
unset table
print DailyMax

# create label with max values
do for [i=1:NoOfDays] {
    set label i at graph 0.05, 0.95-i*0.05 tc lt i-1 sprintf("Max of day %d: %.1f\n", i, DailyMax[i]) 
}

set xdata time
set format x "%H:%M"
set timefmt "%s"

plot $Data u (Hours24($1)):2:(DayNo($1)) w l lc var notitle
### end of code
### timestamp plot plot maximum within every 5 min interval over all days
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

set xdata time
set format x "%H:%M"
set timefmt "%s"
plot $Data u (Hours24($1)):2 w boxes fill solid 1.0 fc "web-green" notitle
### end of code
结果:

### timestamp plot for 24h with extraction of daily maximum 
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

# extract the maximum of each day and store it in an array
array DailyMax[NoOfDays]
DayPrev = NaN
set table $Dummy
   plot $Data u (DayPrev!=DayNo($1) ? Comp=$2 : 0, DayPrev=DayNo($1), \
       $2>=Comp ? (Comp=$2, DailyMax[DayNo($1)+1]=Comp) : 0) with table
unset table
print DailyMax

# create label with max values
do for [i=1:NoOfDays] {
    set label i at graph 0.05, 0.95-i*0.05 tc lt i-1 sprintf("Max of day %d: %.1f\n", i, DailyMax[i]) 
}

set xdata time
set format x "%H:%M"
set timefmt "%s"

plot $Data u (Hours24($1)):2:(DayNo($1)) w l lc var notitle
### end of code
### timestamp plot plot maximum within every 5 min interval over all days
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

set xdata time
set format x "%H:%M"
set timefmt "%s"
plot $Data u (Hours24($1)):2 w boxes fill solid 1.0 fc "web-green" notitle
### end of code

添加:

### timestamp plot for 24h with extraction of daily maximum 
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

# extract the maximum of each day and store it in an array
array DailyMax[NoOfDays]
DayPrev = NaN
set table $Dummy
   plot $Data u (DayPrev!=DayNo($1) ? Comp=$2 : 0, DayPrev=DayNo($1), \
       $2>=Comp ? (Comp=$2, DailyMax[DayNo($1)+1]=Comp) : 0) with table
unset table
print DailyMax

# create label with max values
do for [i=1:NoOfDays] {
    set label i at graph 0.05, 0.95-i*0.05 tc lt i-1 sprintf("Max of day %d: %.1f\n", i, DailyMax[i]) 
}

set xdata time
set format x "%H:%M"
set timefmt "%s"

plot $Data u (Hours24($1)):2:(DayNo($1)) w l lc var notitle
### end of code
### timestamp plot plot maximum within every 5 min interval over all days
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

set xdata time
set format x "%H:%M"
set timefmt "%s"
plot $Data u (Hours24($1)):2 w boxes fill solid 1.0 fc "web-green" notitle
### end of code
假设我现在已经理解了您的意思,那么您可以简单地用方框绘制数据
。无需提取最大值。较高的盒子将覆盖较短的盒子

代码:

### timestamp plot for 24h with extraction of daily maximum 
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

# extract the maximum of each day and store it in an array
array DailyMax[NoOfDays]
DayPrev = NaN
set table $Dummy
   plot $Data u (DayPrev!=DayNo($1) ? Comp=$2 : 0, DayPrev=DayNo($1), \
       $2>=Comp ? (Comp=$2, DailyMax[DayNo($1)+1]=Comp) : 0) with table
unset table
print DailyMax

# create label with max values
do for [i=1:NoOfDays] {
    set label i at graph 0.05, 0.95-i*0.05 tc lt i-1 sprintf("Max of day %d: %.1f\n", i, DailyMax[i]) 
}

set xdata time
set format x "%H:%M"
set timefmt "%s"

plot $Data u (Hours24($1)):2:(DayNo($1)) w l lc var notitle
### end of code
### timestamp plot plot maximum within every 5 min interval over all days
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

set xdata time
set format x "%H:%M"
set timefmt "%s"
plot $Data u (Hours24($1)):2 w boxes fill solid 1.0 fc "web-green" notitle
### end of code
结果:

### timestamp plot for 24h with extraction of daily maximum 
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

# extract the maximum of each day and store it in an array
array DailyMax[NoOfDays]
DayPrev = NaN
set table $Dummy
   plot $Data u (DayPrev!=DayNo($1) ? Comp=$2 : 0, DayPrev=DayNo($1), \
       $2>=Comp ? (Comp=$2, DailyMax[DayNo($1)+1]=Comp) : 0) with table
unset table
print DailyMax

# create label with max values
do for [i=1:NoOfDays] {
    set label i at graph 0.05, 0.95-i*0.05 tc lt i-1 sprintf("Max of day %d: %.1f\n", i, DailyMax[i]) 
}

set xdata time
set format x "%H:%M"
set timefmt "%s"

plot $Data u (Hours24($1)):2:(DayNo($1)) w l lc var notitle
### end of code
### timestamp plot plot maximum within every 5 min interval over all days
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

set xdata time
set format x "%H:%M"
set timefmt "%s"
plot $Data u (Hours24($1)):2 w boxes fill solid 1.0 fc "web-green" notitle
### end of code

添加2:

### timestamp plot for 24h with extraction of daily maximum 
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

# extract the maximum of each day and store it in an array
array DailyMax[NoOfDays]
DayPrev = NaN
set table $Dummy
   plot $Data u (DayPrev!=DayNo($1) ? Comp=$2 : 0, DayPrev=DayNo($1), \
       $2>=Comp ? (Comp=$2, DailyMax[DayNo($1)+1]=Comp) : 0) with table
unset table
print DailyMax

# create label with max values
do for [i=1:NoOfDays] {
    set label i at graph 0.05, 0.95-i*0.05 tc lt i-1 sprintf("Max of day %d: %.1f\n", i, DailyMax[i]) 
}

set xdata time
set format x "%H:%M"
set timefmt "%s"

plot $Data u (Hours24($1)):2:(DayNo($1)) w l lc var notitle
### end of code
### timestamp plot plot maximum within every 5 min interval over all days
reset session
set colorsequence classic

# define some values and functions
StartTime = strptime("%d.%m.%Y %H:%M:%S", "29.08.2019 00:00:00")
NoOfDays = 5
Hours24(n) = int(n-StartTime)%86400
DayNo(n) = int((n-StartTime)/86400)

# generate some dummy data
set print $Data
    t(n) = StartTime + n*5*60
    do for [i=0:NoOfDays-1] {
        A = rand(0)*70+30
        p(n) = A*exp(-(n%288-144-rand(0)*10)**2/(720+rand(0)*2000))
        do for [j=0:287] {
            print sprintf("%.0f\t%f",t(i*288+j),p(i*288+j))
        }
    }
set print

set xdata time
set format x "%H:%M"
set timefmt "%s"
plot $Data u (Hours24($1)):2 w boxes fill solid 1.0 fc "web-green" notitle
### end of code
现在,我想我们越来越近了。我修改了代码。您可以输入要绘制的数据的时间范围。您需要跳过数据顶部的7行,因为它们不是以注释字符开头的,例如
#
。检查下面的代码

这是一个简单得多的代码,然而,让我困惑的是,在plot命令中不能直接使用函数
Hours24(n)
。它会在00:00给你一个高峰。我还不明白为什么。 如果交换最后两行,可以测试它

set xrange[0:86400]
plot $Data u 1:2 w boxes fill solid 1.0 fc "web-blue" notitle

代码:(为自动全时范围编辑)

时间戳图;将天数的值放在彼此的顶部 重置会话 文件='log-pv-20190607-20190811.csv' #自动开始时间,结束时间 #开始时间总是从00:00:00开始 统计文件u 1跳过7 nooutput StartTime=strTime(“%d.%m.%Y”,strTime(“%d.%m.%Y”,STATS_min)) EndTime=STATS\u max #或者,手动设置开始时间、结束时间 #StartTime=strTime(“%d.%m.%Y”,“2019年8月8日”) #EndTime=strtime(“%d.%m.%Y%H:%m:%S”,“11.08.2019 23:59:59”) 打印“开始时间:”,strftime(“%d.%m.%Y%H:%m:%S”,开始时间) 打印“结束时间:”,strftime(“%d.%m.%Y%H:%m:%S”,结束时间) 小时24(n)=(nEndTime)?“”:(int((n-StartTime))%86400) #这一额外的绘图到$Data步骤似乎是必要的, #因为如果你直接通过 #打印文件u(小时24($1)):4个跳过7个w框填充实心1.0 fc“web blue”notitle #你会在00:00得到一些价值,我不明白。虫子? 设置表$Data 打印文件u(小时24($1)):4跳过7 w表格 未设置的表 设置扩展数据时间 设置格式x“%H:%M” 设置时间表“%s” 设置Y范围[0:] 设置X范围[0:86400] 绘图$Data u 1:2跳过7 w框填充实心1.0 fc“web蓝色”notitle ###代码结束