Gnuplot 简单使用
Gnuplot 简单使用
2008 年 12 月
第 1 页
Gnuplot 简单使用
一、最简单的例子
gnuplot>plot sin(x)
其中 gnuplot> 为命令提示符,不是命令的一部分,结果如下:
二、加入设置
1、设置 x、y 坐标的范围
改变坐标范围
set xrange [xmin:xmax] set yrange [ymin:ymax]
让 gnuplot 决定坐标轴范围 gnuplot>set autoscale
也可以只在本次绘图中设置,使用 plot [xmin:xmax] [ymin:ymax] f(x),例如:
gnuplot>plot [0:5] [-2:2] sin(x) ,结果如下
第 2 页
可只设置 x 或 y 的范围,如只设置 x 范围
gnuplot>plot [0:5] sin(x)
结果:
Gnuplot 简单使用
只设置 y 的范围
gnuplot>plot [] [0:1] sin(x)
结果:
只设置一边,如设置 y 为小于 0
gnuplot>plot [] [:0] sin(x)
结果:
第 3 页
Gnuplot 简单使用
2、设置坐标轴名称
gnuplot>set xlabel ‘xlabel’ ; set ylabel ‘ylabel’ ; plot sin(x)
可以在一行写多个命令,命令间用分号隔开;结果如下:
取消坐标轴名称设置
gnuplot>unset xlabel
gnuplot>unset ylabel
3、设置对数坐标
设置为对数坐标 set logscale
第 4 页
Gnuplot 简单使用
设置 x、y 轴为对数坐标
gnuplot>set logscale x
gnuplot>set logscale y
取消对数坐标 unset logscale
4、设置绘图标题
gnuplot>set title ‘title’
gnuplot>plot sin(x)
取消绘图标题设置 unset title
5、同时绘制多个函数
gnuplot>plot f1(x),f2(x),......fn(x),例如:
gnuplot>plot sin(x),x ,结果:
第 5 页
Gnuplot 简单使用
6、为曲线设置标题
gnuplot>plot f(x) title 'title'
例如:
gnuplot>plot sin(x) title ‘y=sin(x)’,x title ‘y=x’
结果:
7、设置曲线标题的放置位置
gnuplot>set key x,y
其中 x,y 为横、纵坐标,例如:
gnuplot>set key 8,-0.8
gnuplot> plot [-10:10] [-1:1] sin(x) title 'sin'
将文本‘sin’放置在(8,-0.8)坐标处,结果:
第 6 页
Gnuplot 简单使用
将曲线标题放置到默认位置,使用命令:
gnuplot>set key default
设置不显示曲线标题,使用名令 unset key
可设置绘图标题框
gnuplot>set key box
效果如下
取消设置绘图标题框,使用命令 set key nobox
8、添加网格
gnuplot>set grid;plot cos(x)
取消网格,使用命令 unset grid
第 7 页
Gnuplot 简单使用
三、设置输出
1、输出成图片
其中<类型>为图片的类型,如 png、jpeg、gif
设置输出文件名
gnuplot>set terminal <类型>
gnuplot>set output ‘<文件名>’
例如:
gnuplot>set terminal gif
gnuplot>set output ‘sinx.gif’
gnuplot>plot sin(x)
即会生成名为 sinx.gif 的图片
设置图片的大小
set size m,n
m,n 为放大(缩小)倍数(相对于当前大小),如:
set size 1.2, 0.5 将长设置为当前的 1.2 倍,宽设为当前的 0.5 倍
2、设置回窗口输出(即默认状态)
命令
gnuplot>set terminal windows
可简写为 se t w
3、其他
也可设置输出为其他类型文件,如(ps、tex 等);也可直接输出到打印机
四、三维绘图
命令 splot 用于三维绘图,如:
gnuplot>splot x*y
第 8 页