初心者から中級者のための実践記事
1. 基礎編(CSVファイルと代表的なグラフ)
1.1 ヘッダ付きCSVファイルを扱う
gnuplotでは、デフォルトで数値列のみを読み込みます。ヘッダ行(カラム名)を持つCSVファイルを扱う場合は、以下の設定が便利です。
set datafile separator ","
set key autotitle columnheadset 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,951.2 複数列を使った折れ線グラフ(固定列指定)
set datafile separator ","
set key autotitle columnhead
plot "data.csv" using 0:2 with linespoints, \
"data.csv" using 0:3 with linespointsusing 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 linespointsfor [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 leftset key outside right top box | 凡例の位置と枠の有無 |
| グリッド | set gridset 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,600set output "graph.png" | PNG, PDF, SVG などに保存 |
| マルチプロット | set multiplot layout 2,1 | 複数グラフを並べて表示 |
| 軸範囲 | set xrange [0:100]set yrange [0:50] | 表示範囲を制限 |
| 対数軸 | set logscale y | Y軸を対数スケールに |
| ラベル追加 | set label "注目点" at 5,10 | 任意の位置に注釈 |
| 矢印描画 | set arrow from 2,3 to 8,15 | 強調用の矢印を追加 |
| 色付け領域 | set style fill solid 0.3plot ... with boxes | 棒グラフや領域塗りつぶし |
| フォント変更 | set terminal pngcairo font "Arial,14" | 出力フォントとサイズを変更 |
| マージン | set lmargin 10set rmargin 5 | グラフ領域の余白を調整 |
| 軸ラベル回転 | set xtics rotate by -45 | X軸ラベルを斜めに表示 |
| 色分け | 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 histogramsplot "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 linessplot→ 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