gnuplot tips

手軽にグラフを描きたい時に便利。
最近のLinuxには、標準インストールされていない?


2Dプロット例

gnuplot> plot 'filename.dat' using column#

描画方法を変更可、with lines で折線、
他に
points, linespoints, dots, impulses, yerrorbars,
xerrorbars, xyerrorbars, steps, fsteps, histeps,
filledcurves, boxes, boxerrorbars, boxxyerrorbars,
vectors, financebars, candlesticks, errorlines,
xerrorlines, yerrorlines, xyerrorlines, pm3d
など。


title "文字列"で凡例を変更可
gnuplot> plot "datafile" using 1:2 title "sampleA"
凡例が不要な場合
gnuplot> unset key

複数行の入力。バックスラッシュ。ん、だめ?
gnuplot> plot 'filename.dat' using 1:6 \
   title titlename with linespoints 3 4

複数のグラフを同画面に。カンマで区切る。
gnuplot> plot cos(x), sin(x)

PNGに出力
gnuplot> set term png
gnuplot> set output 'filename.png'
gnuplot> plot cos(x)

コマンドをテキストファイルから読み込む
gnuplot> load "hogehoge.txt"

設定内容をテキストファイルに保存する
gnuplot> save "hogehoge.txt"

軸ラベルの指定
gnuplot> set xlabel "time(sec)"
gnuplot> set ylabel "energy(kcal/mol)"
文字列を渡さなければ初期化
gnuplot> set xlabel

軸目盛の向きを変える
gnuplot> set tics out
gnuplot> set tics in
gnuplot> set xtics 

X軸目盛に文字列を付与する
gnuplot> set xtics ('A'0.0, 'B'0.5, 'C'1.0)
gnuplot> set xtics ("-180" 0, "-135" 3, "-90" 6, "-45" 9, "0" 12, "45" 15, "90" 18, "135" 21,  "180" 24)

Y=0に軸を表示する
gnuplot> set xzeroaxis
gnuplot> set xzeroaxis 2
X=0に軸を表示する
gnuplot> set yzeroaxis

グリッドを表示する
gnuplot> set grid
gnuplot> set grid xtics

Y軸に平行な直線、または直線をひく
gnuplot> set arrow from 3, graph 0 to 3, graph 1 nohead
gnuplot> set arrow nohead from 0,0 to 1,1
gnuplot> set arrow to 1,2 ls 5
gnuplot> set arrow 3 to 1,1,1 nohead lw 0.2
gnuplot> set arrow from 0,-5 rto graph 0.1,0.1

指定座標(X,Y)にラベルを配置する
gnuplot> set label 'y=x' at 1,2

軸の目盛間隔を指定する
gnuplot> set xtics 10
gnuplot> set xtics -180,60 -180から+60刻みで表示

軸の補助目盛を追加する
gnuplot> set mxtics num(主目盛の分割数)

表示形式を変更する
gnuplot> set format "%6.3f"
gnuplot> set format x "%6.3f"

対数軸に変更する
gnuplot> set logscale x

軸を反転する
gnuplot> set xrange [] reverse

データ範囲の指定
gnuplot> set xrange [0:100]
gnuplot> set yrange [0:1]

設定内容の確認と初期化
gnuplot> show オプション名(e.g. all, term, tics)
gnuplot> reset

拡大した図を元のサイズに戻す。
"a"キーを押す。

グラフタイトルの設定
gnuplot> set title "グラフ名"

グラフタイトルのフォント、フォントサイズを指定
gnuplot> set title font "フォント名,サイズ"
例)
gnuplot> set title font "arial,40"

その他、フォント設定
gnuplot> set terminal png font "Times,14"    # 全般
gnuplot> set tics font "Times,14"                     # 目盛り
gnuplot> set xlabel font "Times,14"                 # xlabel
gnuplot> set ylabel font "Times,14"                 # ylabel
gnuplot> set zlabel font "Times,14"                 # zlabel
gnuplot> set key font "Times,14"                      # 凡例

直線を補完してプロット(スムージング)
gnuplot> plot "data.txt" using 1:2  smooth csplines with lines
その他には、unique/frequency/acsplines/bezier/sbezierを選択可


初期値に戻す
gnuplot> reset

よくわからなければ、、
gnuplot> help


3Dサーフェス&等高線プロット例

データ形式(データセットを空行で区切る)

x y z
x y z
x y z

x y z
x y z
x y z

x y z
x y z
x y z
...
gnuplot> set contour
gnuplot> set cntrparam levels 40
gnuplot> splot 'data.txt' u 1:2:3 w l

(3Dグリッドの設定、デフォルト10)
gnuplot> set dgrid3d 50,50
nuplot> replot

(等高線のみの場合)
gnuplot> unset surface
gnuplot> set view 0,0
gnuplot> replot

(サーフェスをカラーマップ表示)
gnuplot> set pm3d
gnuplot> set surface
gnuplot> set view 60,40
gnuplot> replot

(カラーマップ等高線を底面に表示)
gnuplot> set pm3d at b
gnuplot> replot

(カラーマップ等高線を上面に表示)
gnuplot> set pm3d at t
gnuplot> replot

(カラーマップをサーフェスに表示)
gnuplot> set pm3d at s
gnuplot> replot

(カラーマップをサーフェスと底面に表示)
gnuplot> set pm3d at bs
gnuplot> replot

(pm3dのatオプションは6つまでの組み合わせが可能、順番は効いてるが、意味ない?)
gnuplot> set pm3d at bstbst
gnuplot> replot

(等高線の凡例を非表示)
gnuplot> unset clabel
gnuplot> replot

(カラーマップ上にラベルを表示)
gnuplot> set label 1 'Saddle Point' at 9,9 front
gnuplot> set label 1 'Saddle Point' at 9,9 front tc rgb "white" font ",32"

(アスペクト比を変更)
gnuplot> set size ratio 1.0
gnuplot> set size ratio 0.5
gnuplot> set size square
※グラフサイズを調整(x軸方向に1/2)
gnuplot> set size 0.5,1

(等高線をカラーマップ表示)
gnuplot> set pm3d map
gnuplot> unset surface
gnuplot> unset contour
gnuplot> replot

(凡例カラーのサンプル確認、変更)
gnuplot> help palette rgbformulae
gnuplot> show palette rgbformulae
gnuplot> set palette rgbformulae 7,5,15
gnuplot> set palette model RGB rgbformulae 33,13,10
gnuplot> set palette rgbformulae 34,35,36
gnuplot> replot

(現在の凡例カラーから、rgbformulaeに適した値を探す)
gnuplot> set palette model RGB functions abs(2*gray-0.5), sin(pi*gray), cos(pi/2*gray)
gnuplot> show palette fit2rgbformulae
gnuplot> set palette model XYZ functions gray**0.35, gray**0.5, gray**0.8
gnuplot> show palette fit2rgbformulae
gnuplot> set palette model HSV functions gray,1,1
gnuplot> show palette fit2rgbformulae

(視点変更)
gnuplot> set view 45, 30, 1, 1
gnuplot> show view
gnuplot> replot
※ set view <rot_x>{,{<rot_z>}{,{<scale>}{,<scale_z>}}}

(カラーボックスの座標指定)
gnuplot> set colorbox user origin 0.8, 0.5, 0 size 0.025, 0.4
gnuplot> set cblabel "SCF Energy / hartree" offset -12.0, 0


マトリックスデータのサーフェスプロット

usingの代わりに、matrixを使用する。


gnuplot> splot 'data.txt' matrix with lines


2軸をつかったプロット


gnuplot> set y2tics nomirror
gnuplot> set y2label 'y2'
gnuplot> plot sin(x) axis x1y1, x**2 axis x1y2
gnuplot> set ylabel 'y1'
gnuplot> set ytics nomirror
gnuplot> set xrange [-pi:pi]
gnuplot> set y2range [0:9]
gnuplot> replot


タイトルに上付き文字(superscript)、下付き文字を使用(subscript)

terminalにpostscript + enhancedオプションを指定する。


gnuplot> set term postscript enhanced color
gnuplot> set output 'test.ps'
gnuplot> set title 'sample pl_{o}t'
gnuplot> plot x**3 t 't^{3}'


空行でデータブロックを分けて、indexでブロック指定する際に、
"warning: Skipping data file with no valid points"が出る場合、
空行を1行から2行に変更すると、警告を回避できた。↓なかんじ。


# data 1
x1 y1
x2 y2


# data 2
x1 y1
x2 y2


gnuplot> plot 'datafile' index 0 title 'data 1' w l, 'data' i 1 t 'data 2' w l


"warning: Too many axis ticks requested"の対処
おそらくterminalの問題、


gnuplot> set term png
gnuplot> set output 'name.png'
※現在の端末を確認するには、
gnuplot> show term
サポートされてる端末がわからない場合は
gnuplot> set term
とする。

等として、ターミナルを変更する。


線の色や太さ、形を変更する。


gnuplot> plot sin(x) linecolor rgbcolor "red" linetype "solid" linewidth 8
gnuplot> plot exp(-x**2) lc rgb "blue" lt "dashed" lw 4
gnuplot> splot sin(x)*cos(y) lc palette z lw 2

ん?lcが使えない。とりあえずの対処法
gnuplot> help color_resources
でラインカラーを確認して、gnuplot起動時にリソースを変更する。
例:
$ gnuplot -xrm 'gnuplot*line2Color:blue'


例、バンド図の描画。


gnuplot> set xtics ("Gamma" 0, "X" 0.612324, "W" 0.918485)
gnuplot> set ytics add ("Ef" -3.86316331725612)
gnuplot> set grid x
gnuplot> plot 'bands' w l t "", -3.86316331725612 t ""


ギリシャ文字のGammaを使う場合

postscriptで出力設定。
"{/Symbol G}"として指定する。小文字のgを指定すると小文字に。
他の文字もA-Z/a-zのアフファベットが割り当てられてる。


gnuplot> set xtics ("{/Symbol G}" 0, "X" 0.612324, "W" 0.918485)
gnuplot> set term postscript eps enhanced color
gnuplot> set output 'bands.ps'
として、プロット。

よくわからなければ、xticsのヘルプを参照。


gnuplot> help xtics


オンザフライでプロットしたい場合、here documentを使うと良い。
例。


#!/bin/sh

gnuplot << EOF

set term png
set output 'plot.png'
set xrange [-pi:pi]
set xzeroaxis
plot sin(x)

EOF


複数の列(カラム)、データファイルを一括プロット(v4.4以降)

次のような、N x 4 のデータの場合。


$ cat mkdat.awk

BEGIN{
pi=3.1415926535897932384626
dm=100
i=1
while (i <= dm*2)
{
printf "%12.8f %12.8f %12.8f %12.8f \n", pi/dm*i, sin(pi/dm*i), cos(pi/dm*i), sin(pi/dm*i)*cos(pi/dm*i)
i++
}
}
$ awk -f mkdat.awk > dat.txt

for文を使う。(titleは宿題)

gnuplot> plot for [i=2:4] './dat.txt' u 1:i w l t ""

ファイルnum01.dat, num02.datがある場合。。


gnuplot> plot for [i=1:2] "num0".i.".dat" w l
OR
gnuplot> plot for [s in "01 02"] "num".s.".dat" w lp t "num".s.""
OR
gnuplot> list = system('ls num*.dat')
gnuplot> plot for [i=1:words(list)] word(list,i) with lp


最大値、最小値等の統計値を表示


gnuplot> show variables all
gnuplot> show variables GPVAL_Z_MIN
gnuplot> show variables GPVAL_DATA


次のサイトにあったサンプル。
http://www.phyast.pitt.edu/~zov1/gnuplot/html/statistics.html


reset
set sample 50
set table 'stats1.dat'
plot [0:10] 0.5+rand(0)
unset table

set yrange [0:2]
unset key
plot 'stats1.dat' u 1:2
min_y = GPVAL_DATA_Y_MIN
max_y = GPVAL_DATA_Y_MAX

plot 'stats1.dat' u ($2 == min_y ? $2 : 1/0):1
min_pos_x = GPVAL_DATA_Y_MIN
plot 'stats1.dat' u ($2 == max_y ? $2 : 1/0):1
max_pos_x = GPVAL_DATA_Y_MAX

# Automatically adding an arrow at a position that depends on the min/max
set arrow 1 from min_pos_x, min_y-0.2 to min_pos_x, min_y-0.02 lw 0.5
set arrow 2 from max_pos_x, max_y+0.2 to max_pos_x, max_y+0.02 lw 0.5
set label 1 'Minimum' at min_pos_x, min_y-0.3 centre
set label 2 'Maximum' at max_pos_x, max_y+0.3 centre
plot 'stats1.dat' u 1:2 w p pt 6


Tutorial
http://physicspmb.ukzn.ac.za/index.php/Gnuplot_tutorial




x軸に垂線を引く。


あまり巧い方法ではないけども、x2ticsを使う方法。

まずx2ticsの範囲をxticsに合わせる。例えば、

gnuplot > set x2range [-5:20]

で、任意のxをx2ticsとして定義する。

gnuplot> set x2tics ("Ef" 14.8299)

既存のものに追加する場合は、set x2rangeは不要で、

gnuplot> set x2tics add ("Ef" 14.8299)

でOK。続けて、

gnuplot> set grid x2

として、

gnuplot> replot

とすれば点線で垂線が引ける。xticsのmirrorが邪魔な場合は、

gnuplot> set xtics nomirror

として消す。線種を変える場合、

gnuplot> set grid x2 linetype 3

など。

ヒストグラム(Histogram)をプロット(便利!)


gnuplot> bin(x,width)=width*floor(x/width)
gnuplot> binwidth=0.2
gnuplot> plot 'data.txt' using (bin($5,binwidth)):(0.999) smooth freq with imp,
int()を使用する場合の例:
gnuplot> filter(x,y)=int(x/y)*y
gnuplot> plot 'dat_Ha.txt' u (filter($1,0.2)):(1) sm freq w boxes

ヒストグラム、再び。。


ヒストグラム クラスタボックス
gnuplot> set style data histogram
gnuplot> set style histogram cluster gap 1
gnuplot> set style fill solid border -1
gnuplot> set boxwidth 0.9
gnuplot> plot 'result.txt' u 2:xtic(1) t col, '' u 3 t col

ヒストグラム スタック
gnuplot> set xtics nomirror rotate by -45 scale 0
gnuplot> set style data histogram
gnuplot> set style histogram rowstacked
gnuplot> set style fill solid border -1
gnuplot> set boxwidth 0.75
gnuplot> plot 'result.txt' u 2:xtic(1) t col, '' u 3 t col

ヒストグラム プロット例
http://gnuplot.sourceforge.net/demo/histograms.html


グラフを元に数値データ出力。splotでも可。


gnuplot> set table "sample.dat"
gnuplot> plot sin(x)