起動方法
windows版
- 公式サイト(http://www.gnuplot.info/)から最新版をダウンロードし、適当な場所に解凍。解凍されたフォルダ内のbinaries内のwgnuplot.exeを起動するとfig.1のようなターミナルが立ち上がる。
- フォントが読めない場合は、ターミナル内で右クリック->choose font…からフォントを変更した後、再度ターミナル内で右クリックしてupdate C:\...をクリック(fig.2参照)
![]() |
fig.1 windows版gnuplotの起動画面 |
![]() |
fig.2 フォントの変更方法 |
Linux版
- ターミナルを起動してgnuplotとタイプすると立ち上がる。
![]() |
fig.3 Linux版gnuplotの起動画面 |
基本的なグラフ描画方法
まずはプロットしてみる
- ターミナル内でplot sin(x)やplot exp(x)と入力するとy=sin(x)、y=exp(x)の関数が描画される。
$ plot sin(x) |
![]() |
fig.4-1 y=sin(x)のグラフ |
$ plot exp(x) |
![]() |
fig.4-2 y=exp(x)のグラフ |
軸の範囲を設定する
- 軸の範囲の設定方法は2種類ある。
- 特定のグラフで範囲を決める場合はplotの後に[x軸の範囲][y軸の範囲]を入れる。
- この場合は軸の範囲を指定したグラフでは範囲が指定されるが、次に描画したグラフでは軸の範囲が指定されない。
- 以降に描画するグラフの範囲を全て固定する場合は、plotの前にset xrangeまたはset yrangeで指定する。
$ plot [0:10][-2:2] sin(x) |
$ set xrange [0:10] $ set yrange [-2:2] $ plot sin(x) |
![]() |
fig.4-3 軸範囲の変更 |
データからプロットする
- 関数ではなく、データをプロットする。
- gnuplotで扱うことのできるデータ区切りは、スペース区切り、カンマ区切り、タブ区切りなど一般的な区切りであれば全て可能。
- データをsample1.txt(1列目:時間[s]、2列目:圧力[Pa]、3列目:抗力[N]のデータ)
- 通常にプロットすると、1列目(時間)と2列目(圧力)をプロットすると以下のグラフが表示される
- ※使うデータ列が無指定の場合は1列目をx軸、2列目をy軸として表示する。
- 1列目をx軸、3列目をy軸にするには、using 1:3と入れる
$ plot "sample.txt" with line |
![]() |
fig.5 圧力の時間変化 |
$ plot "sample1.txt" using 1:3 with line |
![]() |
fig.6 抗力の時間変化 |
各軸のラベルを設定するには?
- plotする前に軸の設定を行う。
- このままでは見難いため、x軸を[ms]、y軸を[kPa]に変更してみる
- このままでは凡例がわかりにくいため、凡例の文字を変更する
- 凡例の文字はtitle "凡例"で変更できる
$ set xlabel "Time [s]" $ set ylabel "Pressure [Pa]" $ plot "sample1.txt" using 1:2 with line |
![]() |
fig.7 圧力の時間変化(軸設定) |
$ set xlabel "Time [ms]" $ set ylabel "Pressure [kPa]" $ plot "sample1.txt" using ($1*1000):($2/1000) with line |
![]() |
fig.8 圧力の時間変化(軸範囲設定) |
$ plot "sample1.txt" using ($1*1000):($2/1000) with line title "Pressure" |
![]() |
fig.9 圧力の時間変化(凡例設定) |
グラフ中の線の太さ、色を変更するには?
- linewidth, linecolorで指定する。
$ plot "sample1.txt" using ($1*1000):($2/1000) with line linewidth 2.5 linecolor 1 title "Pressure" |
![]() |
fig.10 圧力の時間変化(線種の変更) |
![]() |
![]() |
参考)linecolorの一覧 | 参考)point typeの一覧 |
グラフの縦横比をを変更するには?
- グラフの縦横比はset sizeで指定します。
- 指定方法は2つあって、縦横比を指定する方法と、縦と横をそれぞれ指定する方法です。
- 縦横比を指定する方法では、set size ratio 1.0(数字は横軸を1とした場合の縦軸の比率)とします。
- 縦と横をそれぞれ指定する方法では、set size 1,0.5(出力画像に対する横軸のサイズ、縦軸のサイズ)とします。
- 2つ目の方法では、出力画像のサイズによって縦横比が変わってくるので、set size ratioで指定するのがおすすめです。
$ set size ratio 1 $ plot sin(x) |
![]() |
fig.11 縦横比1:1のグラフ |
グラフ中の線の種類を変更するには?
- gnuplotには標準でlintypeを設定できる。
- linetypeでは通常はカラーで色の異なる表示になるが、postscriptファイルでmonochromeを指定した場合には線種の変更になる。
複数のグラフを出力する
同じグラフに複数のグラフを出力する
- プロットするデータを","(カンマ)で区切って記述する
- プロットしたグラフの上に"replot"で上書きする
$ plot sin(x),cos(x) |
$ plot sin(x) $ replot cos(x) |
![]() |
fig.12 複数のグラフ |
グラフ中の様々な処理
補助線を引く
- set grid xtics/yticsで設定できる
$ set grid xtics
$ set grid ytics
$ plot sin(x)

fig.13 グリッドをつけたグラフ
軸ラベルを消す
- set format x/y ""で軸ラベルを消去
$ set format x ""
$ set format y ""
$ plot sin(x)

fig.14 軸ラベルを消去グラフ
予め線種などを指定しておく
- set style lineでグラフの線やポイントを事前に設定しておくことができるため、統一的な描画をするのに使用する
- 線で描画する場合の例
$ set style line 1 lt 1 lc rgb "black"
$ set style line 2 lt 1 lc rgb "red"
$ plot sin(x) with lines linestyle 1, cos(x) with lines linestyle 2

fig.15 線種を指定して描画したグラフ
- ポイントで描画する場合の例
$ set style line 1 pt 6 ps 2 lt 1 lc rgb "black"
$ set style line 2 pt 6 ps 2 lt 1 lc rgb "red"
$ plot sin(x) with lines linestyle 1, cos(x) with lines linestyle 2

fig.16 ポイントの種類・色を指定して描画グラフ
グラフを画像で出力するには?
- 出力を変更するにはset terminalで変更できます。
- 使用できるターミナルは'png'、'postscript'、'png'、emf'などいろいろあります。
- ここでは著者が頻繁に使うpostscriptまたはpngのterminal設定の一例を紹介しますが、詳しくは自分で調べましょう。
- postscriptのフォントArial
$ set terminal postscript enhanced eps color "Helvetica-Bold" 24
- postscriptのフォントTimes
$ set terminal postscript enhanced eps color "Times-Bold" 24
- 小技集の自作バッチファイルでemfにすることで上記のフォントになります。
- terminalを希望の形式に変更した後、plotを記述する前に出力ファイル名を指定します。
$ set terminal postscript enhanced eps color "Times-Bold" 24
$ set output "output.eps"
$ plot sin(x)
- 上記の3つを入力すれば、y=sin(x)の曲線がoutput.epsとして出力されます。
- 備考) terminalオプションのenhancedはenhanced postscriptファイル=epsファイル、color(monochrome)はグラフの色指定、フォントの後の文字はフォントサイズの指定。
略語集
- plot -> p
- xrange -> xr
- yrange -> yr
- xlabel -> xl
- ylabel -> yl
- linewidth -> lw
- linecolor -> lc
- linetype -> lt
- with lines-> w l
- with points -> w p
$ set grid xtics $ set grid ytics $ plot sin(x) |
![]() |
fig.13 グリッドをつけたグラフ |
$ set format x "" $ set format y "" $ plot sin(x) |
![]() |
fig.14 軸ラベルを消去グラフ |
$ set style line 1 lt 1 lc rgb "black" $ set style line 2 lt 1 lc rgb "red" $ plot sin(x) with lines linestyle 1, cos(x) with lines linestyle 2 |
![]() |
fig.15 線種を指定して描画したグラフ |
$ set style line 1 pt 6 ps 2 lt 1 lc rgb "black" $ set style line 2 pt 6 ps 2 lt 1 lc rgb "red" $ plot sin(x) with lines linestyle 1, cos(x) with lines linestyle 2 |
![]() |
fig.16 ポイントの種類・色を指定して描画グラフ |
$ set output "output.eps"
$ plot sin(x)