LoginSignup
21
26

More than 5 years have passed since last update.

gnuplot でプロットに好きな色を使う

Posted at

目的

gnuplot では出力するターミナルにもよりますが,個別に設定しなければプロットの色は自動的に選択されます.デフォルトで選択される色はいわゆる原色ばかりです.シアン #00ffff,黄緑 #00ff00,黄色 #ffff00 などはディスプレイ上ではかなり視認性が悪くあまり使いたくありません.特にプレゼンテーションの資料などでこのような色を使うことは推奨されません.プロットの色は積極的に設定していきたいものです.1

set xrange [0:2*pi]
set sample 500
set key outside
plot for [n=1:7] sin(n*x/2.) t sprintf("sin(%dx/2)",n) lw 2

default.png

方法

gnuplot (>4.2) ではプロットする色をかなり柔軟に設定できます.例えば以下のように入力することで,それぞれ #ff0000 #ff80ff の色でプロットできます.

plot sin(x) w line linecolor rgb "#ff0000", \
     cos(x) w line lc rgb "orchid"

あらかじめ定義されている色の名前については show colorname と入力することで一覧表を得ることができます.手元にインストールされている gnuplot-4.6 patchlevel 6 では 112 の色が定義されていました.

色の設定をより簡単にするためには 16 進での入力より 10 進での入力をしたいところです.また,作成したプロット全体でカラーの統一感を出したい場合などは RGB よりも HSV で色を入力したくなります.gnuplot-4.2 以上であれば sprintf が使えると思われるので,以下のような関数を定義しておくと便利です.

color.gp
### convert (r,g,b) to #RRGGBB
rgb(r,g,b) = sprintf("#%02x%02x%02x",r%256,g%256,b%256);
### convert (h,s,v) to #RRGGBB
hsv_hi(h) = floor((h%361)/60.)%6
hsv_f(h) = (h/60.)-floor(h/60.)
hsv_p(s,v) = floor(v*(1.-(s/255.))+0.5)
hsv_q(h,s,v) = floor(v*(1.-(s/255.)*hsv_f(h))+0.5)
hsv_t(h,s,v) = floor(v*(1.-(s/255.)*(1.-hsv_f(h)))+0.5)
hsv(h,s,v) = hsv_hi(h)==0?rgb(v,hsv_t(h,s,v),hsv_p(s,v)):\
             hsv_hi(h)==1?rgb(hsv_q(h,s,v),v,hsv_p(s,v)):\
             hsv_hi(h)==2?rgb(hsv_p(s,v),v,hsv_t(h,s,v)):\
             hsv_hi(h)==3?rgb(hsv_p(s,v),hsv_q(h,s,v),v):\
             hsv_hi(h)==4?rgb(hsv_t(h,s,v),hsv_p(s,v),v):\
             rgb(v,hsv_p(s,v),hsv_q(h,s,v))
###

この関数を使うと以下のようにプロットの色を変更できます.

set xrange [0:2*pi]
set sample 500
set key outside
plot for [n=1:7] sin(n*x/2.) t sprintf("sin(%dx/2)",n) lw 2 \
     lc rgb hsv(180+15*n,200-20*n,255)

hsv.png

僕は上記の関数を color.gp という名前で ~/.gnuplot.d/ というフォルダに保存. gnuplot の起動時に読み込まれる ~/.gnuplot に以下の記述を追加して毎回読み込まれるようにしています.

.gnuplot
load '~/.gnuplot.d/color.gp'

毎回指定するのが面倒であれば .gnuplot に色指定を書き込んでしまうのも 1 つの方法です.

.gnuplot
set style line 1 lc rgb "royalblue"
set style line 2 lc rgb "goldenrod"
set style line 3 lc rgb "dark-spring-green"
set style line 4 lc rgb "purple"
set style line 5 lc rgb "steelblue"
set style increment user

参考資料

[1]: gnuplot-5.0 ではデフォルトのカラーが変更になったようです.

21
26
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
21
26