gnuplot bashshell图中的几个曲线和拟合曲线



这是我之前的问题的后续:"gnuplot bashshell在一个窗口中绘制多条曲线",Christoph对此提供了帮助。然而,我简化了我的问题,假设我可以自己从那里前进。不用说,我不能!:(我真正想在一个图框中绘制的是一组数据文件,每个数据文件都有一条曲线(指数函数)。不幸的是,我一直使用gnuplot 4.2,它不允许我在循环和迭代中使用。)。如果有任何建议,我将不胜感激。下面的bash script将九条曲线及其拟合线打印在单独的文件中。

#!/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

在编写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文件并删除最后一个逗号。

对不起,我没有太多时间,但我想帮你。。。

如果你想让@swerber的代码循环,你可以这样做:

#!/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 ...

最新更新