gnuplot识别%Y-%m-%d %H:%M:%S日期格式异常问题求助
问题与解决方案
问题场景
测试数据test.log每行包含日期、时间、数值三部分,用空格分隔:
2024-08-22 13:00:02 9 2024-08-22 13:10:03 20 2024-08-22 13:20:03 20 2024-08-22 13:30:03 9 2024-08-22 13:40:03 9
执行gnuplot脚本后出现Warning: empty y range [13:13],说明程序错误将时间字段的小时部分(如13)当作y轴数据,未读取第三列的目标数值。
原因
gnuplot默认按单个空格分割数据列,因此每行被拆分为3列:
- 日期(如
2024-08-22) - 时间(如
13:00:02) - 目标数值(如
9)
原脚本中plot "test.log" using 1:2实际取第一列(日期)和第二列(时间)作为x、y轴数据,完全偏离了预期逻辑。
最优解决方案
无需修改数据文件,只需调整plot命令,将前两列合并为完整时间字符串并转换为时间戳,同时指定第三列为y轴数据:
修改后的完整脚本
set terminal x11 1 set timefmt "%Y-%m-%d %H:%M:%S" set xdata time set xtics rotate by 60 offset -3.8,-7.8 set bmargin 15 set format x "%Y-%m-%d %H:%M:%S" set ylabel "test Value (Bq/m3)" plot "test.log" using (strptime(timefmt, strcol(1)." ".strcol(2))):3 title 'test measurement in Bq/m^3 every 10 minutes' with lines pause 60
原理说明
strcol(n):读取第n列的原始字符串内容strcol(1)." ".strcol(2):将日期和时间列拼接为符合timefmt格式的完整时间字符串strptime(timefmt, <字符串>):按照预设格式将字符串转换为gnuplot可识别的时间戳,作为x轴数据:3:明确指定第三列作为y轴的数值数据
内容的提问来源于stack exchange,提问作者Codizon
相关产品推荐
相关产品推荐

