2015-12-03 40 views
2

使用某個程序(PersistenceLandscapes工具箱)我正在生成大量的腳本,從中我使用gnuplot生成了繪圖。我遍歷文件並使gnuplot顯示命令gnuplot gnuplotCommand.txt -p。我怎樣才能讓gnuplot以PNG或(最好是)EPS格式保存劇情? (我想避免與gnuplotCommand型腳本插手。)使GNUPLOT保存從shell中的繪圖

回答

2

最簡單的解決辦法是增加一個bash腳本通過-e選項進行終端設置,並將標準輸出傳送到所需的輸出文件:

gnuplot -e 'set term pngcairo' gnuplotCommand.txt > output.png 
+0

這很方便。我只是使用同一劇情節目的兩個版本。一個用於pngcairo,一個用於X11,暫停並重新讀取屏幕。但是這很高興知道。永遠不要試圖管道這樣的PNG文件,無論是。我的輸出文件需要按日期命名,所以我需要設置文件名爲 - system(「date +%F :)。png - 我會在設置的術語之後使用\ n以便添加更多的行,或者命名可以在bash中完成?我的帽子給你,@Christoph。 – SDsolar

3

你可以嘗試像

gnuplot <<- EOF 
    set term png 
    set output 'gnuplotCommand.txt.png' 
    load 'gnuplotCommand.txt' 
EOF 

,或者在.EPS版本

gnuplot <<- EOF 
    set terminal postscript eps 
    set output 'gnuplotCommand.txt.eps' 
    load 'gnuplotCommand.txt' 
EOF 
+1

變化'陰謀「gnuplotCommand.txt''爲'負荷」 gnuplotCommand.txt'',因爲這個文件是從其他程序自動生成的腳本。 – vagoberto

+0

我想你只是回答了我在上面評論中提出的問題。我需要使用system(「date + F」).png作爲我的文件名。我知道如何在兩行代碼中做到這一點。 TNX。 – SDsolar

2

如果您有gnuplot版本5.0,您可以將參數傳遞給您的腳本。例如,

# script.gp 
if (ARGC > 1) { 
    set terminal ARG2 
    set output ARG3 

    print 'output file : ', ARG3, ' (', ARG2 , ')' 
} 

# load the script needed 
load ARG1 

該腳本必須以選項-c

gnuplot -c script.gp gnuplotCommand.txt pngcairo output.png 

在這個例子中被調用,我們已經設置了變量ARG1=gnuplotCommand.txtARG2=pncairo,和ARG3=output.png。參數的數量是ARCG=3。此外,它已被設置爲ARG0=script.gp作爲主腳本的名稱。

如果你只是想看到的輸出,而不將其保存到一個文件,你可以調用這個腳本:

gnuplot -p script.gp gnuplotCommand.txt 

您可能要檢查用戶是否對輸出文件中給出的名稱。如果沒有,我們可以使用默認的名字:

if (ARGC > 1) { 
    if (ARGC < 3) { ARG3="default" } # without extension... it doesn't matter in linux :) 
    set terminal ARG2 
    set output ARG3 

    print 'output file : ', ARG3, ' (', ARG2 , ')' 
} 
+0

參數也可以在早期版本中傳遞,但使用不同的語法。 – Matthew

+0

這看起來不錯,但如果參數需要是今天的日期還是任意的?在程序內部,我會使用date = system(「date +%F」)然後設置輸出日期。「。png」得到今天的日期。 – SDsolar

+0

@SDsolar用'gnuplot -c script.gp「$(date +%F)」''這會將'ARG1'設置爲日期(在腳本中使用它作爲'ARG1。「。png」') – vagoberto