在gnuplot中如何在同一张图上绘制多条拟合曲线?

3

我正在尝试使用简单的回归线作为 gnuplot 图表的基础,但无论我怎么做,都不能在一个图表上显示多条拟合线。这是可能的吗? 这是我的(当前的)gnuplot 程序....

set title "Foo" font "Arial Bold,14"
set term epscairo size 8,5
set style line 1 lw 0 pt 7 lc rgb "black"
set key outside
set pointsize .75
set ylabel "Y Range" font "Arial Bold"
set xlabel "X Range" font "Arial Bold"
set grid ytics
set yrange [-1:100]
set xrange [1:80]
set output 'graph.ps'

f1(x) = a1*x + b1
fit f1(x) "data/dvdate/1" using 2:3 via a1,b1

f2(x) = a1*x + b1
fit f2(x) "data/dvdate/2" using 2:3 via a1,b1

f3(x) = a1*x + b1
fit f3(x) "data/dvdate/3" using 2:3 via a1,b1

plot f1(x) title '# 1', f2(x) title '# 2', f3(x) title '# 3'

结果是我得到了一个 .ps 文件,其中包含一行,它不会与其他线叠加,并带有三个数据系列的标签。
2个回答

4

在每个拟合中,您需要使用不同的变量(a1,b1)。

f2(x) = a2*x + b2
fit f2(x) "data/dvdate/2" using 2:3 via a2,b2

谢谢。我想我还没有完全理解变量的使用方式。我假设每个适配都是独立的,从输入文件中读取/处理是一个离散事件,并且变量是/曾经是事件本地的。 - docSquale
另外,我有超过50行需要绘制。虽然我已经“发现”了循环,但我无法使fn(x)定义可循环,这意味着需要大量的复制/粘贴来处理我的数据。 :-( - docSquale
2
每个拟合都是独立的,但变量不是本地的 - 因此在您的第一个形式中,每个拟合都会覆盖先前的值。要在循环中生成50个,我会编写一个外部脚本(Python或您喜欢的任何语言)来生成gnuplot脚本。 - agentp

0

你可以使用 '!' 在 gnuplot 中调用外部程序,并且一个循环将在生成一个临时文件后立即加载它。

!rm filetoload
! for ((i=1;i<=50;i++)); do echo "f$i(x)=a$i*x+b$i; fit f$i(x) './file$i' u 1:2 via a$i, b$i">> filetoload;done
!cat filetoload
load "filetoload"

将生成:

f1(x)=a1*x+b1; fit f1(x) file1 u 1:2 via a1, b1
f2(x)=a2*x+b2; fit f2(x) file2 u 1:2 via a2, b2
f3(x)=a3*x+b3; fit f3(x) file3 u 1:2 via a3, b3
...

随心所欲地调整


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接