#author("2026-05-15T11:06:20+09:00","default:okazaki","okazaki")
#author("2026-05-15T11:07:37+09:00","default:okazaki","okazaki")
#menu(howto/MenuBar)
* howto/gnuplot-tips [#s15a9848]
#contents
** 一行メモ [#gf844b58]
- gnuplot -e "file='some_dir/name_of_file'; legend='label of data'" test2.gp  で、スクリプトで使っている変数に値を代入できる --  &new{2015-04-01 (水) 13:55:28};
- lt 0 lw 2 (線の種類、太さ)  pt 6 ps 2 (プロットの種類、大きさ) rgb "red" (色) --  &new{2017-11-23 (木) 11:40:47};
- 挙動がわかりづらい理由:
-- オプションとその引数に順番はある。引数とオプションの違いを把握しておらず、逆の順番などにすると正しく処理されない。間違ったものはエラーを出さずに無視される。と、言うあたりに理由があると思われる。
-- gnuplotは、terminalの種類により別々の補助関数が実行されるような形らしい。terminal毎の統一は完全ではない。symbolではダメでSymbolである必要があるなど、terminal毎にちがう。
-- フォントの種類などはterminal毎に完全に別々で(かつ、どこのXサーバを使っているかの表示する環境にも依存)、qtで見れるように作ってもgifではそうはならない(qtでも、表示する環境が違うと、ちがうように表示される)。 --  &new{2018-12-16 (日) 09:27:41};
- メモ --  &new{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文 --  &new{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)" } }
- 目盛りのフォントを大きくするなど --  &new{2019-09-09 (月) 10:55:33};
-- set lmargin 10; set tics font "Arial,20"  #左のマージンを広げ、目盛りフォントを大きく --  &new{2019-09-09 (月) 10:56:14};
-- set xlabel offset 0,-1; set ylabel offset -2,0  #軸ラベルのオフセット(未確認) --  &new{2019-09-09 (月) 10:57:11};
-- set bmargin  3; set xtics offset 0,-0.4  #下のマージンを広げて、目盛り文字をちょっと下げる --  &new{2019-09-09 (月) 11:07:11};
-- set tmargin  3;  #上のマージンを広げる --  &new{2019-09-09 (月) 11:07:33};
-- set ytics offset 1.8,0    #y軸のメモリの数字をちょっと右にずらす --  &new{2019-09-09 (月) 11:24:50};
- linuxコマンドの実行 --  &new{2019-09-13 (金) 11:28:48};
 gnuplot> ! ls
- set macro をすることで次が可能になる --  &new{2020-04-12 (日) 12:57:35};
 pstyle="lp"
 plot  hogehoge  @pstyle  hogehoge
- XQuartz 2.7.8 (xorg-server 1.16.4) だと set term x11 でウィンドウを引っぱってこれる --  &new{2020-11-25 (水) 21:56:09};
-- 複数ディスプレイを利用している場合に、X11(XQuartz)のウィンドウが領域外に置かれてしまう場合 --  &new{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できるようである --  &new{2020-11-25 (水) 21:56:44};
-- set datafile commentschars "#@&" としてから、plotする。 https://manual.gromacs.org/current/how-to/visualize.html
-- 例
 $ gnuplot
 > set datafile commentschars "#@&"
 > plot 'file.xvg' u 1:CN w l    #CNはデータの列番号
-- xmgrace -nxy hoge.xvg   複数のデータを含む xvgファイルをプロットする https://www.systutorials.com/docs/linux/man/1-xmgrace/ --  &new{2020-11-25 (水) 22:08:01};

- gnuplot script1 script2 ... とすると、順番にscript1, script2, ...が実行される。スクリプトに - を指定するとそこは標準入力になる(exitしてもgnuplotのプロンプトが返ってこないときがある? いったんbgにして fgにすると - 以降は無視されたり?、いろいろバグる? )。 --  &new{2022-07-21 (木) 22:22:45};

- たとえば複数のファイルの6列目のグラフの重ね合わせ、10列目の重ね合わせ、8列目の重ね合わせ、を順次表示するgnuplotを呼び出すbashスクリプト --  &new{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) --  &new{2022-09-10 (土) 21:59:17};
 set terminal qt position 10,1100; replot
 set terminal qt position 10,110; replot
 など
- 媒介変数によるグラフの描画例 --  &new{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で階段状に線を描いてくれる。 --  &new{2022-12-14 (水) 15:38:36};
- グラフを画像で保存:グラフのウィンドウで、メニューのexportアイコン→export to imageで、pngで保存 --  &new{2023-04-14 (金) 11:57:01};
- set arrow from 5.0,0.0 to 5.0,1.0; replot  #座標(5,0)-(5,1)間に(縦に)線を描く --  &new{2023-12-18 (月) 11:21:05};
- gnuplotで plot時、 Warning: slow font initializationqt_processTermEvent received a GE_fontprops event. This should not have happened が出た場合、フォントキャッシュが遅いだけ。プロットは崩れていると思うが、replotで正しくプロットされる。または、gnuplot --slow で起動すると正しくプロットされる。 --  &new{2024-12-18 (水) 19:40:10};
- set y2tics; plot 'temp.txt' u 1:2 w lp axes x1y1, 'temp.txt' u 1:3 w lp axes x1y2  #y1軸とy2軸を使う --  &new{2025-02-23 (日) 00:07:32};
- plot "< grep loop matmgm.out.AN8tol6 | sed s/r=//" u 3 w lp --  &new{2025-03-03 (月) 16:49:40};
- ファイルhoge.xvgが存在したら、グラフ(棒)をプロットする --  &new{2025-06-21 (土) 01:08:44};
 rcd = system( 'test -f '.'hoge.xvg'.' && echo 0 || echo 1' )
 if (rcd == 0) {
    set style fill solid
    set boxwidth 0.8 relative
    set xtics rotate by 90 right
    plot 'hoge.xvg' u 1:2 w boxes title 'hoge'
 }
- 普通に表示場所を指定 --  &new{2025-06-22 (日) 09:40:22};
 gnuplot -e "set terminal qt position 1250,500; plot sin(x); pause -1"
 複数ディスプレイでも 右上が 0,0 である、たぶん。
- plotで using 1:0 というものも使える。 --  &new{2025-09-23 (火) 15:15:33};
- plot sin(x) with filledcurves など http://takeno.iee.niit.ac.jp/~foo/gp-jman/data/20170823/gnuplot-ja-div/node70.html --  &new{2025-10-08 (水) 09:30:21};
- plot 'points.dat' w filledcurves closed    #閉曲面として塗りつぶす --  &new{2025-10-29 (水) 22:12:38};
- set xrange[0:3]; set yrange[0:3]; splot 1/x+2*y,  "< echo 1 1 5" w impulses --  &new{2026-04-22 (水) 08:24:54};
- clear    #プロットを消す、ウィンドウが無ければ空のが現れる --  &new{2026-05-13 (水) 14:34:21};
- degree=pi/180  #と変数定義するとmathematicaと同様な扱い
- メモ --  &new{2026-05-14 (木) 16:11:12};
 set pm3d map; splot sin(x)*cos(y); set isosamples 20; replot  #z軸の値を色で塗ったヒートマップ
 set palette rgbformulae 33,13,10; replot
 
 reset; clear
 set pm3d map; set isosamples 20; set contour; set cntrparam levels auto 10
 splot sin(x)*cos(y)
 set palette rgbformulae 33,13,10; replot
 unset surface; replot
 splot sin(x)*cos(y) w l    #期待するものではない。???
- gnuplot --version --  &new{2026-05-14 (木) 23:18:16};
 allium:           gnuplot 5.2 patchlevel 6
 kalmia:           gnuplot 5.4 patchlevel 8
 プログラミング室: gnuplot 5.4 patchlevel 3
 allium:           gnuplot 5.2 patchlevel 6  , terminal qt
 kalmia:           gnuplot 5.4 patchlevel 8  , terminal qt
 プログラミング室: gnuplot 5.4 patchlevel 3  , terminal qt

#comment


//--------------------------------------------------------------------------------
* plot [#ua50b4b4]

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

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"  #例

- プロットから軸まで線を引く

 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


** 凡例の位置・フォントサイズを変更する [#cd5746fc]
 set key at 3.5,-5     #座標は凡例の右下の位置、に表示らしい
 set key right bottom  #右下に表示(bottom以外に、top, centerなどあり)
 set key font "Arial,13"  #ほかに"Times New Roman,12"なども可


//--------------------------------------------------------------------------------
* その他 [#d532e5db]

- plot 'sample2.dat' using 1:2 smooth csplines with lines  #スプライン補間 


- 変数確認や初期化に関する事柄

 clear  # グラフの消去
 reset  # 設定の初期化(表示領域をデフォルトにもどす)

 show variable  #ユーザ変数の一覧
 undefine fn  # 変数の取消


- ターミナルの変更と、出力

 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の操作>https://orange.eit.hirosaki-u.ac.jp/pw151/index.php?howto/%E3%81%9D%E3%81%AE%E4%BB%96tips#b4a7c9fb]]などを参照。

//--------------------------------------------------------------------------------
* アニメーション表示やgif動画の作成 [#af9d3d8b]
- スクリプト例

 # 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


//--------------------------------------------------------------------------------
* リンク・マニュアルなど [#j75354f7]

- Gnuplot入門(2007年) http://dsl4.eee.u-ryukyu.ac.jp/DOCS/gnuplot/gnuplot.html
- gnuplot 4.6 の日本語マニュアル http://www.gnuplot.info/docs_4.6/gnuplot-ja.pdf
-- グラフメニュー
-- 置換とコマンドラインマクロ
- gnuplotコマンド集 http://www.gnuplot-cmd.com/index.html
- 3Dプロットに関する dgrid3d による平均の説明 http://takeno.iee.niit.ac.jp/~foo/gp-jman/data/20110307/gnuplot-ja-div/node202.html


//--------------------------------------------------------------------------------

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS