You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gnuplot时间范围显示异常求助:血糖数据绘图不符合预期

问题:Gnuplot生成血糖曲线图表异常

我的血糖时间数据如下:

2025-02-08  07:12   3.8
2025-02-08  12:15   4.7
2025-02-08  17:30     7
2025-02-08  19:40   6.3

使用的Gnuplot代码片段:

reset

set title "血糖水平"
set xlabel "日期时间"
set ylabel "血糖浓度 (mmol)"

set xtics rotate by -45

set grid ytics
set grid xtics
set grid

set term pngcairo size 1500,500

set xdata time
set timefmt '%Y-%m-%d %H:%M'
set format x "%H:%M\n%b,%d"

set xtics 24*60*60

plot data using 1:3 with linespoints

期望生成对应日期时间的血糖水平曲线,但图表显示异常。


问题原因及修复方案

问题1:x轴刻度间隔设置不合理

当前set xtics 24*60*60设置的是24小时的刻度间隔,但所有数据都集中在同一天,导致x轴仅显示一个刻度点,图表布局异常。

问题2:时间数据读取错误

数据中的日期(第1列)和时间(第2列)是分开的两列,但timefmt定义的是包含日期和时间的完整格式,直接用using 1:3只会读取第1列的日期,忽略时间部分,导致所有数据点被映射到同一天的起始时刻,曲线无法正常展示时间变化。


修复后的完整Gnuplot代码

reset

set title "血糖水平"
set xlabel "日期时间"
set ylabel "血糖浓度 (mmol)"

set xtics rotate by -45
set grid

set term pngcairo size 1500,500

set xdata time
set timefmt '%Y-%m-%d %H:%M'
set format x "%H:%M\n%m-%d"  # 改用更简洁的日期格式,避免缩写歧义

# 调整x轴刻度间隔为3小时,适配当天的数据分布
set xtics 3*60*60
# 强制显示所有数据点对应的时间刻度(可选)
set xtics add (strftime("%H:%M", strptime("%Y-%m-%d %H:%M", "2025-02-08 07:12")) strptime("%Y-%m-%d %H:%M", "2025-02-08 07:12"), \
               strftime("%H:%M", strptime("%Y-%m-%d %H:%M", "2025-02-08 12:15")) strptime("%Y-%m-%d %H:%M", "2025-02-08 12:15"), \
               strftime("%H:%M", strptime("%Y-%m-%d %H:%M", "2025-02-08 17:30")) strptime("%Y-%m-%d %H:%M", "2025-02-08 17:30"), \
               strftime("%H:%M", strptime("%Y-%m-%d %H:%M", "2025-02-08 19:40")) strptime("%Y-%m-%d %H:%M", "2025-02-08 19:40"))

# 合并日期和时间列,正确读取完整时间数据
plot "data" using (strptime("%Y-%m-%d %H:%M", sprintf("%s %s", $1, $2))):3 with linespoints pt 7 ps 1.5 lc rgb "#2E86AB" title "血糖值"

关键修改点

  1. 调整刻度间隔:将set xtics 24*60*60改为3*60*60(3小时),适配单日数据的时间跨度。
  2. 合并时间列:通过sprintf("%s %s", $1, $2)将日期和时间拼接为完整字符串,再用strptime转换为时间戳,确保x轴正确映射每个数据点的时间。
  3. 优化刻度显示:可选添加数据点对应的精确时间刻度,让图表更直观。

内容的提问来源于stack exchange,提问作者user11672821

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 09:56:20