英文:
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
英文:
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
答案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_y
和STATS_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
结果:
英文:
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 "Stats command not available in timedata mode"
.
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 <<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
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论