【问题标题】:Plotting a function directly from a text file直接从文本文件中绘制函数
【发布时间】:2013-02-21 16:40:46
【问题描述】:

有没有办法根据文本文件中的值绘制函数?

我知道如何在 gnuplot 中定义一个函数然后绘制它,但这不是我需要的。 我有一个表,其中包含定期更新的函数的常量。当这个更新发生时,我希望能够运行一个用这条新曲线绘制图形的脚本。由于要绘制的数字很少,我想自动化该过程。

这是一个带有常量的示例表:

location a  b  c
1        1  3  4
2

我看到有两种方法可以解决这个问题,但我不知道它们是否以及如何实现。

  1. 然后我可以使用 awk 生成字符串:f(x)=1(x)**2+3(x)+4,将其写入文件并以某种方式让 gnuplot 读取这个新文件并在某个 x 范围内绘图。
  2. 或在 gnuplot 中使用 awk,例如 f(x) = awk /1/ {print "f(x)="$2 等,或直接在 plot 命令中使用 awk。

无论如何,我被困在网上没有找到解决此问题的方法,您有什么建议吗?

【问题讨论】:

  • 您为什么要寻找单线解决方案?
  • 当我使用gnuplot 时,我总是从Perl 内部调用它。如果出于某种原因这不是一个选项,我建议您从 gnuplot 脚本内部调用 awk。请参阅 herehere 了解实现此目的的方法。
  • 我认为问题的描述不是很精确,具体是如何解释示例文本文件。

标签: awk gnuplot


【解决方案1】:

另一种可能有一个通用的版本,您可以执行以下操作:

假设,参数存储在文件parameters.dat 中,第一行包含变量名称和所有其他参数集,例如

location a b c
1        1 3 4

脚本文件如下所示:

file = 'parameters.dat'
par_names = system('head -1 '.file)
par_cnt = words(par_names)

# which parameter set to choose
par_line_num = 2
# select the respective string
par_line = system(sprintf('head -%d ', par_line_num).file.' | tail -1')
par_string = ''
do for [i=1:par_cnt] {
  eval(word(par_names, i).' = '.word(par_line, i))
}
f(x) = a*x**2 + b*x + c

plot f(x) title sprintf('location = %d', location)

【讨论】:

    【解决方案2】:

    这个问题 (gnuplot store one number from data file into variable) 在第一个答案中对我有一些提示。

    在我的例子中,我有一个包含抛物线参数的文件。我已将参数保存在 gnuplot 变量中。然后我绘制包含每个时间步的参数变量的函数。

    #!/usr/bin/gnuplot
    
    datafile = "parabola.txt"
    
    set terminal pngcairo size 1000,500
    set xrange [-100:100]
    set yrange [-100:100]
    titletext(timepar, apar, cpar) = sprintf("In timestep %d we have parameter a = %f, parameter c = %f", timepar, apar, cpar)
    
    do for [step=1:400] {
      set output sprintf("parabola%04d.png", step)
    
      # read parameters from file, where the first line is the header, thus the +1
      a=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $1}' " . datafile)
      c=system("awk '{ if (NR == " . step . "+1) printf \"%f\", $2}' " . datafile)
    
      # convert parameters to numeric format
      a=a+0.
      c=c+0.
    
      set title titletext(step, a, c)
    
      plot   c+a*x**2
    }
    

    这给出了一系列名为 parabola0001.png 的 png 文件, 抛物线0002.png, 抛物线0003.png, …,每个都显示一个抛物线,其参数从名为parabola.txt 的文件中读取。标题包含给定时间步的参数。

    要了解 gnuplot system() 函数,您必须知道:

    • gnuplot 不解析双引号内的内容
    • 点用于连接 gnuplot 中的字符串
    • 必须对 awk printf 命令的双引号进行转义,以便对 gnuplot 解析器隐藏它们

    要测试这个 gnuplot 脚本,请将其保存到具有任意名称的文件中,例如parabolaplot.gplot 并使其可执行 (chmad a+x parabolaplot.gplot)。 parabola.txt 文件可以用

    创建

    awk 'BEGIN {for (i=1; i<=1000; i++) printf "%f\t%f\n", i/200, i/100}' > parabola.txt

    【讨论】:

      【解决方案3】:
      awk '/1/ {print "plot "$2"*x**2+"$3"*x+"$4}' | gnuplot -persist
      

      将选择线并绘制它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多