2014-09-11 51 views
1

我想在不同的文件上擬合曲線,讓它們在單個png中。
我使用下面的代碼:如何gnuplot - 在png終端中重新繪製兩個不同的等式?

set terminal png enhanced font arial 14 size 800,600 
set key outside horizontal left 
f(x) = a*x**b 
b = 1 
a = 10000 
fit f(x) 'a.txt' via a,b 
plot 'a.txt' with dots lc rgb"red" title ' ', \ 
f(x) with lines lc rgb"red" title sprintf('Curve Equation: f(x) = %.2f·x^{%.2f}', a, b) 


f1(x) = c*exp(d*x) 
d = -1 
c = 10000 
fit f1(x) 'b.txt' via c,d 
plot 'b.txt' with dots lc rgb"red" title ' ', \ 
f1(x) with lines lc rgb"blue" title sprintf('Curve Equation: f1(x) = %.2f·e^{-.%.2f.x}', c, d) 

replot 

unset output 
exit gnuplot; 

什麼可能在此代碼丟失。

回答

1

使用replot在寫入文件時通常是不好的選擇。

基本上,你有兩個選擇:

  1. 寫在一個單一的plot命令的所有情節的信息(我離開了fit東西的清晰度):

    set terminal pngcairo enhanced font arial 14 size 800,600 
    set output 'output.png' 
    # do some fitting 
    
    set style data dots 
    set style function lines 
    plot 'a.txt' lc rgb "red" title ' ', \ 
        f(x) lc rgb "red" title sprintf('Curve Equation: f(x) = %.2f·x^{%.2f}', a, b), \ 
        'b.txt' lc rgb "red" title ' ', \ 
        f1(x) lc rgb"blue" title sprintf('Curve Equation: f1(x) = %.2f·e^{-.%.2f.x}', c, d) 
    
  2. 如果你想分開兩個繪圖塊你需要一些tricking不同的終端,使用replot ...除了第一個繪圖塊,你必須設置png終端和輸出文件只在最後replot

    set style data dots 
    set style function lines 
    
    set terminal unknown 
    # do fitting of f(x) 
    plot 'a.txt' lc rgb "red" title ' ', \ 
        f(x) lc rgb "red" title sprintf('Curve Equation: f(x) = %.2f·x^{%.2f}', a, b) 
    
    # do fitting of f1(x) 
    set terminal pngcairo enhanced font arial 14 size 800,600 
    set output 'output.png' 
    replot 'b.txt' lc rgb "red" title ' ', \ 
        f1(x) lc rgb"blue" title sprintf('Curve Equation: f1(x) = %.2f·e^{-.%.2f.x}', c, d) 
    
    unset output 
    
  3. 這是2對您要在腳本的開始到指定最後終端的情況下的變體。您可以保存當前的終端與set terminal push後來與set terminal pop恢復它:

    set terminal pngcairo enhanced font arial 14 size 800,600 
    output_file = 'output.png' 
    set style data dots 
    set style function lines 
    
    set terminal push 
    set terminal unknown 
    # do fitting of f(x) 
    plot 'a.txt' lc rgb "red" title ' ', \ 
        f(x) lc rgb "red" title sprintf('Curve Equation: f(x) = %.2f·x^{%.2f}', a, b) 
    
    # do fitting of f1(x) 
    set terminal pop 
    set output output_file 
    replot 'b.txt' lc rgb "red" title ' ', \ 
        f1(x) lc rgb"blue" title sprintf('Curve Equation: f1(x) = %.2f·e^{-.%.2f.x}', c, d) 
    
    unset output 
    
+0

**選項:2 **是一個更好的辦法,是有益的。 – BioDeveloper 2014-09-11 10:55:27

+1

我添加了一個類似於2的第三個選項,但允許您在開始時設置最終的終端。 – Christoph 2014-09-11 11:08:50