Bus errorとSegmentation faultに困ったら見るブログ

物理の研究者による日々の研究生活のメモ書きです ( python/emacs/html/Japascript/シェルスクリプト/TeX/Mac/C言語/Linux/git/tmux/R/ポケモンGO)

【gnuplot】グラフの外側に書いたkeyの行数・列数をいじりたいときは

今回やりたいことは、グラフのkey(legend)を1行に書く個数をこちらで指定して、見栄えを整えたいということ
文章にしてもわかりずらいので比較のグラフを見てもらった方が早いだろう・・・・



参考にしたのはこちら
3.24.30 key

重要なところを抜粋

Elements within the key are stacked according to ‘vertical‘ or ‘horizontal‘. In the case of ‘vertical‘, the key occupies as few columns as possible. That is, elements are aligned in a column until running out of vertical space at which point a new column is started. The vertical space may be limited using ’maxrows’. In the case of ‘horizontal‘, the key occupies as few rows as possible. The horizontal space may be limited using ’maxcols’.
要するに
 keyのオプションであるverticalを付けたとき、何個のkeyで改行するかはmaxrowsで指定する
 keyのオプションであるhorizontalを付けたとき、何個のkeyで改行するかはmaxcolsで指定する
ということらしい

これを使ってグラフを書くと・・・・・


デフォルトでは
枠内にkeyが表示される


set key below horizontal maxcols 1では
縦にkeyを並べられる、maxcolsで折り返しの回数を指定するっぽい


set key below horizontal maxcols 2では
縦にkeyを並べられる、maxcols 2なので2個表示したら改行される


set key below horizontal maxcols 3では
縦にkeyを並べられる、maxcols 3なので3個表示したら改行される

set key below vertical maxrows 1では
横にkeyを並べられる、maxcols 1なのでずらっと横にならぶ


set key below vertical maxrows 4では

これらのグラフを書くためのソースコードは、

set term postscript enhanced color
set xtics pi
set format x "%.0P{/Symbol p}"

set output "pic_key_default.eps"
set xrange[0:2*pi]
plot sin(x) w l lw 3 title "sin(x)",\
sin(2*x) w l lw 3 title "sin(2x)",\
sin(3*x) w l lw 3 title "sin(3x)",\
sin(4*x) w l lw 3 title "sin(4x)",\
cos(x) w l lw 3 title "cos(x)",\
cos(2*x) w l lw 3 title "cos(2x)",\
cos(3*x) w l lw 3 title "cos(3x)",\
cos(4*x) w l lw 3 title "cos(4x)"


set output "pic_key_vertical_maxrows1.eps"
set key below vertical maxrows 1
set xrange[0:2*pi]
plot sin(x) w l lw 3 title "sin(x)",\
sin(2*x) w l lw 3 title "sin(2x)",\
sin(3*x) w l lw 3 title "sin(3x)",\
sin(4*x) w l lw 3 title "sin(4x)",\
cos(x) w l lw 3 title "cos(x)",\
cos(2*x) w l lw 3 title "cos(2x)",\
cos(3*x) w l lw 3 title "cos(3x)",\
cos(4*x) w l lw 3 title "cos(4x)"

set output "pic_key_vertical_maxrows4.eps"
set key below vertical maxrows 4
set xrange[0:2*pi]
plot sin(x) w l lw 3 title "sin(x)",\
sin(2*x) w l lw 3 title "sin(2x)",\
sin(3*x) w l lw 3 title "sin(3x)",\
sin(4*x) w l lw 3 title "sin(4x)",\
cos(x) w l lw 3 title "cos(x)",\
cos(2*x) w l lw 3 title "cos(2x)",\
cos(3*x) w l lw 3 title "cos(3x)",\
cos(4*x) w l lw 3 title "cos(4x)"


set output "pic_key_horizontal_maxcols1.eps"
set key below horizontal maxcols 1
set xrange[0:2*pi]
plot sin(x) w l lw 3 title "sin(x)",\
sin(2*x) w l lw 3 title "sin(2x)",\
sin(3*x) w l lw 3 title "sin(3x)",\
sin(4*x) w l lw 3 title "sin(4x)",\
cos(x) w l lw 3 title "cos(x)",\
cos(2*x) w l lw 3 title "cos(2x)",\
cos(3*x) w l lw 3 title "cos(3x)",\
cos(4*x) w l lw 3 title "cos(4x)"


set output "pic_key_horizontal_maxcols2.eps"
set key below horizontal maxcols 2
set xrange[0:2*pi]
plot sin(x) w l lw 3 title "sin(x)",\
sin(2*x) w l lw 3 title "sin(2x)",\
sin(3*x) w l lw 3 title "sin(3x)",\
sin(4*x) w l lw 3 title "sin(4x)",\
cos(x) w l lw 3 title "cos(x)",\
cos(2*x) w l lw 3 title "cos(2x)",\
cos(3*x) w l lw 3 title "cos(3x)",\
cos(4*x) w l lw 3 title "cos(4x)"

set output "pic_key_horizontal_maxcols3.eps"
set key below horizontal maxcols 3
set xrange[0:2*pi]
plot sin(x) w l lw 3 title "sin(x)",\
sin(2*x) w l lw 3 title "sin(2x)",\
sin(3*x) w l lw 3 title "sin(3x)",\
sin(4*x) w l lw 3 title "sin(4x)",\
cos(x) w l lw 3 title "cos(x)",\
cos(2*x) w l lw 3 title "cos(2x)",\
cos(3*x) w l lw 3 title "cos(3x)",\
cos(4*x) w l lw 3 title "cos(4x)"

いつものpngではなくpostscriptを使っているのは、x軸の区切り文字をギリシャ文字のpiで綺麗に書きたいと思っていたのだが、pngにしたままだとエラーが出るから・・・
以前からめんどくさがって問題解決しようとしていないから放置している・・・
一応、情報があったので共有

ランキング参加中です

↓クリックしていただけると嬉しいです〜