【问题标题】:gnuplot bashshell several curves and fitted curves in one figgnuplot bashshell 几条曲线和拟合曲线在一张图中
【发布时间】:2014-10-03 03:22:48
【问题描述】:

这是我之前的问题的后续:"gnuplot bashshell to plot several curves in one window" Christoph 对此提供了帮助。但是,我已经简化了我的问题,假设我可以自己从那里前进。不用说,我做不到! :(我真正想在一个无花果框架中绘制的是一组数据文件,并且对于每个数据文件都有一条适合它的曲线(指数函数)。不幸的是,我被 gnuplot 4.2 卡住了,它不允许我使用 for 循环iterations。我将不胜感激任何建议。以下 bash 脚本在单独的文件中打印九条曲线及其拟合线。

#!/bin/bash

for Counter in {1..9}; do
FILE="dataFile"$Counter".data"
gnuplot <<EOF
set xlabel "k"
set ylabel "p(k)"
set term png
set output "${FILE}.png"
title_fexp(a,b) = sprintf('exp(x) = %.2f * e(%.2f)', a, b)
expon(x) = c * exp(-x*d)
fit [10:100] expon(x) '${FILE}' via c,d
plot [1:50] expon(x)t title_fexp(c,d), '${FILE}' 
EOF
done

【问题讨论】:

  • 你已经准备好所有的拟合参数了吗?然后就是将它们读入 gnuplot 的问题。如果它们在外部文件中,您可以使用外部命令读取它们。例如。您有一个 temp 文件,其中包含文本 12345,您可以使用 a=system("awk 'NR == 1 {print $1}' temp") 将其读取到 gnuplot。
  • 实际问题是什么?最终的情节应该是怎样的?
  • @Miguel 不,我没有单独文件中的拟合参数。您是否建议我先运行上面的代码估计拟合参数,将它们写入文件,然后再从该文件中读取它们进行绘图?
  • @MarkSetchell 我无法添加一个数字来说明我的意思,所以用文字来说:最终的图有来自数据文件的九条曲线,每条曲线都有一条适合它的线。喜欢这里第 7 节中的图片:link
  • 有机会升级到 4.6 版吗?然后你可以使用 gnuplot 的循环和gnuplot linear fit within for loop 的解决方案。

标签: bash loops gnuplot curve-fitting


【解决方案1】:

在编写 bash 脚本来生成 gnuplot 脚本之前,您应该考虑一下 gnuplot 脚本应该是什么样子。之后,您可以编写一个 bash/whatever 脚本来生成相同的 gnuplot 代码。

那么,你想要的是这样的:

set xlabel "k"
set ylabel "p(k)"
set term png
set output "MySingleFile.png"


# Note that I've added c and d to the declaration
expon(x, c, d) = c * exp(-x*d)

# This allows to store the fit parameters for each datafile separately
fit expon(x, c1, d1) "dataFile1.data" via c1, d1
fit expon(x, c2, d2) "dataFile2.data" via c2, d2
fit expon(x, c3, d3) "dataFile3.data" via c3, d3

# now plot it. Note: The backslash allows multi line commands
plot \
    "dataFile1.data" notitle pt 1, \
    expon(x, c1, d1) title sprintf('exp(x) = %.2f * e(%.2f)', c1, d1) lt 1,\
    "dataFile2.data" notitle pt 2, \
    expon(x, c2, d2) title  sprintf('exp(x) = %.2f * e(%.2f)', c2, d2) lt 2,\
    "dataFile3.data" notitle pt 3, \
    expon(x, c3, d3) title  sprintf('exp(x) = %.2f * e(%.2f)', c3, d3) lt 3


unset output

现在您可以根据自己的脚本编写一个 bash 脚本,该脚本会生成像我一样的代码。 但是,请考虑将 gnuplot 命令通过管道传输到单独的文件中,然后使用此文件作为参数调用 gnuplot。这允许调试 bash 脚本的输出。

请注意,命令图的最后一行末尾没有逗号。根据您的需要,您必须关心 bash 脚本,或者您只需编辑 gnuplot 文件并删除最后一个逗号。

【讨论】:

    【解决方案2】:

    对不起,我没有太多时间,但我想帮您一把……

    如果你想在@sweber 的代码中创建一个循环,你可以这样做:

    #!/bin/bash    
    { 
    cat<<EOF
    set xlabel "k"
    set ylabel "p(k)"
    set term png
    set output "MySingleFile.png"
    EOF
    
    for i in {1..3}
    do
       cat<<EOF
       fit ... something with $i ...
    EOF
    done
    
    for i in {1..3}
    do
       cat<<EOF
       plot ... something with $i ...
    EOF
    done } | gnuplot
    

    只需去掉末尾的| gnuplot 即可调试或查看生成的代码。是这样的:

    set xlabel "k"
    set ylabel "p(k)"
    set term png
    set output "MySingleFile.png"
    fit ... something with 1 ...
    fit ... something with 2 ...
    fit ... something with 3 ...
    plot ... something with 1 ...
    plot ... something with 2 ...
    plot ... something with 3 ...
    

    【讨论】:

    • 感谢@MarkStechell 非常有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多