gnuplotは簡単に2D、3Dのグラフが作成できるフリーのソフトウェーである。単純なグラ フから、学術論文用の高品質なグラフまで作成可能で、世界中で使われている。本当の読 み方は「ニュープロット」ではあるが、「グニュープロット」と呼ばれることも多い。こ れは、Free Software Foundation (FSF)が進めているGNUプロジェクト2とは関係が無い。
UNIXに限らず、WindowsやMachintoshでも動作する。さらに、EXCELとくらべものにならな いくらい美しいグラフを書くことができる。しかも、フリーである。卒業研究のグラフ作 成に使うのが良いだろう。
マニュアル類は、webにたくさんある。情報が必要になれば、以下のサイトを調べるのが 良いだろう。
http://t16web.lanl.gov/Kawano/gnuplot/
http://lagendra.s.kanazawa-u.ac.jp/ogurisu/manuals/gnuplot-intro/
$ gnuplotすると、gnuplotが立ち上がり、コマンド入力画面になる。まずは、三角関数のグラフを 書いてみよう。以下のコマンドを入力する。
gnuplot> plot sin(x)
gnuplot> helpとする。webページの方が圧倒的に分かり易いが、ネットに接続されていない環境の場合、 このヘルプや役立つ。gnuplotを終了するときには、
gnuplot> exitとする。
plot x**3+x+1 plot x**0.5 plot log(x) gnuplot> plot log10(x) gnuplot> plot exp(x)
3次元グラフも簡単にかける。3次元グラフの場合、右マウスでドラッグすると視点を変え ることができるのでおもしろい。
splot x**2+y**2 splot x*sin(x+y)
3次元グラフで隠線処理が必要であれば、set hidden3dとする。また、表示するデー タ点は、set isosampleで設定する。たとえば、以下のよ うにすれば、隠線処理し、x方向とy方向とも40点のデータを出力する。
gnuplot> set hidden3d gnuplot> set isosample 40,40 gnuplot> splot exp(0.5*(-x*x-y*y))*cos(x*x+y*y)
gnuplot> plot "trifunc.txt" using 1:2とする。各データ点を線で結びたければ、
gnuplot> plot "trifunc.txt" using 1:2 with lineとする。複数のデータを一度に描くためには、
gnuplot> plot "trifunc.txt" using 1:2 with line, "trifunc.txt" using 1:3 with line, "trifunc.txt" using 1:4 with lineとする。ただし、改行しないで(Enterキーを押さない)記述する必要がある。プロットす るレンジを変えたい場合は、set xrange[ymin:ymax]をつかう。
gnuplot> set yrange[-1.5:1.5] gnuplot> plot "trifunc.txt" using 1:2 with line, "trifunc.txt" using 1:3 with line, "trifunc.txt" using 1:4 with line
UNIXでは、パイプを使うことにより、かなり複雑な動作も簡単に記述できる。そのパイプ を開くためには、ファイルポインターが必要である。そのための変数を用意する。パイプ の先もファイルとして扱われるのである。
FILE *hoge;
次にgnuplotを立ち上げて、そこにパイプを接続する必要がある。パイプの情報のファイ ルポインターで示される。
hoge = popen("gnuplot -persist","w");popen()関数がパイプを開く命令である。
パイプを通して、gnuplotにコマンドを送るのはfprintf()関数を使う。
fprintf(hoge, "plot sin(x)");
終了時には、開いたパイプは閉じるのが礼儀である。
pclose(hoge);
1 #include <stdio.h>
2 #include <math.h>
3 void mk_triangle_data(char *a, double x1, double x2, int n);
4 void mk_graph(char *f, char *xlb, double x1, double x2,
5 char *ylb, double y1, double y2);
6
7 /*==========================================================*/
8 /* main function */
9 /*==========================================================*/
10 int main(void){
11
12 double pi = 4*atan(1);
13
14 mk_triangle_data("out.txt", -2*pi, 2*pi, 1000);
15 mk_graph("out.txt", "x", -2*pi, 2*pi, "y", -3, 3);
16
17 return 0;
18 }
19
20 /*==========================================================*/
21 /* make a data file */
22 /*==========================================================*/
23 void mk_triangle_data(char *a, double x1, double x2, int n){
24 double x, dx;
25 double y1, y2, y3;
26 int i;
27 FILE *out;
28
29 dx = (x2-x1)/n;
30
31 out = fopen(a, "w");
32
33 for(i=0; i<=n; i++){
34 x = x1+dx*i;
35 y1 = sin(x);
36 y2 = cos(x);
37 y3 = tan(x);
38
39 fprintf(out, "%e\t%e\t%e\t%e\n", x, y1, y2, y3);
40 }
41
42 fclose(out);
43 }
44
45 /*==========================================================*/
46 /* make a graph */
47 /*==========================================================*/
48 void mk_graph(char *f, char *xlb, double x1, double x2,
49 char *ylb, double y1, double y2)
50 {
51
52 FILE *gp;
53
54 gp = popen("gnuplot -persist","w");
55
56 fprintf(gp, "reset\n");
57 fprintf(gp, "set terminal postscript eps color\n");
58 fprintf(gp, "set output \"graph.eps\"\n");
59 fprintf(gp, "set grid\n");
60
61 /* ------- set x axis ---------*/
62
63 fprintf(gp, "set xtics 1\n");
64 fprintf(gp, "set mxtics 10\n");
65 fprintf(gp, "set xlabel \"%s\"\n", xlb);
66 fprintf(gp, "set nologscale x\n");
67 fprintf(gp, "set xrange[%e:%e]\n", x1, x2);
68
69 /* ------- set y axis ---------*/
70
71 fprintf(gp, "set ytics 1\n");
72 fprintf(gp, "set mytics 10\n");
73 fprintf(gp, "set ylabel \"%s\"\n", ylb);
74 fprintf(gp, "set nologscale y\n");
75 fprintf(gp, "set yrange[%e:%e]\n", y1, y2);
76
77 /* ------- plat graphs ---------*/
78
79 fprintf(gp, "plot \"%s\" using 1:2 with line,\
80 \"%s\" using 1:3 with line,\
81 \"%s\" using 1:4 with line\n", f, f, f);
82
83 fprintf(gp, "set terminal x11\n");
84 fprintf(gp, "replot\n");
85
86 pclose(gp);
87 }