【问题标题】:Gnuplot: replot inside do forGnuplot:在内部重新绘制
【发布时间】:2020-12-13 11:43:10
【问题描述】:

在重新绘制的 3 个版本中,只有 2 个可以按需要工作:

plot sin(x)
replot sin(x)/2
replot sin(x)/3
replot sin(x)/4

在每个命令之后,相应的曲线将添加到绘图中 - 正如预期的那样。

plot sin(x)
replot for [i=2:4] sin(x)/i

给出相同的(最终)结果(图中有 4 条曲线),但是

plot sin(x)
do for [i=2:4] {replot sin(x)/i}

导致绘图只有第一条和第四条曲线,尽管键中写了 4 条线。这是一个“功能”还是(如何)我可以在 do-for-version 中获得 4 条曲线(因为我必须在 do-clause 中做一些额外的事情)?

[gnuplot 5.2 (8),Ubuntu MATE 20.04]

【问题讨论】:

  • 您可以像这样plot for [i=1:4] sin(x)/i 绘制所有 4 条曲线。在为什么需要do for 循环之间,您还需要做什么?
  • 除了一些其他的东西(使用'i'的计算)我想 - 说非常简化 - 还要画 cos(x)/i。使用 "replot for [i=2:4] sin(x)/i ; replot for [i=2:4] cos(x)/i" 会产生错误的顺序,因为我想要 sin(x), cos(x ), sin(x)/2, cos(x/2, ...
  • 实际上在上一个示例中,sin(x)/4 的 3 条曲线将相互重叠。如果您在 gnuplot 控制台中输入 show plot,您将获得 last plot command was: plot sin(x), sin(x)/i, sin(x)/i, sin(x)/i 和最后一个 replot 命令 i=4。我想这就是replot 的工作方式。它不会绘制sin(x), sin(x)/2, sin(x)/3, sin(x)/4
  • 如果订单很重要,例如对于交替的 sin(x)、cos(x) 示例,您可以使用:plot for [i=1:4] for [b=0:1] b?sin(x)/i:cos(x)/i title sprintf("%s(x)/%d",b?'sin':'cos',i)。但也许您可以编辑问题并更详细地描述您的最终目标。
  • “我想这就是 replot 的工作方式。” - 看起来像,是的。所以我找到了一个创建宏的解决方案(我将在几秒钟内发布)。 (虽然这在我的实际问题中并不像这里的这个简单示例那样好。)

标签: for-loop gnuplot


【解决方案1】:

我认为表达你最初尝试做的语法是这样的:

do for [i=1:4] {
    # ... do some stuff ...  #
    plot for [j=1:i] sin(x)/j title sprintf("sin(x)/%d",j)
}

replot 真的不是你想要的这种东西。

【讨论】:

    【解决方案2】:

    其他方式

    还有一种方法可以在循环中使用eval命令,如下。

    plot sin(x), cos(x)
    do for [i=2:4] {
      # some calculations involving i
      command = sprintf("replot sin(x)/%i, cos(x)/%i", i, i)
      eval command
      # maybe some more stuff involving i
    }
    

    【讨论】:

    • 这是将正确数字输入i 的可行方法。哈,我记得我在 StackOveflow 上的第一个问题可能是关于在 Python 中使用 eval,我得到了 5 票反对,因为他们说“永远不要使用 eval()。Eval 是邪恶的。嗯,...
    【解决方案3】:

    因此,在 do-for-loop 中使用 replot 的一种方法是创建一个宏:

    plot sin(x), cos(x)
    txt = ""
    do for [i=2:4] {
      # some calculations involving i
      txt = txt . "replot sin(x)/" . i . ", cos(x)/" . i . " ; "
      # maybe some more stuff involving i
    }
    @txt
    

    【讨论】:

    • 好的,很好......但是“你需要在 do-for 循环中做的其他事情”在哪里?使用我在上面的 cmets 中发布的代码,您会得到相同的结果...
    • 我编辑了我的答案并添加了“其他内容”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多