gnuplotのinstall方法
(1)Homebrewをインストールする.開いたターミナル上で、以下の一行をコピーペーストして、⏎キーを押してください。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
(2)ターミナルで
brew install gnuplot --with-aqua --with-x11
https://hatarakutaitsu.hatenablog.com/entry/2019/09/11/141842
$ brew install gnuplot を実行する. インストール終了後にSummaryの次の行に/usr/local/Cellar/gnuplot/5.2.8/bin/gnuplotのように表示されるはずであり, これはc++でgnuplotを使うときに必要なのでメモしておく.
ここにサンプルコードが置いてある
ここgnuplotの実行
hoge.gpというバッチファイルを作る.gnuplot hoge.gpで実行.
ファイルの書き方
1.プロットの設定2.プロット
pause -1 "press [Enter] key or [OK] button to quit"
#------------保存------------------------
set terminal postscript eps enhanced solid color
set output "gazou.eps"
replot
unset output
二次元陰極関数
f(x,y)=0のように表現される関数を陰極関数という.gnuplotにはこの陰極関数をプロットする機能はない.しかし, Z=f(x,y)の3次元プロットを行い, Z=0の等高線のみを表示することにより, プロットできる.
set xrange [-2:2]
set yrange [-1:1]
unset clabel
set isosamples 128,128
set nosurface
set view map
set contour
#set cntrparam cubicspline#等高線の補完をすると, うまくつながることがある.
set cntrparam levels discrete 0.0
set samples 256
splot (x**2+y**2)**2-2*(x**2-y**2) notitle
pause -1 "press [Enter] key or [OK] button to quit"
分離記号の変更
gnuplotの標準の分離記号はスーペースであるため, コンマやタブを使う場合は,set datafile separator ","
や,
set datafile separator "タブ"
データファイルにコメントがある場合
set datafile commentschars "#!%"を書けば, #, !, %がコメントアウト記号として使える.
データの欠損
set datafile missing "non"データファイル中にデータの欠損があるところはnonとかく.
列の選択
plot "sample.dat" using ($1):(sin($2)) with lines nontitleフィッティング
# --- fitting ----f1(x) = a1*exp(-0.5*(x-m1)*(x-m1)/(s1*s1))
f2(x) = a2*exp(-0.5*(x-m2)*(x-m2)/(s2*s2))
fit f1(x)+f2(x) "fit_sample.dat" using 1:2 via a1, m1, s1, a2, m2, s2
# --- plot ---
set pointsize 0.3
set samples 512
set xrange [-10:10]
plot "fit_sample.dat" using 1:2 with points pointtype 7 notitle,\
f1(x) with lines notitle,\
f2(x) with lines notitle,\
f1(x)+f2(x) with lines notitle
pause -1 "press [enter] key or [OK] button to quit"
3次元の最小二乗法
DATA = "sample.dat"set hidden3d trianglepattern 7 offset -1
f(x,y) = a*x*x + b*y*y
fit f(x,y) DATA using 1:2:3:(1) via a,b
splot DATA with points pointtype 7 notitle,\
f(x,y) with lines notitle
pause -1 "press [Enter] key [OK] button to quit"
三次元の場合には, 4つのデータ(x,y,z,s)が必要になる. 最後のsは, データの誤差の標準偏差です. これはデータの重み付けに使われ1/s^2が重みになる. 重みのないデータの場合, 定数を設定する. s=1は全てのデータが全て同じ重みい .
重みに関して
最小二乗法によるフィッティングではデータの重み付けができる. fitコマンドは二次元の場合は, 重みのデータを要求しないが, 重みを含めることもできる.(x,y,s). sが小さいほど重くなる.重みがあると, \chi^2=\sum_{i=1}^{N}\omega_i (y_i-y(x_i))^2 を最小にする.
set terminal wxt enhanced
DATA = "sample.dat"
# ---- fitting ------------
f(x) = a*x**b
fit f(x) DATA using 1:2 via a,b
g(x) = c*x**d
fit g(x) DATA using 1:2:($2) via c,d
set label "non-weighted fit" at first 4.0e-4, first 1.0e-6 rotate by 31
set label "weighted fit" at first 1.0e-3, first 7.5e-8 rotate by 36
set xrange [1e-4:1]
set logscale x
set format x "10^{%L}"
set yrange [1e-9:1]
set logscale y
set format y "10^{%L}"
plot DATA using 1:2 with points pointtype 7 notitle,\
f(x) with lines notitle,\
g(x) with lines notitle
pause -1 "press [Enter] key or [OK] button to quit"
5-6行目は重みのないフィッティング. それに対し, 7-8行目は, 重みをつけたフィッティング.
重みづけの指定はy座標である.
フィッティングの詳細
gnuplotの最小二乗法はlevenberg-marquadt法の繰り返しアルゴリズムにより誤差の二乗和が最小になるフィッティングパラメータ の値を求める. 繰り返しの終了条件は,1. 繰り返し計算により残差の二乗\chi^2がFIT_LIMIT以下になる.
2. 繰り返し回数が, FIT_MAXITERを超える.
である. 繰り返し回数を無限にする場合, FIT_MAXITER=0とする.
levenberg-marquadtアルゴリズムの\lambdaはFIT_LAMDA_FACTOR= で設定できる.
補完法
直線補完データのソートが勝手に行われる.
plot "sample.dat" using 1:2 smooth unique with lines title "unique"
3次スプライン補完
データのソートが勝手に行われる.
plot "sample.dat" using 1:2 smooth cspline with lines title "cspline
重み付き3次スプライン補完
重みが大きいほど, 点の近くを通る.
plot "sample.dat" using 1:2:(100) smooth acspline with lines title "acspline"
ベジェ曲線
データのソートを勝手に行うsbezierplot "sample.dat" smooth sbezier with lines title "sbezier"
データのソートを勝手に行わないbezier
plot "sample.dat" smooth bezier with lines title "bezier"
数値出力
ste table "出力ファイル名"plot f(x) または, splot f(x,y)
unset table
数値出力の例
pot(x,y)= x*yBx(x,y)= -y; By(x,y)= -x
set xrange [-2:2]
set yrange [-2:2]
#計算結果をdatファイルに保存する.
set isosamples 16; set samples 16
set table "potential.dat"
splot pot(x,y) #x y zをファイルに書き込む.
unset table #必ずtableをunsetする
c=0.1
plot "potential.dat" using 1:2:(c*Bx($1,$2)):(c*By($1,$2)) with vectors notitle
pause -1 "press [Enter] key or [OK] button to quit"
変数
gnuplotは型指定がなく, 変数を定義できる.定義済み数学変数
piNaN
exp(1)
単項演算子
データファイルのカラムの値$3
階乗
a!
二項演算子
べき乗a**b剰余a%b
==, !=, <=, ||,&&, なども使える.
三項演算子
aが真ならbを実行, 然もなくばca?b:c
2分岐
if(条件){} else if{} else{}繰り返し処理
for[i=0:i=100:1]#c言語の「;」と違い「:」を用いること.
while
while(式){}c言語で使う方法
#include#define GNUPLOT "/usr/local/Cellar/gnuplot/5.2.8/bin/gnuplot" //gnuplotのパスを指定
FILE *gp ; //gnuplotのパイプ
if((gp=popen(GNUPLOT " -persist", "w"))==NULL){
cout <<"パイプがひらけません\n";exit(1) ;}
//gnuplotで描写
fprintf(gp,"set title 'Trigonometric function(sin x)'\n");
fprintf(gp,"plot sin(x)\n");
fprintf(gp,"set terminal postscript eps enhanced solid color\n");
fprintf(gp,"set output \"gazou.eps\"\n");
fprintf(gp,"replot\n");
fprintf(gp,"unset output\n");
pclose(pipe);
データを読み込みたい場合は, 計算値をスペース入りファイルに出力し,
fprintf(gp,"plot 'sample.dat' with linespoints notitle\n");
gnuplot.c
8
フォント
set tics font "Times New Roman,25" # 目盛りのフォントの変更set xlabel font "Times New Roman,25" # xlabelのフォントの変更
set ylabel font "Times New Roman,25" # ylabelのフォントの変更
set zlabel font "Times New Roman,25" # zlabelのフォントの変更
set key font "Times New Roman,25" # 凡例のフォントの変更
ギリシャ文字
{/Symbol アルファベット}図中に文字
set label 1 "ABCDEFG" at -7.5, 3.5 set label 数字 "BCDEFG" at 座標拡張文字
a^ba_b
a@^b_c
&{abc}#文字abc分の長さのスペース
色
rgbcolor "colorname"枠線の太さ
set linewidth 1.8軸
# -------- set x axis --------set xlabel "x"
set logscale x#対数軸
set xrange [1e-2:1e+4];
set xtics 1e-2,10 nomirror scale 2,1#nomirrorで2軸にする場合対岸の軸を消す
set mxtics 10#小目盛の刻み数
set format x "%.0e"#軸の数字の書き方
# -------- set y axis --------
set ylabel "y"
set logscale y
set yrange [1e-1:1e+3]
set ytics nomirror scale 2,1
set format y "10^{%L}"
# -------- set x2 axis --------
set x2label "x2"
set x2range [-1:1]
set x2tics -1,0.2#大目盛の刻み間隔15,5,10なら左端が-5で0.5ずつ増加し、右端が10
set format x2 "%.1f"
# -------- set y2 axis --------
set y2label "y2"
set y2range [0:1]
set y2tics 0,0.2
set format y2 "%.1f"
目盛のフォーマット
set format x "%.1l{/Symbol \246}10^{%L}"%.1l 仮数部
10^{%L} 指数部
{/Symbol \246}は乗算の記号
set format x "%.0P{/Symbol p}"#π目盛
対数表示
set format x "10^{%T}"
set format y ".1E"
set format z "%g"
gridの設定
set grid#主目盛だけset grid mxtics #noxticsなどをかくと消せる
凡例の設定
unset key #凡例なしset keyに続けて
inside#枠内sutsideで枠外
at 4.0,5.0#座標(4.0,5.0)に凡例をおく
Left #凡例が左揃え,右揃えはRight
reverse #凡例のタイトルと線が入れ替わる.
点種, 線種
set style line 1 linecolor rgb "dark-pink" linewidth 2 pointtype 6 pointsize 1.5set style line 2linetype 3 linecolor rgb "bark-pink" linewidth 2 pointtype 7 pointsize 1.5
plot sin(x) with linespoints linestyle 1,\
cos(x) with linespoints linestyle 2

マルチプロット
ファイル3.25,4.28から4.30統計
stats "hoge.dat"インストールエラーが出る対処法
brew install gnuplot --with-aquaterm --with-x11https://hatarakutaitsu.hatenablog.com/entry/2019/09/11/141842
