Gnuplot按数据值设不同颜色时,平滑曲线功能异常
解决Gnuplot平滑温度曲线后颜色区分失效及报错问题
问题背景
原始Gnuplot配置可实现温度曲线按20℃分界着色(低于20℃为蓝色,≥20℃为红色),但为温度曲线添加smooth bezier平滑后,颜色区分消失,同时出现如下报错:
"minimon.gp" line 24: warning: extra columns ignored by smoothing option
"minimon.gp" line 29: warning: extra columns ignored by smoothing option
"minimon.gp" line 30: warning: extra columns ignored by smoothing option
"minimon.gp" line 30: warning: extra columns ignored by smoothing option
"minimon.gp" line 33: internal error : STRING operator applied to undefined or non-STRING variable
原因分析
Gnuplot的smooth操作默认仅处理x轴和y轴数据列,会忽略用来指定颜色的额外列,导致颜色变量丢失,进而触发报错。
解决方法
通过嵌套plot命令,先对温度数据执行平滑处理,再基于平滑后的温度值判断颜色。修改后的完整配置代码如下:
set datafile separator "," set title "CO2, Temp" set key bottom right set linetype 11 linecolor rgb "red" set linetype 12 linecolor rgb "blue" unset grid set grid x set grid y2 set ylabel "ppm" set y2label "°C" set xdata time set timefmt "%Y-%m-%d %H:%M:%S" set format x "%H:%M" set y2tics set y2range [15:30] # range for temp plot "minimonlog.csv" using 1:2 with lines smooth bezier axes x1y1 title "Co2", \ "<plot 'minimonlog.csv' using 1:3 smooth bezier" using 1:2:($2 < 20 ? 12 : 11) linecolor variable with lines axes x1y2 title "Temp" done = 0 bind all 'd' 'done = 1' while(!done) { replot pause 5 # waiting time in seconds }
代码说明
- 子命令
<plot 'minimonlog.csv' using 1:3 smooth bezier:先对原始温度数据执行平滑处理,输出包含时间(x轴)和平滑后温度值(y轴)的临时数据集 - 外层plot命令:基于临时数据集,用平滑后的温度值(
$2)判断颜色,实现平滑曲线的温度分界着色
内容的提问来源于stack exchange,提问作者Ocean
相关产品推荐
相关产品推荐

