gnuplot的入门学习
2D绘图:
最简单的二维作图命令是: plot function
1.例如:简单的画出sin(x),cos(x).
plot sin(x),cos(x)
可以指定函数作图范围:
plot [x1:x2] [y1:y2] function
plot [x1:x2] function (指定 x 范围)
plot [] [y1:y2] function (仅仅指定 y 范围)
注意, 范围设定将会影响以后的作图. 默认作图范围是 x = [-10,10], 键入: set xrange [-10:10]
可以恢复 x 的默认范围值. 键入:set autoscale y
可以恢复 y 的默认范围值. 键入:plot [-5:5] cosh(x)
2.例如:假设只想单看一个正弦函数的周期,我们可以采用“闭”区间的方法得到
set xrange [-pi:pi]
replot cos(x) with points pointtype 2
或 plot [-pi:pi] sin(x), cos(x) with points pointtype 2
3.例如:我们想画出数据文件中的数据
plot sin(x),‘1.dat’
4.例如:命名图和坐标轴
set title 'My first graph'
set xlabel 'shijian'
set ylabel 'lucheng'
plot sin(x)
5.例如:改变轴上的tic,并设置网格,让你的数据可视化
set title "My first graph"
set xrange [-pi:pi] # we want only one cycle
set xtics ('0' 0, '90' pi/2, '-90' -pi/2, '45' pi/4,'-45' -pi/4,'135' 3*pi/4,'-135' -3*pi/4)
set grid
set xlabel 'Angle, in degrees'
set ylabel 'sin(angle)'
plot sin(x)
3D绘图
三维作图命令是 splot. 最简单的语法: splot function
例如, 作出函数 z = -x3-y, 键入: splot -x**3 -y
类似地, 可设置作图范围:
· splot [x1:x2] [y1:y2] [z1:z2] function
· splot [x1:x2] function (To just set the x range)
· splot [] [y1:y2] function (To just set the y range).
· splot [] [] [z1:z2] function (To just set the y range).
三维图形的表达不是很容易的:z = 2(x2+ y2)exp(-x2- y2)
命令代码:splot [-2:2] [-2:2] 2*(x2 + y2)*exp(-x2 - y2)
执行:set grid
之后, 再执行:gnuplot> splot [-2:2] [-2:2] 2*(x2 + y2)*exp(-x2 - y2) 或者
gnuplot replot 得到(注意 grid 网格已经显示出来)
使用 set hidden3d 命令可以对三维图形进行消隐处理:
set hidden3d
replot
增加三维作图的精度
对于二维图形, 作图精度(点数多少)可以使用 set samples 命令, 对于三维作图, 可用 set isosamples 命令.
其格式是:set isosamples x_rate, y_rate 。默认的设置是 x_rate=y_rate= 10.
作图命令:set isosamples 30, 30
splot [-2:2] [-2:2] 2*(x**2 + y**2)*exp(-x**2 - y**2)
得到图一
再输入命令:
set isosamples 100, 100
replot
得到图二
通过对比两张图图可以发现,作图精度越高, 作图所消耗的资源就越多. 作图速度就越慢. 建议作图精度不要高于 100 。
为三维图 添加等高线
采用如下命令来添加等高线:
· set contour base – 沿着图的底部画出等高线.
· set contour surface – 沿着三维表面作出等高线
· set contour both – 沿着表面和底部作出等高线
· set nocontour – 关闭等高线
注意, 三维作图消隐 set hidden3d 生效后, set contour surface 将无效。
例如· set hidden3d
· set contour base
· splot [-2:2] [-2:2] 2*(x**2 + y**2)*exp(-x**2 - y**2)
得到图一
· unset hidden3d
· set contour surface
· replot
得到图二
· set nohidden3d
· set contour both
· replot
得到 图三
图一