如何在Gnuplot中条件式绘制贯穿全图的水平线?
Gnuplot 条件绘制贯穿绘图区的水平线
问题背景
现有如下Gnuplot代码:
set autoscale xfix set terminal qt size 1700, 650 stats "memory.log" prefix "A" set key left top Left title "\n".sprintf(' Time: %1.1f h', A_records/1800.0)."\n".sprintf(' Max: %d GB', A_max) font ',11' plot "memory.log" using (column(0)*2/3600):1 w lp notitle pt -1 lw 1
需要实现:仅当A_max(由stats命令统计得到的内存最大值)超过指定常量阈值时,绘制一条贯穿整个绘图区域的水平线,且无需依赖数据文件的额外列。
解决方案
方法1:在plot命令中直接条件绘制
通过plot命令的if条件判断,结合Gnuplot内置的x1/x2变量(对应绘图区x轴的左右边界),直接绘制水平线:
set autoscale xfix set terminal qt size 1700, 650 THRESHOLD = 8 # 自定义阈值,可根据需求修改 stats "memory.log" prefix "A" set key left top Left title "\n".sprintf(' Time: %1.1f h', A_records/1800.0)."\n".sprintf(' Max: %d GB', A_max) font ',11' plot "memory.log" using (column(0)*2/3600):1 w lp notitle pt -1 lw 1, \ if (A_max > THRESHOLD) [x1:x2] (THRESHOLD) w l lw 2 lc rgb "red" title sprintf('Threshold: %d GB', THRESHOLD)
[x1:x2]会自动获取当前绘图区的x轴范围,确保水平线贯穿整个区域(THRESHOLD)指定水平线的y轴位置,无需数据文件支持
方法2:使用set arrow条件创建水平线
利用set arrow命令创建无箭头的直线,通过graph坐标系统定位到绘图区的左右边界:
set autoscale xfix set terminal qt size 1700, 650 THRESHOLD = 8 # 自定义阈值,可根据需求修改 stats "memory.log" prefix "A" # 仅当最大值超过阈值时创建水平线 if (A_max > THRESHOLD) set arrow from graph 0, first THRESHOLD to graph 1, first THRESHOLD lw 2 lc rgb "red" nohead set key left top Left title "\n".sprintf(' Time: %1.1f h', A_records/1800.0)."\n".sprintf(' Max: %d GB', A_max) font ',11' plot "memory.log" using (column(0)*2/3600):1 w lp notitle pt -1 lw 1
graph 0和graph 1分别代表绘图区域的最左侧和最右侧first THRESHOLD将直线定位到y轴对应阈值的位置,nohead参数隐藏箭头,呈现为纯直线
内容的提问来源于stack exchange,提问作者Aleksander
相关产品推荐
相关产品推荐

