LoginSignup
3
3

More than 5 years have passed since last update.

gnuplotで最大値、最小値を求める

Posted at
plot.dat
0 1
2 3
5 2
2 5

このようなファイルがあったときに、それぞれの列の最大or最小値が知りたい。
直接的には求められませんが、調べたら次のような方法があることがわかりました。

x0 = 1e38
x1 = -1e38
y0 = 1e38
y1 = -1e38

grabx(x) = (x<x0) ? x0=x : (x>x1) ? x1=x : 0
graby(y) = (y<y0) ? y0=y : (y>y1) ? y1=y : 0

plot "plot.dat" using (grabx($1)):(graby($2))

解説します。

まず、plot "plot.dat"でplot.datの中身をプロットしますが、そのときにusing修飾子を指定できます。例えば

plot3.dat
0 1 2
2 3 2
4 6 7
1 2 2

というデータファイルから1列目をx座標、3列目をy座標としてグラフを描画したいときはplot "plot3.dat" using 1:3のように指定します。この1は単に1列目を示していますが、これを括弧で囲むとその括弧内でcolumn(n)という関数が使用できます。column(n)はn列目の値を表します。$nはcolumn(n)の略記です。

なので、plot "plot.dat" using 1:2plot "plot.dat" using ($1):($2)は同じ意味になります。

gnuplotでは変数や関数が定義できます。grabxとgrabyはそれぞれxとyの最大最小値を更新する関数です。よって、plot "plot.dat" using (grabx($1)):(graby($2))で1列目と2列目の値を全てgrabxとgrabyに適用するので、x0、x1、y0、y1にそれぞれ求める値が入っています。

このとき描画されるグラフが不要ならば

set table "/dev/null"
plot "plot.dat" using (grabx($1)):(graby($2))
unset table

などとすれば値だけ手に入りますが、このやり方は少なくともlinux限定です。

3
3
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
3
3