2013-07-09 11 views
20

3つのプロットを含むgnuplotを作成したいとします。パイププロットデータからgnuplotスクリプト

https://s22.postimg.org/qcc94e1i9/test.png

を現在、私はプロットを作成するには、次のgnuplotスクリプトを使用しています:

set terminal png 
set output "test.png" 
plot for[col=2:4] "data.txt" using 1:col title columnheader(col) with lines 
私だけ

それは次のようになりますしたいよう データは(インラインでなければなりません

ファイルdata.txtは、

Generation Best Worst Average 
0 2 1 0 
1 3 1 2 
2 4 3 3 
3 4 3 3 
4 6 3 4 
5 7 4 5 
6 9 6 7 
7 10 6 9 
8 10 5 6 
9 11 6 8 
10 12 7 9 
です。

data.txtをgnuplotにパイプし、スクリプト内の参照されたデータファイルに依存しないようにしたいと思います。 何かのようなものcat data.txt | gnuplot plot.gnu。 これは、私がいくつかのファイルがdata.txtであり、それぞれにplot.gnuファイルを作成したくないためです。

'-'ファイルin this stackoverflow threadと私は約multiple plots in one fileを読んでいます。しかし、これはgnuplotコードでデータをインクルードする必要があり、クリーンではありません。シェルからのgnuplotの-eオプションを使用してと間違って何

+0

一つは、人口ベースのアルゴリズムといくつかの最適化問題を解決するため、賭けることができます;) –

答えて

11

?あなたがforループを使用して、シェルから「ファイル名」のために異なる値で上記のコマンドを複数回呼び出すことができるはず

gnuplot -e "filename='data.txt';ofilename='test.png'" plot.gnu 

を: あなたが入力として変数を提供することができ、使用してシェルから、data.txtを言います。

そして、あなたはあなたのスクリプトplot.gnuに変わり:あなたがUnixシステム(つまり、Windowsではなく)上にある場合は、標準入力から読み取るように'<cat'の代わり'-'を使用することができます

set terminal png 
set output ofilename 
plot for[col=2:4] filename using 1:col title columnheader(col) with lines 
+0

グレート答えを、認識していません'-e'コマンドはまだ! –

21

plot '<cat' using ... 

cat data.txt | gnuplot script.gpすることができます。しかし、特定のケースでは、あなたの質問に言及すると、forループのプロットでは、あなたは入力を3回読む。したがって、データを最初に読み込んだ後にデータがなくなるため、stdinでデータを送信することは適切ではありません。

-2

ミックスの両方の答え:

cat data.txt | gnuplot -e "set terminal png; set output "test.png"; plot for[col=2:4] '<cat' using 1:col title columnheader(col) with lines 
+1

stdinからの読み込みをgnuplotのforループと組み合わせることはできないので、これは動作しません。回避策については、http://stackoverflow.com/questions/4981279/how-to-make-several-plots-from-the-same-standard-input-data-in-gnuplotを参照してください。 – ramn

2

どのようにシステムの使用方法について()コマンド

set terminal png 
set output "test.png" 

# read shell input 
# the echo prints the variable, which is then piped to gnuplot 
fname = system("read filename; echo $filename") 

plot for[col=2:4] fname using 1:col title columnheader(col) with lines 

あなたは

echo "data.txt" | gnuplot script.gp 
15

ない直接の回答で、今それを呼び出すことができますが、これは私がデータをすばやく見るために使用するもの。それはあなたが複数回パイプからのデータをプロットしたい場合は、メモリに何とかそれを格納する必要がありcutコマンド

cat data.txt | cut -f2 -d' ' | gnuplot -p -e "plot '<cat'" 
10

では特に便利です。私の好みの方法は、/dev/shmの一時ファイルを使用することです。これはほとんどのLinuxシステムに存在し、RAMにマップしています。物事をきれいに保つために、私は退出時に一時ファイルを削除するトラップを設定しました。

(あなたdata.txtを使用)例:

cat data.txt | (cat > /dev/shm/mytempfile && trap 'rm /dev/shm/mytempfile' EXIT && gnuplot -e "set terminal dumb; plot for[col=2:4] '/dev/shm/mytempfile' using 1:col title columnheader(col) with lines") 

結果:

12 ++------------+-------------+-------------+-------------+------------** 
    +    +    +    +    + Best ****** + 
    |              Worst***#### | 
10 ++            *******Average $$$$$$++ 
    |           ****      | 
    |          *** $$$$    $$$$ 
8 ++          **  $$ $$  $$$$$ ++ 
    |         **  $$  $$ $$  | 
    |        ***** $$$    $$  #### 
6 ++       ****  $$ ############# $$ ##### ++ 
    |       **   $$ ##    # ####  | 
    |      **  $$$ ##    ##   | 
    |      **  $$$$ ##        | 
4 ++   *********** $$$$$ ####         ++ 
    |  ***** ###################          | 
    | **** $$##               | 
2 ** $$$##               ++ 
    #########                | 
    + $$   +    +    +    +    + 
0 $$------------+-------------+-------------+-------------+------------++ 
    0    2    4    6    8    10