xy2ps: X-Yデータをポストスクリプト形式のグラフに変換する
sample graph

必要なもの: Perl, gs(GNU Ghostscript: ポストスクリプトインタープリター)あるいはポストスクリプトプリンター
(convert(1), cut(1), paste(1), sed(1), tac(1)などを併せて使うと効率的な処理が可能となる。)

0. インストールの方法

$ wget http://members.jcom.home.ne.jp/maegawa/pub/xy2ps
$ chmod +x xy2ps
$ su
# cp xy2ps /usr/local/bin

1. 基本的な使い方

データが与えられなかった場合、xy2psはランダムなX-Yデータを自動的に生成する。
$ xy2ps >temp.ps

描画にgs(Ghostscript)を用いる。
$ gs temp.ps

ポストスクリプトプリンターにデータを送る。
$ lpr temp.ps

上述の2つのコマンド(xy2psとgs)は以下の1つのコマンドで実行が可能。
$ xy2ps -gs

さらに上記は次のコマンドと同じ
$ xy2ps >~/.xy2ps/temp.ps && gs -q ~/.xy2ps/temp.ps

なので、-gsオプションで表示した前回のグラフは次のようにしてプリンターに送ることができる。
$ lpr ~/.xy2ps/temp.ps

プリンターにダイレクトにデータを送信することも可能である。
$ xy2ps | lpr

イメージファイルを作成する。
$ xy2ps -image | convert - portrait.jpg
$ xy2ps -image -landscape | convert - -rotate 90 landscape.jpg

2. 数値データのグラフ描画

2.1 "sample.dat"をプロットする

$ cat sample.dat
放物線のデータ
0  0
1  1
2  4
3  9
4  16
5  25
6  36
7  49
8  64
9  81
10 100
$ xy2ps sample.dat -gs

2.2 標準入力からデータを読み込む

(OK)
$ cat sample.dat | xy2ps - >temp.ps && gs -q temp.ps

(NG)
$ cat sample.dat | xy2ps - -gs
$ cat sample.dat | xy2ps - | gs -

(OK)
$ gs `locate tiger.ps`
(NG)
$ cat `locate tiger.ps` | gs -

3. 装飾

-no_pedantic
$ xy2ps -no_pedantic -gs

描画をコントロールする様々なコマンドラインオプションの例(xy2ps -h, xy2ps -h2を参照されたい)
$ xy2ps sample.dat -gs -xmin 0 -xmax 10 -xtick 5 -xsubt 5 -ymin 0 -ymax 100 -ytick 50 -ysubt 5 -xtitle "X axis name" -ytitle "Y axis name" -gtitle "Graph Title" -lwidth 1.2 -psym 8 -psize 7 -lcolor 1

線種、色、太さの指定
$ xy2ps data-file -xyxy -gs -only_axis -lwidth ".5|.5|4" -lstyle "2|1|0" -lcolor "0|2|1"

4. 複数の線

4.1 xyyyタイプのマルチカラムデータ

    x y1 y2 y3...
    :
    :
$ ./mkxy_0 > two-lines.dat
$ xy2ps two-lines.dat -gs -no_pedantic -label "exp(-x/5)*sin(x)|exp(-x/5)" -legend_x 0.5 -legend_y 0.05

$ ./mkxy_1 > multi-lines.dat
$ xy2ps multi-lines.dat -gs -psym 1 -voffset 150

4.2 xyxyタイプのマルチカラムデータ

    x1 y1 x2 y2 x3 y3...
    :
    :
$ ./mkxy_2 > xyxy-data
$ xy2ps xyxy-data -gs -xyxy -only_data -no_lstyle

4.3 各カラムのデータのポイント数

$ cat points.dat
1	1	1
2	4	8
3	9	27
4	16	64
5	25	125
6	36
7	49
8	64
9	81
10	100
$ xy2ps points -xlog -ylog -gs
-xyxyタイプも同様。
    x1_1  y1_1  x2_1  y2_1  x3_1 y3_1
    x1_2  y1_2  x2_2  y2_2  x3_2 y3_2
    x1_3  y1_3  x2_3  y2_3
    x1_4  y1_4  x2_4  y2_4
    x1_5  y1_5
    x1_6  y1_6
$ ./mkxy_3 100 0.25 >_1
$ ./mkxy_3  50 0.50 >_2
$ ./mkxy_3  25 1.50 >_3
$ ./mkxy_3  10 2.00 >_4
$ ./mkxy_3   5 3.00 >_5
$ paste _[1-5] > _all
$ xy2ps _all -xyxy -psym 2 -label "line-1|line-2|line-3" -no_tick -gs
データポイント数の大きなカラムは左側に位置しなければならないことに注意。

4.4 マルチカラムデータから特定のフィールドを取りだす。

cut(1)コマンドを使えば任意のカラムデータを取りだすことができる。
$ cut -f7,8 _all | xy2ps - >w.ps && gs -q w.ps

4.5 CSV(Comma Separated Value)データの扱い

xy2psではデータ中のコンマ(,)は無視される。CSVデータについては、sed(1)コマンドを用いることにより扱うことが可能となる。
$ cat csv.dat
1,1,1
2,4,8
3,9,27
4,16,64
5,25,125
$ sed 's/,/ /g' csv.dat | xy2ps - >w.ps && gs -q w.ps

5. データファイル中のコマンドラインオプション

コマンドラインオプションをデータファイルに記述することが可能である。
$ xy2ps population | convert - pop.jpg

-no_file_optionを用いればデータファイル中のオプションを無視することができる。
$ xy2ps population -no_file_option -gs

コマンドラインオプションによりデータファイル中のオプションは上書きされる。
$ xy2ps population -ylog -ymin 10 -ymax 10000 -legend_x 0.05 -legend_y 0.12 -npp -no_psym | convert - pop_log.jpg

オプションファイルとデータファイルは次のように別々のものにすることもできる。
$ for i in `seq 1 5`;do xy2ps -stderr_data >/dev/null 2>_$i; done; paste _[1-$i] >data-file
$ cat option-file data-file | xy2ps - >w.ps && gs -q w.ps


以下はシンプルな実行の例である。'mkgraph'を書き変えることによって全ての描画パラメータを制御することが可能である。
$ ./mkgraph | xy2ps - -landscape -image | convert - -rotate 90 freevib.png
$ ./mkgraph | xy2ps - >w.ps && gs -q w.ps

6. ヘルプ

以下に示すように使用法やその他の情報を得ることができる。
$ xy2ps -help
$ xy2ps -more
$ xy2ps -table
$ xy2ps -builtin

7. なぜxy2psか?

このような作業をGUI環境で実行することを想像して欲しい。
$ ls *.dat | wc -l
    100
$ for data in *.dat; do xy2ps $data | lpr; done
恐らく、gnuplotは同じことをより効率的に、より美しく、より素晴らしく実行するだろう。 しかしながら、私のやりたいことの95%はgnuplotの5%の機能でできてしまう。そのこと自体は決して問題があるわけでは無い。ただ...



◆おまけ(円グラフ)
$ ./piechart test.dat | convert - pc.png
$ ./piechart -gs -doughnut -no_outline -only_ratio test2.dat

Linux, Perl, gs, emacs, その他多くのGNU toolsの作者に多いに感謝します。
-Home- -The program under construction-

inserted by FC2 system