Gnuplot技术实现:如何在箱线图上方展示时间序列?
Gnuplot:箱线图叠加时间序列及时间刻度构建方案
需求明确
- 参考数据(norm区块)以箱线图展示,统一颜色
- cross、long区块的时间序列以点连线形式叠加在对应箱线上方
- 相邻指标的图形不重叠,每个箱线对应其指标的时间序列
- cross与long各用单一颜色区分
核心实现要点
- 时间刻度构建:将原始
YYYYMMDD格式日期转换为gnuplot可识别的时间戳,实现时间的数值化映射 - 区间偏移布局:为每个指标(RH/LH/RA/LA)分配专属x轴子区间,避免相邻图形重叠
- 样式统一控制:箱线用固定灰色,cross用蓝色点线,long用橙色点线,保证视觉区分度
完整可运行代码
reset session # 定义数据区块和指标标签 ref_blocks = "norm" blocks = "cross long" labels = "RH LH RA LA" num_labels = words(labels) # 时间转换函数:把YYYYMMDD格式字符串转为gnuplot内部时间戳 date2time(s) = strptime("%Y%m%d", s) # 样式配置 set style fill solid 0.25 border -1 set style boxplot outliers pointtype 7 set style data boxplot set boxwidth 0.5 absolute # 缩小箱线宽度,为上方时间序列预留空间 # 颜色定义 color_box = "#888888" # 箱线统一灰色 color_cross = "#2196F3" # cross区块蓝色 color_long = "#FF9800" # long区块橙色 set title "dat -> ref" font "Arial,14" set xtics 1,1,num_labels scale 0 set ylabel "Value" font "Arial,12" # 计算每个指标的时间序列x区间范围(避免相邻重叠) x_min(i) = i - 0.3 x_max(i) = i + 0.3 # 先绘制箱线图 plot for [i=1:num_labels] $REF index "norm" \ using (i):(column(word(labels,i))) \ title word(labels,i) lc rgb color_box, \ # 叠加cross区块时间序列:将时间戳线性映射到对应指标的x子区间内 for [i=1:num_labels] $DAT index "cross" \ using (x_min(i) + (date2time(stringcolumn("date")) - GPVAL_DATA_X_MIN)/(GPVAL_DATA_X_MAX - GPVAL_DATA_X_MIN)*(x_max(i)-x_min(i))):(column(word(labels,i))) \ with linespoints pt 7 lc rgb color_cross notitle, \ # 叠加long区块时间序列 for [i=1:num_labels] $DAT index "long" \ using (x_min(i) + (date2time(stringcolumn("date")) - GPVAL_DATA_X_MIN)/(GPVAL_DATA_X_MAX - GPVAL_DATA_X_MIN)*(x_max(i)-x_min(i))):(column(word(labels,i))) \ with linespoints pt 5 lc rgb color_long notitle # 补全x轴指标标签 set xtics (word(labels,1) 1, word(labels,2) 2, word(labels,3) 3, word(labels,4) 4) scale 1 replot
关键逻辑说明
- 时间刻度处理:
date2time(s)将字符串日期转为gnuplot时间戳,让时间可以作为数值参与坐标计算;如果需要单独显示时间轴,可开启代码末尾注释的双x轴配置 - 无重叠布局:通过
x_min(i)和x_max(i)为每个指标划分独立的x区间,再将时间序列线性映射到该区间内,彻底避免相邻指标的图形重叠 - 视觉区分:箱线、cross、long分别用不同颜色和点型,保证两类时间序列和参考箱线的清晰区分
内容的提问来源于stack exchange,提问作者magfan
相关产品推荐
相关产品推荐

