gnuplot

unix

初心者から中級者のための実践記事


1. 基礎編(CSVファイルと代表的なグラフ)

1.1 ヘッダ付きCSVファイルを扱う

gnuplotでは、デフォルトで数値列のみを読み込みます。ヘッダ行(カラム名)を持つCSVファイルを扱う場合は、以下の設定が便利です。

set datafile separator ","
set key autotitle columnhead
  • set datafile separator "," → 区切り文字をカンマに指定
  • set key autotitle columnhead → 1行目(ヘッダ)を凡例ラベルとして利用

例:data.csv

Date,Sales,Profit,Cost
2025-01-01,100,20,80
2025-01-02,150,30,100
2025-01-03,120,25,95

1.2 複数列を使った折れ線グラフ(固定列指定)

set datafile separator ","
set key autotitle columnhead

plot "data.csv" using 0:2 with linespoints, \
     "data.csv" using 0:3 with linespoints
  • using 0:2 → X軸にレコード番号、Y軸に2列目(Sales)
  • using 0:3 → Y軸に3列目(Profit)

※列数が増えると追記が必要です。


1.3 複数列を自動で描画する(for文)

列数に依存せず、すべての数値列を自動で描画するには for 構文を使います。

set datafile separator ","
set key autotitle columnhead
set xdata time
set timefmt "%Y-%m-%d"
set format x "%m/%d"

# 2列目以降を自動で描画
plot for [col=2:*] "data.csv" using 1:col with linespoints
  • for [col=2:*] → CSVの2列目から最後の列まで繰り返し描画
  • using 1:col → 1列目をX軸(Date)、col列をY軸に指定
  • autotitle columnhead → 凡例をヘッダ名から自動取得

👉 列が増えても追記不要で自動対応できます。


まとめ(基礎編)

  • CSVのヘッダを自動で凡例に利用可能
  • 固定列指定よりも for を使うと効率的
  • 日付や複数系列のグラフを簡単に扱える

2. 応用編(設定一覧・カスタマイズ)

2.1 よく使う設定一覧(チートシート形式)

設定カテゴリコマンド例説明
軸ラベルset xlabel "時間"
set ylabel "温度"
軸に名前をつける
タイトルset title "実験データ"グラフ上部にタイトルを表示
凡例set key top left
set key outside right top box
凡例の位置と枠の有無
グリッドset grid
set grid xtics ytics lw 1 lc rgb "#aaaaaa"
補助線を表示・カスタマイズ
線スタイルset style line 1 lc rgb "blue" lw 2色・太さを指定
点スタイルplot ... with points pt 7 ps 1.5点の種類とサイズを調整
出力形式set terminal pngcairo size 800,600
set output "graph.png"
PNG, PDF, SVG などに保存
マルチプロットset multiplot layout 2,1複数グラフを並べて表示
軸範囲set xrange [0:100]
set yrange [0:50]
表示範囲を制限
対数軸set logscale yY軸を対数スケールに
ラベル追加set label "注目点" at 5,10任意の位置に注釈
矢印描画set arrow from 2,3 to 8,15強調用の矢印を追加
色付け領域set style fill solid 0.3
plot ... with boxes
棒グラフや領域塗りつぶし
フォント変更set terminal pngcairo font "Arial,14"出力フォントとサイズを変更
マージンset lmargin 10
set rmargin 5
グラフ領域の余白を調整
軸ラベル回転set xtics rotate by -45X軸ラベルを斜めに表示
色分けset palette defined (0 "blue", 1 "red")値に応じて色を付与

2.2 カスタマイズ例(実践)

凡例・グリッド・出力調整

set key outside right top box
set grid xtics ytics lw 1 lc rgb "#cccccc"
set terminal pngcairo font "Arial,14"
set output "custom.png"

棒グラフを半透明に

set style fill solid 0.5
plot "data.csv" using 2:xtic(1) with boxes

複数グラフを上下に並べる

set multiplot layout 2,1
plot "data.csv" using 1:2 with lines title "Sales"
plot "data.csv" using 1:3 with lines title "Profit"
unset multiplot

グラフ形式変更の方法(一覧表)

形式コマンド例説明
折れ線plot "data.csv" using 1:2 with lines連続した線で描画
散布図plot "data.csv" using 1:2 with points点だけで描画
折れ線+点plot "data.csv" using 1:2 with linespoints線と点を両方表示
棒グラフset style data histograms
plot "data.csv" using 2:xtic(1)
棒で表現(ヒストグラム)
箱ひげ図plot "data.csv" using (1):(2):(3):(4):(5) with candlesticks統計的な分布可視化
面グラフplot "data.csv" using 1:2 with filledcurves面を塗りつぶして表示

3次元プロットの方法

基本的な3Dプロット
splot "data3d.dat" using 1:2:3 with lines
  • splot → 3D描画用コマンド
  • using 1:2:3 → X, Y, Z 軸に列を割り当て
  • with lines → 3Dメッシュの線
サーフェス(面)表示
set hidden3d
splot "data3d.dat" using 1:2:3 with lines
カラーマップ(ヒートマップ風)
set pm3d
splot "data3d.dat" using 1:2:3 with pm3d
等高線表示
set contour base
set view map
splot "data3d.dat" using 1:2:3 with lines
タイトルとURLをコピーしました