howto/gnuplot

一行メモ

  • gnuplot -e "file='some_dir/name_of_file'; legend='label of data'" test2.gp で、スクリプトで使っている変数に値を代入できる -- 2015-04-01 (水) 13:55:28
  • lt 0 lw 2 (線の種類、太さ) pt 6 ps 2 (プロットの種類、大きさ) rgb "red" (色) -- 2017-11-23 (木) 11:40:47
  • 挙動がわかりづらい理由:
    • オプションとその引数に順番はある。引数とオプションの違いを把握しておらず、逆の順番などにすると正しく処理されない。間違ったものはエラーを出さずに無視される。と、言うあたりに理由があると思われる。
    • gnuplotは、terminalの種類により別々の補助関数が実行されるような形らしい。terminal毎の統一は完全ではない。symbolではダメでSymbolである必要があるなど、terminal毎にちがう。
    • フォントの種類などはterminal毎に完全に別々で(かつ、どこのXサーバを使っているかの表示する環境にも依存)、qtで見れるように作ってもgifではそうはならない(qtでも、表示する環境が違うと、ちがうように表示される)。 -- 2018-12-16 (日) 09:27:41
  • メモ -- 2019-07-09 (火) 19:51:32
    set grid; set grid mytics mxtics; set mytics 5; set mxtics 5; au2fs=0.02418
    fn='chsRC1Xg32N.sup'; plot fn u (au2fs*$2):3 w l, fn u (au2fs*$2):4 w l, fn u (au2fs*$2):5 w l, \
                               fn u (au2fs*$2):7 w l, fn u (au2fs*$2):8 w l, fn u (au2fs*$2):15 w l, \
                               fn u (au2fs*$2):14 w l
  • if else文 -- 2019-07-15 (月) 19:35:32
    au2fs=0.02418
    if ( au2fs == 0.02418 ) { set xlabel "time (fs)"  } else {
    if ( au2fs == 1 ) {       set xlabel "time (au)"  } else {
                              set xlabel "time (ERROR: illegal au2fs)" } }
  • 目盛りのフォントを大きくするなど -- 2019-09-09 (月) 10:55:33
    • set lmargin 10; set tics font "Arial,20" #左のマージンを広げ、目盛りフォントを大きく -- 2019-09-09 (月) 10:56:14
    • set xlabel offset 0,-1; set ylabel offset -2,0 #軸ラベルのオフセット(未確認) -- 2019-09-09 (月) 10:57:11
    • set bmargin 3; set xtics offset 0,-0.4 #下のマージンを広げて、目盛り文字をちょっと下げる -- 2019-09-09 (月) 11:07:11
    • set tmargin 3; #上のマージンを広げる -- 2019-09-09 (月) 11:07:33
    • set ytics offset 1.8,0 #y軸のメモリの数字をちょっと右にずらす -- 2019-09-09 (月) 11:24:50
  • linuxコマンドの実行 -- 2019-09-13 (金) 11:28:48
    gnuplot> ! ls
  • set macro をすることで次が可能になる -- 2020-04-12 (日) 12:57:35
    pstyle="lp"
    plot  hogehoge  @pstyle  hogehoge
  • XQuartz 2.7.8 (xorg-server 1.16.4) だと set term x11 でウィンドウを引っぱってこれる -- 2020-11-25 (水) 21:56:09
    • 複数ディスプレイを利用している場合に、X11(XQuartz)のウィンドウが領域外に置かれてしまう場合 -- 2021-03-14 (日) 10:02:28
      gnuplot> set terminal x11 position 0,0
      または、
      $ gnuplot -e "set terminal x11 position 0,0;   hogehoge" hogescript.gp
      $ gnuplot -e "set terminal x11 position 0,1050;hogehoge" hogescript.gp
      $ gnuplot -e "set terminal qt  position 0,1050;hogehoge" hogescript.gp
    • XQuartzの環境設定->ウィンドウ->新規ウィンドウにフォーカス、のチェックを外しておくとよい。
  • xmgrace の xvgファイルをそのまま plotできるようである -- 2020-11-25 (水) 21:56:44
  • http://www.gnuplot.info/docs_4.6/gnuplot-ja.pdf マニュアル -- 2022-07-21 (木) 21:46:52
  • gnuplot script1 script2 ... とすると、順番にscript1, script2, ...が実行される。スクリプトに - を指定するとそこは標準入力になる(exitしてもgnuplotのプロンプトが返ってこないときがある? いったんbgにして fgにすると - 以降は無視されたり?、いろいろバグる? )。 -- 2022-07-21 (木) 22:22:45
  • gnuplot> ! linuxコマンド [引数 ...] #gnuplotからlinuxコマンド実行 -- 2022-07-21 (木) 22:26:55
  • たとえば複数のファイルの6列目のグラフの重ね合わせ、10列目の重ね合わせ、8列目の重ね合わせ、を順次表示するgnuplotを呼び出すbashスクリプト -- 2022-07-21 (木) 22:42:50
    #!/bin/bash --norc
    
    df='  file1.dat  fil2.dat   file3.dat   file4.dat  '
    
    nd[6]='max\_d'
    nd[10]='max\_disp\_eh'
    nd[8]='max\_disp\_x'
    
    for n in  6  10  8  ; do
    
        str=''
        for f in  $df; do
          str="$str '$f' u 1:$n w lp, "
        done
        str="plot $str"
        str=`echo $str | sed 's/,$//'`
    
        echo "set title '${nd[n]}'" > .load.gp
        echo $str >> .load.gp
    
        gnuplot   '.load.gp'  -e "set grid; set xrange[0:100]; set logscale y; replot"  -
    done
  • MacOSでデュアルウインドウのとき、Gnuplotウインドウがが見えないところに描画されたとき、次をする(XQuartz 2.8.1) -- 2022-09-10 (土) 21:59:17
    set terminal qt position 10,1100; replot
    set terminal qt position 10,110; replot
    など
  • 媒介変数によるグラフの描画例 -- 2022-10-24 (月) 10:33:42
    set parametric
    degree=pi/180
    plot tan(t*degree),tan(t/2*degree)
    set trange[-180:180]
    h=5; set xrange[-h:h]; set yrange[-h:h]; replot
    
    plot t, t, t, 2*t  #となどとすると媒介変数による複数のグラフを描ける
  • plotのとき with stepsで階段状に線を描いてくれる。 -- 2022-12-14 (水) 15:38:36
  • グラフを画像で保存:グラフのウィンドウで、メニューのexportアイコン→export to imageで、pngで保存 -- 2023-04-14 (金) 11:57:01
  • set arrow from 5.0,0.0 to 5.0,1.0; replot #座標(5,0)-(5,1)間に(縦に)線を描く -- 2023-12-18 (月) 11:21:05

グラフ軸など

set title 'タイトル'  #グラフのタイトル
set xlabel "らべる"   #横軸のラベル
set ylabel "らべる"   #縦軸のラベル
set xtics rotate by -90  #目盛の数字を-90°回す;もとに戻すには set xtics noroatte
set format y "%.2f"      # y軸の表示形式の指定
set yrange [*:*] ; replot  #軸範囲を自動設定に戻す
set ytics autofreq  # y軸の目盛を自動に戻す
set mxtics 2  #x軸の副目盛数
set mytics 2  #y軸の副目盛数
set grid ; replot        #方眼グリッドを引く 
set grid mytics; replot  #副目盛りにも方眼グリッドを引く
set logscale y    #対数グラフにする。戻すときは unset logscale y

plot

  • データを演算した結果をグラフにする

using 1:(-$7) などができる。ここで$7はカラム番号である(ファイル内データの7番目のカラム)、 $7の符号を逆転させている。 7を変数ncで与えたい場合は using 1:(-column(nc))とする。 また、log10(), abs() などの関数も使える。

plot 'output.txt' u 1:(sqrt($4)) every w l    #例

また、using 1:($7>0.0 ? $1 : $2) などの三項演算子も使える。

  • 複数のファイルにあるデータを、演算した結果をグラフにする

linuxコマンドpasteを使う。ファイル名を変数として扱うときは下の例のように文字列結合をするとよい。

f0='hoge0.dat'
f1='hoge1.dat'
plot "< paste ".f0." ".f1 using 3:($11/$4) title "ratio"  #例
  • 51点毎にプロット(データを間引く)
plot 'output.txt' u 1:2 every 51 w l
  • プロットから軸まで線を引く
plot 'list.libxc_graph' using 2:1 with impulses title '# of paper in the libxc Mar 28'
  • 棒グラフをプロットする
plot 'list.libxc_graph' using 2:1 with boxes title '# of paper in the libxc'
  • multiplotで複数のグラフを並べる
(例) 縦長のグラフを横に2つ並べる
set size 0.5,1.0
set multiplot
set origin 0.0,0.0 ; plot sin(x)
set origin 0.5,0.0 ; plot cos(x)
unset multiplot
  • 欠損データと無効な値
欠損データは NaN (グラフの線がここで途切れる)
無効な値は   ?   (set datafile missing "?" のように設定できるらしい。1/0の計算値でもよい。
                   グラフの線はここを飛ばして引かれるが、using 1:($2)のように()内で使うと
                   欠損データ扱いになって、途切れる。)
例
set style data linespoints
plot '-' title "(a)"
 1 10
 2 20
 3 ?
 4 40
 5 50
 e
plot '-' using 1:($2) title "(d)"
 1 10
 2 20
 3 NaN
 4 40
 5 50
 e

[ref] http://takeno.iee.niit.ac.jp/~foo/gp-jman/data/20190108/gnuplot-ja/index.html#missing

  • everyの説明
例) every ::0:frame::frame など(frameは0,1,2などの値)
    frame番目のブロックの、すべての行を最初から飛ばさず最後まで使う指定。

every 行刻み:ブロック刻み:初期行:初期ブロック:終了行:終了ブロック

行刻み       何行毎に飛ばすか   (指定無しは飛ばさない)
ブロック刻み 何ブロック毎に飛ばすか(指定無しは飛ばさない)
初期行       何行目から使うか   (指定無しは最初の0行目から使う)
初期ブロック 何ブロック目から使うか(指定無しは最初の0ブロック目から使う)
終了行       何行目まで使うか   (指定無しは最後の行まで)
終了ブロック 何ブロック目まで使うか(指定無しは最後のブロックまで)

plotの基本

次のようなファイル tmp.trjがあったとする。

  0.03125    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    ...
  0.06250    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    ...
  0.09375    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    0.00000    ...
  ...

1列と2列、1列と9列、1列と10列、を使って3本のグラフを描く。

$ gnuplot
gnuplot> plot "tmp.trj" u 1:2 w l, "tmp.trj" u 1:9 w l, "tmp.trj" u 1:10 w l

または、リモートログイン時は
(たとえば windows PC(ホスト名がhogehoge)から端末で 別のマシンにログインしており、 別のマシンで実行した gnuplotのグラフをwindows PCに表示したいとき)

$ DISPLAY=hogehoge.eit.hirosaki-u.ac.jp:0 gnuplot

もしくは、sshリモートログイン時に -Yオプションを付けていた場合などは、単に、

$ gnuplot

とする。

凡例の位置・フォントサイズを変更する

set key at 3.5,-5     #座標は凡例の右下の位置、に表示らしい
set key right bottom  #右下に表示(bottom以外に、top, centerなどあり)
set key font "Arial,13"  #ほかに"Times New Roman,12"なども可

関数の定義

deg(x)=180.0/pi*x

rad(d)=pi/180.0*d

degree=pi/180  #と変数定義するとmathematicaと同様な扱い

その他

  • 文字列の連結は「.」
  • plotの入力ファイルをコマンドラインの出力に替える
(例)plotでファイルの先頭と末尾の何行かを除く
plot '< cat hogehoge.sup | tail -n +5 | head -n -2'
  • plot 'sample2.dat' using 1:2 smooth csplines with lines #スプライン補間
  • 変数確認や初期化に関する事柄
clear  # グラフの消去
reset  # 設定の初期化(表示領域をデフォルトにもどす)
show variable  #ユーザ変数の一覧
undefine fn  # 変数の取消
show format # 表示形式の表示
  • 略称など
using -> u
with lines -> w l
w lp -> with linespoints
lw 2  #ライン幅
ps 2  #プロット点サイズ
  • ターミナルの変更と、出力
set terminal   #ターミナルの一覧を表示する(説明つき)

show terminal
show term      #利用中のターミナルの種類を知る
set term postscript [colorなど]    #変更
set term png
set term x11

show output    #利用中の出力先を知る
set output "output.ps" #-> lprプリンタ          #印刷?

save "output.plt" #-> load "output.plt" できる  #現在の状態を保存
  • wxtターミナル

Ubuntu 16.04 では 14.04のように wxt は簡単には利用できなくなった。 代わりに gnuplot-qt パッケージをインストール(gnuplot-x11 と入れ替える)して、 set term qt を使うのが良い。

  • GIFアニメーションからmp4形式の作成
gnuplotで作成したGIFアニメ→adobe premiere elementsで単に読み込む。
→右上の書き出し・配信→携帯電話・携帯プレイヤ→種類はApple TV,iPad,iPhone4の720p 29.97で良い。

Premiereの操作はAdobe Premiere elementsの操作などを参照。

アニメーション表示やgif動画の作成

  • スクリプト例
# 2018/12/16

f2='demoga4b.vCFEM2.dx010dt0.0005.trj-linex'
t0 = 0.0
x0 = 5.0
px = 4.0
dt       = 0.0005
maxstep  = 150000
trjevery =   1000

nframe = maxstep / trjevery  # number of frame data

set term qt enhanced size 1200,400  #ターミナルqtのキャンパスサイズを指定

#gifアニメを作るときは、次の2行をコメントアウトを外す。
# set term gif enhanced animate delay 20 optimize size 1200*1.5,400*1.5
# set output "temprev.gif"

do for [ frame = 0 : nframe : 1 ] {
#--------------------------------------------------------------------------------
time   = t0 + frame * trjevery * dt

packetcenter = x0 + px * time
packetwidth  = 2.5 + 5.0 * time

xrangemax = packetcenter + packetwidth
xrangemin = packetcenter - packetwidth
yrangemax = 1.0
yrangemin = -yrangemax

set xrange [ xrangemin : xrangemax ]
set yrange [ yrangemin : yrangemax ]  #自動にするときはコメントアウトする
set grid

set title  font "Arial,16"; set title  "Time evolution of a gaussian wave packet"
set xlabel font "Arial,16"; set xlabel "x-axis (bohr)"
set ylabel font "Arial,16"; set ylabel "{/Symbol y}(x,t)"
set key    font "Arial,16"; set pointsize 8  #凡例文字、ポイントサイズ
set tics   font "Arial,12"; set format x "%5.0f"; set format y "%5.2f" #目盛文字、フォーマット

set mytics 5  #副目盛の数
set grid mytics

strtitle(frame) = sprintf("t=%5.3f au ( frame %d )", time, frame )
unset label
set label f2               font 'Arial,11'  at graph 0.02, 1.03
set label strtitle(frame)  font 'Arial,18'  at graph 0.55, 0.70
set label "{/Symbol y}(x,t=0) =(2 {/Symbol p} w^2)^{-1/4} exp[-(x-x_0)^2/(4w^2) + i p_x(x-x_0)]" \
          font 'Arial,18'  at graph 0.02, 0.9
set label                                                          "where  x_0=5, p_x=4, w=0.5" \
          font 'Arial,14'  at graph 0.03, 0.8

f2ps=1
f2pe=5
plot \
     f2 u f2ps:f2pe+1  every ::0:frame::frame w l    lc rgb "blue"   title "  Re[{/Symbol y}(x,t)]", \
     f2 u f2ps:f2pe+2  every ::0:frame::frame w l    lc rgb "green"  title "  Im[{/Symbol y}(x,t)]", \
     f2 u f2ps:f2pe+0  every ::0:frame::frame w l    lc rgb "red"    title "  |{/Symbol y}(x,t)|^2", \

#pause 0.1
#pause 0.1
if( frame ==  0 ) {
    pause 2.0
}
if( frame <= 10 ) {
    pause 0.25
}
if( frame <= 20 ) {
    pause 0.25
}
if( frame ==  nframe ) {
    pause 2.0
}
#pause -1

#--------------------------------------------------------------------------------
}
unset output  #STDOUTになる(see: show output)
       
print 'the end'
print 'to reread, press enter' ; pause -1 ; reread
pause -1

リンク・マニュアルなど

色名の一覧など

色の一覧 (Color-name and point-shapelists for gnuplot 4.6.3)
こちらのページを参照のこと。

   colorname-list2modthumb.png

線の太さやマーカ
$ gnuplot
gnuplot> test

nowrap

線やマーカーの変更(w l や w lp のオプション)

lt 0  点線,    lw 2 太線
pt 2  ×印,    ps 2 大きい印サイズ
pt 6  ○印,    ps 1 デフォルトの印サイズ(状況によって異なるようだ)
pt 1  +印,

gnuplotのグラフウィンドウ上での操作コマンド

(gnuplot 4.6 patchlevel 6 の場合)

拡大縮小: Ctrl+マウス中ホイール、 - と +,= キーでも可(カーソルを中心に拡大縮小)
上下移動: マウス中ホイール、↑,↓キーでも可
左右移動: Shift+マウス中ホイール、←,→キーでも可

座標表示: マウス中クリック
  デフォルトの set mouse nolabels の状態で、下記 b やウィンドウの最大化で表示座標は
  クリアされる。 set mouse labels をするとクリアされない(nolabelsの状態にしても
  クリアされない)。表示座標を消す場合は、labelsの状態で、先頭の +印のところを
  Ctrl+マウス中クリックする。
枠非表示のトグル: b
ルーラー表示のトグル: r キー(カーソルのある位置に表示)

操作方法の表示: hキー(端末に表示)

前の表示/次の表示へ戻る/進む: p,n(下図のアイコン -,+とも同じ)
最初の表示に戻る: u

マウスで囲んだ領域を拡大表示: マウス右ドラッグ後、左クリック
                              (ヒント:pキーで元に戻り、nキーで拡大表示に戻れる)
nowrap

[ref] https://yutarine.blogspot.com/2019/06/gnuplot-keyboard-mouse-operations.html


添付ファイル: filegnuplot_menu_icon.png 380件 [詳細] filelinepointstyles_bygnuplot.png 628件 [詳細] filecolorname-list2modthumb.png 206件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-12-21 (木) 17:46:53 (150d)