Contents


実行環境

 • Windows7 x64 + Cygwin 2.5.1 + ConEmu 150813g
 • Cygwinpsql(パッケージpostgresql-client 9.5.3でインストール)
 • pyxplot 0.9.2(公式サイトのソースからCygwin上でビルド)
 • Windowsの管理者権限ユーザ

psqlが接続したPostgreSQLWindows版の9.5.3(直接関係ないけど。何らかDBサーバに接続しないとpsqlを使えないので)。

pyxplotgnuplotと似たコマンド体系のグラフ作成ツールで、特徴として(1)グラフ中の数字・文字をLaTeXで描画(2)グラフ以外にベクターグラフィックスも描ける、(3)グラフに使う色指定でCYMKを使える、など。残念ながら2012年を最後に開発が止まってます。


準備 …Cygwinシェルでのpyxplotビルドと動作確認

pyxplotgnuplotと最も違うのは「LaTeX必須」な点。オプション的に「グラフ中のラベルにLaTeXを使える」でなく、LaTeXがないとビルド自体できません。

Cygwinでビルドする前にTexLiveを入れようとしたらこれが大変。apt-cygでパッケージ群を入れてもlatex.fmtがない等のエラーで動かず…。

結局setup-x86_64.exeを使ってPublisingカテゴリを全部入れたら(ものすごく時間かかった)なぜか正常に動きました。その後「明らかに不要そうな」パッケージ群を一つずつ削除し、ビルド時に残したのは ↓ これです。まだ不要なのがありそうだけど。
texlive
texlive-collection-basic
texlive-collection-context
texlive-collection-fontsrecommended
texlive-collection-fontutils
texlive-collection-genericrecommended 
texlive-collection-htmlxml
texlive-collection-humanities
texlive-collection-langcjk
texlive-collection-langenglish
texlive-collection-langjapanese
texlive-collection-latex
texlive-collection-latexextra
texlive-collection-latexrecommended
texlive-collection-luatex
texlive-collection-metapost
texlive-collection-music
texlive-collection-omega
texlive-collection-pictures
texlive-collection-pstricks
texlive-collection-publishers
texlive-collection-science
texlive-collection-xetex


LaTeXが動けば、後はpyxplotのソースREADMEに必要ライブラリが ↓ と明記されているし、configureスクリプトもあるので簡単。
- fftw                       (version 2 or, preferably, 3+) 
- gcc and make
- Ghostscript
- The Gnu Scientific Library (version 1.10+)
- ImageMagick
- LaTeX                      (version 2e)
- libpng                     (version 1.2+)
- libxml2                    (version 2.6+)
- zlib


configureを走らせた最後に出たライブラリ関係一覧。当初readlineを入れたところ(READMEstrongly recommendedとあったので)make途中でエラー発生。外したらなぜかmakeできました。
pyxplot_0.9.2.tar $ ./configure
...
Checking for latex             ............. YES
Checking for ImageMagick convert............ YES
Checking for sed               ............. YES
Checking for gunzip            ............. YES
Checking for wget              ............. YES
Checking for ghostscript       ............. YES
Checking for ghostview         ............. NO
Checking for ggv               ............. NO
Checking for GNU make          ............. YES (make) 
Checking for gcc               ............. YES
Checking for libreadline-dev   ............. NO
Checking for libcfitsio-dev    ............. NO
Checking for libgsl0-dev       ............. YES
Checking version of libgsl0-dev ............ YES
Checking for libfftw3-dev      ............. YES
Checking for libxml2-dev       ............. YES
Checking for libpng-dev        ............. YES
Checking for libkpathsea-dev   ............. NO
Checking for kpsewhich         ............. YES
Configuration successful.
To continue installation, type 'make'.

pyxplot_0.9.2.tar $ make
pyxplot_0.9.2.tar $ make install


↓ インストール後の起動、続いてテスト。起動時にアスキーアートでロゴが出ますが、自分の使用フォントのせいでバックスラッシュが¥です。デフォルトのデバイスはepsなのでPDFに変更。グラフがカレントディレクトリのpyxplot.pdfに出力されます。ラベルの中、$$LaTeXの数式。
$ pyxplot
$ set terminal pdf
$ plot 1/sin(x) title "$\frac{1}{sin(x)}$" 

exitまたはquitで終了するとGoodbye. Have a nice day.と出るのがお茶目。出力されたPDF
はこちら(9.2kB)。上の画像でも分かるとおり余白がゼロで、コンテンツぴったりにトリミングされたPDFになります。単独での見映えは今いちな一方、素材として使うにはむしろ好都合。

ファイルだけでなくX11も出力デバイスにできますが、今のところ自分のCygwinXを入れてないので今回は見送り。

対話シェルで困った点。たぶんビルド時にreadlineを含めなかったせいですが、上下カーソルがヒストリ呼び出しでなく文字通り「コンソールにあるカーソルの上下移動」になってました。カーソルを戻してコマンド入力しても、先頭にエスケープシーケンスが入力されているので駄目という…。今回は我慢したけど実際とても使えないので、もう一度ビルドからやり直す予定。解決したら追記します。


確認 … 使えるコマンドラインオプション、標準入力の扱い

↓ 起動時のコマンドラインオプション。gnuplot、Octave、Rどれにもあったコマンド実行(-eまたは--eval)がありません。対話型シェルに入るか、引数でスクリプトを渡す(下記helpにはないけど可能)に特化してる模様。
$ pyxplot --help

Pyxplot 0.9.2
-------------

Usage: pyxplot  
  -h, --help:       Display this help.
  -v, --version:    Display version number.
  -q, --quiet:      Turn off initial welcome message.
  -V, --verbose:    Turn on initial welcome message.
  -c, --color:      Use colored highlighting of output.
  -m, --monochrome: Turn off colored highlighting.

A brief introduction to Pyxplot can be obtained by typing 'man pyxplot'; the
full Users' Guide can be found in the file:
/usr/local/share/doc/pyxplot/pyxplot.pdf

Extensive documentation is also available at , which 
also provides contact information for bug reports.


パイプで渡した標準入力は、Octave、Rと同様コマンドとして解釈&実行されます。残念ながらgnuplotのように「パイプでデータを渡し-eコマンドでグラフ出力」という、最も簡単な方法は取れません。LaTeX使用を前提にしてるあたり、簡便さでなく「ちゃんとしたグラフを作る」指向っぽい。
$ echo '1
2
3' | pyxplot
$ echo 'set terminal pdf; plot sin(x)' | pyxplot 
$ cygstart pyxplot.pdf


psqlから一時ファイル経由でデータをプロット

データの渡し方は、パイプでなく一時ファイル経由。前回までと同様、Octaveのソンブレロの3次元座標をPostgreSQLにクエリし、結果を3Dプロットしてみました。↓
-- set output format
# \f ' ' \\\t on \\\pset format unaligned

-- query and send command to pyxplot
# select x, y, sin(r) / r
  from generate_series(-8, 8, 0.4) as x,
      generate_series(-8, 8, 0.4) as y,
      cast(sqrt(x^2 + y^2) + 1e-9 as float) as r
  \g tmp5 \\\! echo 'set terminal pdf; plot 3d "tmp5" w lines col red' | pyxplot 
  
-- open pdf
# \! cygstart pyxplot.pdf

実際出力されたPDF
はこちら(44kB)。上記クエリでは一時ファイル名をtmp5とし、短いpyxplot用コマンドをパイプでpyxplotに送ってます。コマンドではファイル名のクォートが必須(gnuplotも同じ)。


準備の最後に書いた問題により今回ビルドしたpyxplotの対話シェルは実用に厳しいですが、解決した時に備え、psqlから対話シェルに入る例も ↓ やってみました。
-- set output format
# \f ' ' \\\t on \\\pset format unaligned

-- query and launch pyxplot
# select x, y, sin(r) / r
  from generate_series(-8, 8, 0.4) as x,
      generate_series(-8, 8, 0.4) as y,
      cast(sqrt(x^2 + y^2) + 1e-9 as float) as r
  \g tmp5 \\\! pyxplot -q
pyxplot> set terminal pdf
pyxplot> set grid
pyxplot> set key below
pyxplot> set view -30, 20
pyxplot> plot 3d 'tmp5' title "$\frac{sin(\sqrt{x^2+y^2})}{\sqrt{x^2+y^2}}$" w surface fillc cmyk(0.4, 0, 0.4, 0) 
pyxplot> exit

# \! cygstart pyxplot.pdf

実際出力したPDF
はこちら(44kB)。対話シェルに入る前に「ざっくりグラフ化」はできないものの、クエリ結果をもとに何度もグラフ出力を繰り返す場合は結構使えそう。とにかくLaTeXを簡単にラベルに使えるのが、前回までのgnuplot、Octave、Rに比べて大きな利点です。

また、色を印刷用のCMYKでも指定できるのは珍しい特徴。上記では「C=40%, M=0, Y=40%, K=0」としており、Acrobat Proで確認すると ↓ 確かにそうなってました。余白なしにトリミングされていることもあり、とても文書向きです。