Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf

huangapple go评论88阅读模式
英文:

Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf

问题

我有以下带有时间戳的数据源。

2023-07-19 13:56:37 355
2023-07-19 13:56:41 7
2023-07-19 13:56:43 36
2023-07-19 13:56:52 14
2023-07-19 13:56:54 521
2023-07-19 13:57:09 1642
2023-07-19 13:57:14 512
2023-07-19 13:57:17 171
2023-07-19 13:57:19 407
2023-07-19 13:57:21 527

我创建了以下命令,但最大值和最小值没有显示在图表上。

reset
stats "source.txt" using 3 nooutput
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set format x "%d-%b\n%H:%M"
set label 1 sprintf("%.2f", STATS_min) center at first STATS_index_min,STATS_min point pt 7 ps 1 offset 0,-1.5
set label 2 sprintf("%.2f", STATS_max) center at first STATS_index_max,STATS_max point pt 7 ps 1 offset 0,1.5
plot "source.txt" u 1:3 with lines

请问你能帮忙绘制带有最大值和最小值标签的图表吗?

英文:

I have following data source with timestamps.

2023-07-19 13:56:37 355
2023-07-19 13:56:41 7
2023-07-19 13:56:43 36
2023-07-19 13:56:52 14
2023-07-19 13:56:54 521
2023-07-19 13:57:09 1642
2023-07-19 13:57:14 512
2023-07-19 13:57:17 171
2023-07-19 13:57:19 407
2023-07-19 13:57:21 527

And I create following commands, but max/min did not appear on graf.

reset
stats "source.txt" using 3 nooutput
set xdata time
set timefmt "%Y-%m-%d %H:%M:%S"
set format x "%d-%b\n%H:%M"
set label 1 sprintf("%.2f", STATS_min) center at first STATS_index_min,STATS_min point pt 7 ps 1 offset 0,-1.5
set label 2 sprintf("%.2f", STATS_max) center at first STATS_index_max,STATS_max point pt 7 ps 1 offset 0,1.5
plot "source.txt" u 1:3 with lines

Could you please help plot graf with max/min labels.

答案1

得分: 1

你正在尝试将索引设置为标签的X坐标。你可以使用带有标签的绘图代替:

plot "source.txt" u 1:3 with lines notitle, \
'' u 1:($3+50):($0==STATS_index_min ? sprintf("%.2f", STATS_min) : '') w labels notitle, \
'' u 1:($3+50):($0==STATS_index_max ? sprintf("%.2f", STATS_max) : '') w labels notitle

Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf

英文:

You are trying to set indexes as the X coordinates of labels. You can use plot with labels instead:

plot "source.txt" u 1:3 with lines notitle, \
'' u 1:($3+50):($0==STATS_index_min ? sprintf("%.2f", STATS_min) : '') w labels notitle, \
'' u 1:($3+50):($0==STATS_index_max ? sprintf("%.2f", STATS_max) : '') w labels notitle

Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf

答案2

得分: 1

你看不到最小/最大点的原因是因为你只在第3列上执行了stats命令。gnuplot默认假设x值在伪列0中(基本上是从零开始的行索引,请查看help pseudocolumns)。
此外,如果在set xdata time时执行stats命令,你将收到错误消息“Stats command not available in timedata mode”。
因此,我建议你使用新的时间数据语法,不使用set xdata time,而是使用timecolumn()函数(请查看help timecolumn)。

现在你可以使用带有时间格式的stats命令,并在STATS_pos_min_ySTATS_pos_max_y中找到x位置。

请查看以下修改后的脚本:

脚本:

### timedate min/max labels
reset session

$Data <<EOD
2023-07-19 13:56:37 355
2023-07-19 13:56:41 7
2023-07-19 13:56:43 36
2023-07-19 13:56:52 14
2023-07-19 13:56:54 521
2023-07-19 13:57:09 1642
2023-07-19 13:57:14 512
2023-07-19 13:57:17 171
2023-07-19 13:57:19 407
2023-07-19 13:57:21 527
EOD

myFmt = "%Y-%m-%d %H:%M:%S";

stats $Data u (timecolumn(1,myFmt)):3 nooutput
set format x "%d-%b\n%H:%M" timedate
set label 1 sprintf("%.2f", STATS_min_y) center at first STATS_pos_min_y ,STATS_min_y point pt 7 ps 1 lc "red" offset 0,1
set label 2 sprintf("%.2f", STATS_max_y) center at first STATS_pos_max_y ,STATS_max_y point pt 7 ps 1 lc "red" offset 0,1
set key noautotitle

plot $Data u (timecolumn(1,myFmt)):3 w lines
### end of script

结果:

Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf

英文:

The reason why you don't see your min/max points is because you do stats just on column 3. gnuplot implicitly assumes that the x-values are in pseudocolumn 0 (basically the zero-based line index, check help pseudocolumns).
Furthermore, if you do stats when set xdata time you will get an error message &quot;Stats command not available in timedata mode&quot;.
So, I would use the newer syntax for time data without set xdata time but with timecolumn() (check help timecolumn).

Now you can use stats with timeformat and the x-positions you will find in STATS_pos_min_y and STATS_pos_max_y.

Check the following modified script:

Script:

### timedate min/max labels
reset session

$Data &lt;&lt;EOD
2023-07-19 13:56:37 355
2023-07-19 13:56:41 7
2023-07-19 13:56:43 36
2023-07-19 13:56:52 14
2023-07-19 13:56:54 521
2023-07-19 13:57:09 1642
2023-07-19 13:57:14 512
2023-07-19 13:57:17 171
2023-07-19 13:57:19 407
2023-07-19 13:57:21 527
EOD

myFmt = &quot;%Y-%m-%d %H:%M:%S&quot;

stats $Data u (timecolumn(1,myFmt)):3 nooutput
set format x &quot;%d-%b\n%H:%M&quot; timedate
set label 1 sprintf(&quot;%.2f&quot;, STATS_min_y) center at first STATS_pos_min_y ,STATS_min_y point pt 7 ps 1 lc &quot;red&quot; offset 0,1
set label 2 sprintf(&quot;%.2f&quot;, STATS_max_y) center at first STATS_pos_max_y ,STATS_max_y point pt 7 ps 1 lc &quot;red&quot; offset 0,1
set key noautotitle

plot $Data u (timecolumn(1,myFmt)):3 w lines
### end of script

Result:

Gnuplot. How to plot graf with X time, Y data and find Min and Max on graf

huangapple
  • 本文由 发表于 2023年7月27日 15:24:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定