LoginSignup
1
0

More than 1 year has passed since last update.

gnuplotでHorizontal histogramを描く

Last updated at Posted at 2022-03-09

はじめに

グラフを描画するのに非常に便利なgnuplotですが,ヒストグラムを90度回転させた横書きのヒストグラム(以下,horizontal histogram)は実装されていないそうです.しかし過去に賢人が処方箋を打ち出してくれており,horizontal histogramを疑似的に再現することが可能になっています.
調べた限り詳しい解説がなかったので,ここではなるべく一般化して誰でも利用できるように解説します.

通常のヒストグラム

始めに通常のヒストグラムを作成してみます.以下のプロットに使うdata.datは,1列目に$x$軸,2列目に$y$軸の値がそれぞれ格納されているとしています.

unset key
set xlabel "x"
set ylabel "y"
set xrange [0.0:2.0]
set yrange [0.0:0.1]

BIN = 0.05 # bin widthの指定(任意)
set style fill solid border linecolor rgb "black"
plot "data.dat" using 1:2 bins binwidth = BIN with boxes linecolor rgb "grey" 

これを実行すると以下のヒストグラムが作られます.
before.png

Horizontal histogram

以下の参考文献によると,gnuplotでは横書きのヒストグラムは(少なくともver 5.4.0までは)実装されていないそうです.(参考:Horizontal histogram in gnuplot【gnuplot】散布図+ヒストグラム 統合図作成のコマンドセット)
そこで苦肉の策としてエラーバーをbox表記で示すboxxyerrorbarsオプションを用いることで代替する案が挙げられています.

gnuplotのofficial documentation(Official gnuplot online documentation, Version 5.4(日本語), P83)を見ると,プロット点に$x,y$両方のエラーバーを付ける際にはxyerrorbarsを用いて次のように記述します.

plot "---.dat" using x:y:delta_x:delta_y with xyerrorbars

このときのプロット結果は次の左図のように反映されます.
xyerrorbars.png

ここでxyerrorbarsの拡張版であるboxxyerrorbarsでは,右図のようにエラー範囲をBox状にプロットすることができます.そこでこの箱型のエラーバーを互いに重ならないよう連ねることでヒストグラムっぽくしたのが今回編み出された手法です.
上のboxxyerrorbarsをヒストグラムに応用することを考えると,horizontal histogramを作る場合,$x,y,\Delta x,\Delta y$はそれぞれ次のように設定すれば良いことが分かります. 

$x$ $y$ $\Delta x$ $\Delta y$
binの高さの半分の値 横軸($x$軸)の値 binの高さの半分の値$(=x)$ bin幅の半分の値

これを実装したのが以下のコマンドです.グラフ自体の回転はしていないので,labelとrangeが先ほどと逆転していることに注意してください.

unset key
set xlabel "y" # <-- x,y軸逆転
set ylabel "x"
set xrange [0.0:0.1] # <-- x,y軸逆転
set yrange [0.0:2.0]

BIN = 0.05 # bin widthの指定(任意)
set style fill solid border linecolor rgb "black"
plot "data.dat" using ($2*0.5):1:($2*0.5):(BIN*0.5) linecolor rgb "grey" with boxxyerrorbars

すると以下の図が出力されます.グラフのアスペクト比は(rltb)margin screenで適宜調節しています.
after.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0