【问题标题】:Generate titles from variables in Gnuplot从 Gnuplot 中的变量生成标题
【发布时间】:2014-12-11 15:27:04
【问题描述】:

我正在尝试在 Gnuplot 中自动生成标题。我有一个数组(titleprefix)和另一组变量(a1,a2,a3 ...),标题是前缀后跟斜率(a)。我试过这个:

title(n) = sprintf("word(titleprefix,n).sprintf(\" Slope = %.3f\",%f)",a."n"+0)

这当然没有用。错误是:

在需要数字表达式的地方找到非数字字符串。

知道如何解决这个问题吗?

最后,我想绘制几条这样的曲线:

plot f1 w l t title(1), \
     f2 w l t title(2), \
     f3 w l t title(3)

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    执行此类操作的一种选择是在此类函数中构建完整的 set title 命令,然后在此函数中构建 eval

    在您的情况下,需要花点心思才能使引号正确,以便变量名正确:

    titleprefix="first second third"
    a1 = 1.111
    a2 = 2.222
    a3 = 3.333
    title(n) = 'set title "'.word(titleprefix, n).': ".sprintf("Slope = %.3f", a'.n.')'
    
    set multiplot layout 1,3
    eval(title(1))
    plot x
    eval(title(2))
    plot 2*x
    eval(title(3))
    plot 3*x
    unset multiplot
    

    或者,您可以在定义或计算变量时构建格式化变量值的单词列表。

    a1 = 1.111111
    a2 = 2.222222
    a3 = 3.333333
    tmp = 'a = ""'
    do for [i=1:3] { tmp = tmp.".sprintf('%.3f ', a".i.")" }
    eval(tmp)
    
    plot for [i=1:3] i*x title word(a, i)
    

    【讨论】:

    • 这很有帮助,但我没有使用多图。如果你想绘制多条曲线,你会怎么做。 plot file1 w l t title(1),file2 w l t title(2)
    • 一个经典案例,其中一个最小但完整的示例脚本会有所帮助;)所以,要获取其余信息:您从哪里获取变量值?
    • 很难在评论中输入脚本。问题是如果您不使用多图,您将如何修改它。例如,如果您想执行plot file1 w l t title(1),file2 w l t title(2), file2 w l t title(3) 之类的操作,您将如何评估标题命令。其余的与您的工作示例相同。
    • 您可以随时编辑您的问题以包含更多信息。基本上,您可以在答案末尾遵循我的建议:您可以生成带有预格式化变量值的单词列表。我会在一分钟内更新我的答案。
    • 非常感谢。我会努力,看看我能不能让它工作。顺便说一句,我在尝试您的示例时确实有错误。版本 4.6 补丁级别 4。gnuplot> do for [i=1:3] { tmp = tmp."sprintf('%f ', a".i.")" } gnuplot> eval(tmp) gnuplot> a = sprintf('%f ', a1)sprintf('%f ', a2)sprintf('%f ', a3)sprintf('%f ', a1)sprintf('%f ', a2)sprintf('%f ', a3)sprintf('%f ', a1)sprintf('%f ', a2)sprintf('%f ', a3) ^ ';' expected
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    相关资源
    最近更新 更多