如何基于xrange值动态控制gnuplot中linespoints的标签显隐?
动态控制Gnuplot标签的显示与隐藏
你可以通过两种方式实现基于diagram_range_in_days的标签动态显隐:
方法一:使用宏变量拼接绘图命令
提前根据数据范围判断生成标签绘图的代码片段,再拼接到主plot命令中:
# ----------------------------------------------------------------------------- # plot historical data # ----------------------------------------------------------------------------- if (exists("diagram_range_in_days")) { now = time(0) set xrange [now - (diagram_range_in_days * 24 * 60 * 60):now] if (diagram_range_in_days <=30) { set logscale y 3 } } # 初始化标签绘图命令为空字符串 label_cmd = "" # 当数据范围≤7天时,设置标签绘图代码 if (exists("diagram_range_in_days") && diagram_range_in_days <=7) { label_cmd = ", '' using 1:col_d:(sprintf(\"%'.0f\", column(col_d))) with labels center offset 0, 1 notitle" } # 执行plot命令,拼接标签部分 plot input_file using 1:column(col_a) with linespoints linestyle 5 title "aaa", \ input_file using 1:col_b with linespoints linestyle 6 title "bbb", \ input_file using 1:col_c with linespoints linestyle 1 title "ccc" \ @label_cmd
方法二:直接在plot命令中使用条件判断
利用Gnuplot的三元表达式,当不符合显示条件时返回NaN,让标签系列无输出:
# ----------------------------------------------------------------------------- # plot historical data # ----------------------------------------------------------------------------- if (exists("diagram_range_in_days")) { now = time(0) set xrange [now - (diagram_range_in_days * 24 * 60 * 60):now] if (diagram_range_in_days <=30) { set logscale y 3 } } plot input_file using 1:column(col_a) with linespoints linestyle 5 title "aaa", \ input_file using 1:col_b with linespoints linestyle 6 title "bbb", \ input_file using 1:col_c with linespoints linestyle 1 title "ccc", \ '' using 1:(exists("diagram_range_in_days") && diagram_range_in_days <=7 ? col_d : NaN):(sprintf("%'.0f", column(col_d))) with labels center offset 0,1 notitle
两种方法对比
- 方法一代码结构更清晰,便于后续扩展更多条件逻辑;
- 方法二更紧凑,适合简单的显隐判断场景。
内容的提问来源于stack exchange,提问作者zappee
相关产品推荐
相关产品推荐

