Gnuplot:禁用部分绘图命令

Gnuplot: Disable part of a plot command

假设我有以下简单的 gnuplot 文件:

set term pdf
set output "test.pdf"

plot\
    sin(x),\
    cos(x),\
    0.5

现在我只想临时注释cos(x),\行。

但是,使用注释字符 # 会导致错误的 gnuplot 命令。

我正在寻找无需移动代码行的解决方案。我只想使用我的编辑器的切换评论功能。

尽管 gnuplot 不直接支持您的要求,但您或许可以构建一个解决方法。 由于 gnuplot 不绘制未定义的值,这可能有效

identity = 0
#identity= 1
name=identity ? "cos(x)" : ''

plot sin(x), (1/identity)*cos(x) t name

根据您是打开还是关闭评论,变量 identity 的值将是 01
在第一种情况下(即 identity = 0),(1/identity)(因此 (1/identity)*cos(x))的结果是未定义的,gnuplot 不会绘制它。
删除上面示例中的注释后 identity 将等于 1。在这种情况下,(1/identity) 的结果是 1,因此,(1/identity)*cos(x) 等同于 cos(x)

为了显示正确的标题 (identity = 1) 或根本没有标题(如果 identity=0),我添加了行 name=identity ? "cos(x)" : ''

这是一种解决方法。您可以动态解析脚本以完全删除带有 # 的行。使用 grep:

grep -v "#" script | gnuplot

将成功解析:

plot\
    sin(x),\
    #cos(x),\
    0.5

其中 script 是包含上述代码的文件的名称。