ヒートマップ, 混合行列

References

概要

ヒートマップ (Heatmap), 或いは混合行列 (Confusion Matrix) などと呼ばれるあの表を色付きで図示する. 色の濃さで数の大きさを表現する. ここでは紹介しないが pm3d を使う方法もある.

Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 Row1 Row2 Row3 Row4 Col1 Col2 Col3 $data matrix rowheaders columnheaders u 1:2:3 '' matrix rowheaders columnheaders u 1:2:(sprintf("%g", $3)) 1 2 3 2 4 6 4 8 12 8 16 24 0 5 10 15 20 25 30 Heatmap with image

Data Format

行列形式 ( matrix ) でデータを与える. データ \(z\) を \(n\) 行 \(m\) 列に並べるだけでよい.

z11 z12 z13
z21 z22 z23
z31 z32 z33
z41 z42 z43

行と列にはそれぞれ見出し(ラベル)を付けることができる. 0 行目または0列目に自由にラベル文字列を入れられる.

NOTE. 行にも列にもラベルを入れたい場合は, 0行目0列目はダミーデータとして何か適当な文字列を入れておく必要がある.

xxxx Col1 Col2 Col3
Row1  z11  z12  z13
Row2  z21  z22  z23
Row3  z31  z32  z33
Row4  z41  z42  z43

matrix として読む

plot .. matrix {rowheaders} {columnheaders}
params default value explanation
rowheaders not specified specified 各行の最初の列を見出し(ラベル)として扱う
columnheaders not specified specified 最初の行を見出し(ラベル)として扱う

matrix キーワードを使うことで, 上記形式のデータを行列形式として読み込むことができる. 通常のデータ形式では各行が (x, y, z) のように座標を明示的に指定する必要があるが, matrix 形式では以下の特徴がある:

matrix 形式でデータを読み込む場合、各オプションを組み合わせて使用できる:

with image

plot .. using <x>:<y>:<z> with image {pixels}
params default value explanation
x -- (int) データの \(x\) 座標
y -- (int) データの \(y\) 座標
z -- (double) データ. 対応する色が塗られる
pixels not specified specified 各セルをピクセル単位で描画する

ただし matrix で読んだ場合は x , y には自動的に適切な値が入っている.

Examples

デフォルトの with image では色が滑らかに補完される.

$data << EOD
xxxx Col1 Col2 Col3
Row1    1    2    3
Row2    2    4    6
Row3    4    8   12
Row4    8   16   24
EOD

unset key
set title 'Heatmap with image'

set cbrange [0:30]  # the range of colorbox
set palette cubehelix start -2 cycles 0 saturation 3 gamma 3 negative  # color scheme

plot $data matrix rowheaders columnheaders u 1:2:3 with image ,\
        '' matrix rowheaders columnheaders u 1:2:(sprintf("%g", $3)) with labels
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 Row1 Row2 Row3 Row4 Col1 Col2 Col3 $data matrix rowheaders columnheaders u 1:2:3 '' matrix rowheaders columnheaders u 1:2:(sprintf("%g", $3)) 1 2 3 2 4 6 4 8 12 8 16 24 0 5 10 15 20 25 30 Heatmap with image

with image pixels では色が補完されず, 各セルごとに色が塗られる.

$data << EOD
xxxx Col1 Col2 Col3
Row1    1    2    3
Row2    2    4    6
Row3    4    8   12
Row4    8   16   24
EOD

unset key
set title 'Heatmap with image'

set cbrange [0:30]  # the range of colorbox
set palette cubehelix start -2 cycles 0 saturation 3 gamma 3 negative  # color scheme

plot $data matrix rowheaders columnheaders u 1:2:3 with image pixels ,\
        '' matrix rowheaders columnheaders u 1:2:(sprintf("%g", $3)) with labels
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 Row1 Row2 Row3 Row4 Col1 Col2 Col3 $data matrix rowheaders columnheaders u 1:2:3 '' matrix rowheaders columnheaders u 1:2:(sprintf("%g", $3)) 1 2 3 2 4 6 4 8 12 8 16 24 0 5 10 15 20 25 30 Heatmap with image

格子線 (grid)

set grid によってセル同士の境界に線を引きたい.

References

NOTE

ダメな例

$data << EOD
X    a    b    c    d    e
A 0.00 0.01 0.04 0.09 0.16
B 0.01 0.01 0.03 0.07 0.13
C 0.04 0.03 0.04 0.07 0.12
D 0.09 0.07 0.07 0.09 0.13
E 0.16 0.13 0.12 0.13 0.16
EOD

set grid front lw 1.5 lt -1 lc rgb 'white'

plot $data matrix rowheaders columnheaders w image pixels not
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 gnuplot_plot_1 A B C D E a b c d e 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16

手法

gnuplot には xy 軸とは独立に x2y2 軸がある. これはグラフの上端と右端に表示される補助的な軸. set x2tics , set y2tics で目盛りを設定できる. さらにこれらは小目盛りを持っている ( set mx2tics , set my2tics ). これらを grid の為だけに利用する.

set x2tics
set y2tics

x2y2 軸を有効にする.

set link x2
set link y2

x2y2 軸を xy 軸と連動させる.

set mx2tics 2
set my2tics 2

x2y2 軸の小目盛りを大目盛りの2分割に設定する. x2ticsy2tics が \(1\) 刻みなら, 小目盛りは \(0.5\) 刻みになる.

set xtics scale 0
set ytics scale 0

見栄えの為 xy 軸の目盛り線を消す.

set x2tics scale 0 format ''
set y2tics scale 0 format ''

見栄えの為 x2y2 軸の目盛り線を消し, 目盛りラベルも消す.

Examples

$data << EOD
X    a    b    c    d    e
A 0.00 0.01 0.04 0.09 0.16
B 0.01 0.01 0.03 0.07 0.13
C 0.04 0.03 0.04 0.07 0.12
D 0.09 0.07 0.07 0.09 0.13
E 0.16 0.13 0.12 0.13 0.16
EOD

set xtics 1  # デフォルトで 1 刻みなので無くて良い
set ytics 1

set x2tics
set y2tics
set link x2
set link y2
set mx2tics 2
set my2tics 2

set xtics scale 0
set ytics scale 0
set x2tics scale 0 format ''
set y2tics scale 0 format ''

set grid front mx2tics my2tics lw 1.5 lt -1 lc rgb 'white'

plot $data matrix rowheaders columnheaders w image pixels not
Gnuplot Produced by GNUPLOT 6.0 patchlevel 0 gnuplot_plot_1 A B C D E a b c d e 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16