gnuplot> sinc(x) = sin(pi*x)/(pi*x) gnuplot> set xlabel "X" gnuplot> set ylabel "Y" gnuplot> unset key gnuplot> set samples 500 gnuplot> set xrange [-5:5] gnuplot> set xtics 1 gnuplot> set x2range [0:10] gnuplot> set x2tics 1 gnuplot> set y2range [-2:5] gnuplot> set y2tics 1 gnuplot> set grid gnuplot> set label 1 "Hello first" at 2,0.5 gnuplot> set label 2 "Hello second" at second 2,0.5 gnuplot> set label 3 "Hello graph" at graph 0.2,0.5 gnuplot> set label 4 "Hello screen" at screen 0.2,0.5 gnuplot> set label 5 "Hello character" at character 10,5 gnuplot> plot sinc(x)
这里我们画一个 sinc 函数图像。为了说明问题,我们把第二坐标系也都标示了出来,虽然函数图像并没有用到第二坐标。其他命令前面都讲过了,这里只看五个 set label 命令。set label 之后紧跟的那个整数,就是一个标识符,用以区别各个 label,可以随便选个整数。在字符串之后,at 参数指定标签坐标。默认为 first 坐标系统,也可以使用其它坐标系统。下面是生成的图片:
gnuplot> set label 1 "Hello red left" at 2,0.4 left textcolor rgb "#FF0000" gnuplot> set label 2 "Hello green center" at 2,0.5 center textcolor rgb "#00FF00" gnuplot> set label 3 "Hello blue right" at 2,0.6 right textcolor rgb "#0000FF" gnuplot> set label 4 "Hello rotate" at -2,0.4 rotate by 45 gnuplot> set label 5 "Hello point" at -3,0.2 point pt 7 lc rgb "#FF9900" gnuplot> replot
我们现在所有绘图的坐标刻度均标在图像边框上,无论上下左右。这样做的好处是函数或数据图线清楚,不会和坐标标注混在一起。其实,我们小时候数学课上最早学习坐标系的时候,都是让 X 轴和 Y 轴正交于原点,而刻度标注在坐标轴上。这样的图像在定性表现函数关系,尤其有一定对称性的函数关系时,比较一目了然。
让我们来看看怎样用 gnuplot 得到这样的效果。
用 unset border 命令把边框去掉;
用 set zeroaxis 命令画出正交于原点的坐标轴;
在设定坐标刻度时加上 axis 参数,这样刻度会出现在坐标轴上面,而不是边框上。
为了避免审美疲劳,这次我们拿高斯函数举个例子:
gnuplot> set term wxt enhanced font "Times New Roman,16" gnuplot> gauss(x) = exp(-pi*x*x) gnuplot> set title "函数 e^{-πx^2}" gnuplot> set samples 500 gnuplot> set xrange [-3:3] gnuplot> set yrange [-0.2:1.2] gnuplot> unset key gnuplot> unset border gnuplot> set zeroaxis lt -1 lw 2 gnuplot> set xtics axis -2,1,2 gnuplot> set ytics axis 0,1,1 gnuplot> plot gauss(x) lw 3
gnuplot> set title "函数 e^{-πx^2}" offset 12,-5 gnuplot> set xtics axis -2,1,2 offset 0.4,0 gnuplot> set ytics axis 0,1,1 offset 0,0.4 gnuplot> set arrow 1 from 2,0 to 3.2,0 filled size 0.2,15,60 lw 2 gnuplot> set arrow 2 from 0,1 to 0,1.22 filled size 0.2,15,60 lw 2 gnuplot> set rmargin 4 gnuplot> set label 1 "X" at 3.0,-0.1 gnuplot> set label 2 "Y" at -0.3,1.2 gnuplot> replot