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

如何基于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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 12:31:02