资源描述
用gnuplot画图
目录
1. 画平面图像 1
1.1 直角坐标平面内画图 1
1.1.1 plot命令格式中各参数的说明 1
1.1.2 用plot命令在同一坐标内画多条图像 4
1.2 用plot命令画参数方程确定的函数图像 5
1.3 用plot命令在极坐标画函数的图像 6
1.4 各种参数和选项的设置 7
1.4.1 弧度制和角度制的转换 7
1.4.2 自变量和因变量取值范围的设置 7
1.4.3 图例的位置 8
1.4.4 线的类型、粗细度、颜色等各种参数的设置 8
1.4.5 对坐标轴的设置 10
1.4.6 添加直线和箭头 11
1.4.7 图的大小和位置 13
2.画空间图象 16
2.1 直角坐标空间内画图 16
2.2 各种参数和选项的设置 17
2.2.1设置网络线的数目 17
2.2.2自动调整坐标轴的刻度 18
2.2.3沿z轴拉伸或缩小图象 18
2.2.4对隐藏线的消除 19
2.2.5改变三维图的视角 20
2.2.6控制图象的弯曲部分 21
2.2.7给图象添加颜色 22
2.3画漂亮的彩色图 23
总结 25
参考资料 26
致谢 27
1. 画平面图像
1.1 直角坐标平面内画图
一般在gnuplot 中用plot命令来画直角坐标平面内的图形,它的命令格式如下:
plot {<ranges>}
{<function> | {"<datafile>" {datafile-modifiers}}}
{axes <axes>} {<title-spec>} {with <style>}
{{definitions} <function> ...}
1.1.1 plot命令格式中各参数的说明
(1)选项 ranges 是自变量的取值范围
(2)选项function指一个数学表达式或一对含参变量的数学表达式,表达式有可能完全地被定义,或者在一些gnuplot命令前定义。可以在plot中定义函数与参变量,只用逗号使它与其他项隔开。
例1:想要画函数y=sinx, x∈[0,2p]的图像
plot [0:2*pi] sin(x) %自变量按弧度计算
运行结果如图(1)所示
图 1 函数y=sinx, x∈[0,2p]的图像
例2:想要画函数y=sin(Ax+j), x∈[0,p]的图像
plot [0:pi] f(x)=sin(x*a+b),a=2,b=pi/4,f(x)或者
f(x)=sin(x*a+b)
plot [0:pi],a=2,b=pi/4,f(x)
运行结果如图(2)所示
图 2 函数y=sin(2x+p/4), x∈[0,p]的图像
(3)选项 {"<datafile>" {datafile-modifiers}}根据已有的数据文件来作图时选用。
例3:有文件“D:\gnuplot\test.txt”它的内容为
-20.000000 -6.083352
-19.000000 -6.072853
…………………………..
18.000000 6.061191
19.000000 6.072853
用此数据的一列为横坐标一列为纵坐标画图时要用如下命令即可。
plot 'D:\gnuplot\test.txt'
运行结果如图(3)所示
图3 用已有数据画图
(4)选项title表示图例
例4:画标准正态分布密度函数 的图像且插入图例
plot [-3:3] 1/sqrt(2*pi)*exp(-x*x) title 'Density Function of The Standard Normal Distribution'
运行结果如图(4)所示
图4 插入图例
(5) 选项with表示函数图像线的类型、粗细度、颜色等各种参数
格式:
with <style> { {linestyle | ls <line_style>}| {{linetype | lt <line_type>} {linewidth | lw <line_width>}{linecolor | lc <colorspec>}
{pointtype | pt <point_type>}{pointsize | ps <point_size>}
{fill | fs <fillstyle>} {nohidden3d | nocontours}{palette}}}
例5:画函数的图像
plot [-2*pi:2*pi] sin(x)/x with points linewidth 3 linecolor 3
或者
plot [-2*pi:2*pi] sin(x)/x w p lw 3 lc 3
运行结果如图(5)所示
图5 函数图像线的设置(1)
例6:画函数的图像
plot [-pi:4*pi] sin(x)*exp(x) with lines linewith 4 linecolor 2
或者
plot [-pi:4*pi] sin(x)*exp(x) w l lw 4 lc 2
运行结果如图(6)所示
图6 函数图像线的设置(2)
1.1.2 用plot命令在同一坐标内画多条图像
同一坐标内画多条曲线时,各曲线间均用逗号隔开就可以了。
例7 :同一坐标内画 y=sinx,y=sinAx,y=sin(Ax+w)的图像
plot [0:2*pi] sin(x) lw 3 lc 2, sin(2*x) lw 3 lc 3, sin(2*x+pi/6) lw 3
运行结果如图(7)所示
图7 同时画几条曲线图(A)
图8 同时画几条曲线图(B)
例8:同一坐标内画函数y=x,y=x+a, y=a, y=3a和a=2的图像
set xrange [-1:7]
set yrange [1:7]
a=2
plot a lw 2,3*a lw 2,x lw 2, x+a lw 2
运行结果如图(8)所示
1.2 用plot命令画参数方程确定的函数图像
默认情况下gnuplot用plot命令来画函数y=f(x)在直角坐标平面内的图形,用命令 set parametric 来可以plot 的画图模式转换到画参数方程确定的函数图像
命令格式: et parametric # 转换画图模式
nset parametric # 恢复画图模式
show parametric # 显示画图模式
例1: 用星形线的参数方程 画它的图像
set parametric
a=1
plot a*(cos(t))**3,a*(sin(t))**3 lw 2 lc 11
运行结果如图(9)所示
图 9 星形线图
例2:同一坐标内画圆和椭圆的图像
set parametric
set trange [0:2*pi]
plot 3*cos(t),4*sin(t) title "Circle" lw 2 lc 4 ,3*cos(t),3*sin(t) title"Ellipse" lw 2 lc 5
运行结果如图(10)所示
图 10 用参数方程同时画圆和椭圆图像
1.3 用plot命令在极坐标画函数的图像
在gnuplot中用命令 set polar 来可以plot 的画图坐标转换到极坐标中画函数图像
命令格式: set polar # 转换画图坐标模式
unset polar # 恢复画图坐标模式
show polar # 显示画图坐标模式
例1:画心脏线r=5(1+cosq) 的图形
set polar
plot 5*(1+cos(t)) lw 3
运行结果如图(11)所示
图 11 心脏线图
例2:同一坐标内画三叶玫瑰线 r=acos3q与四叶玫瑰线 r=acos2q的图像
set polar
a=2
plot a*cos(3*t) lw 3 lc 4 ,a*cos(2*t) lw 3 lc 1
运行结果如图(12)所示
图 12 用极坐标同时画三叶玫瑰线和四叶玫瑰线图像
1.4 各种参数和选项的设置
1.4.1 弧度制和角度制的转换
一般在gnuplot自变量按弧度来计算,如果弧度计算和角度计算转换用以下命令。
set angles degree
set angles radian
例1:想要画函数y=sinx, x∈[0,3600]的图像
set angles degree
plot [0:360] sin(x) %自变量按角度计算
运行结果如图(13)所示
图 13 函数y=sinx, x∈[0,360]的图像
1.4.2 自变量和因变量取值范围的设置
格式:set Var_range [{{<min>}:{<max>}}]
例2:画函数 y=tanx, x∈(-p/2,p/2),y∈[-10,10]
set xrange[-pi/2:pi/2]
set yrange [-10:10]
plot tan(x) lw 2 lc 2
运行结果如图(14)所示
图14 正切函数图
1.4.3 图例的位置
设置图例位置的命令格式为:
set key {on|off} {default}
{{inside | outside} | {lmargin | rmargin | tmargin | bmargin}
| {at <position>}}{left | right | center} {top | bottom | center}
{vertical | horizontal} {Left | Right}
{{no}reverse} {{no}invert}
{samplen <sample_length>} {spacing <vertical_spacing>}
{width <width_increment>}{height <height_increment>}
{{no}autotitle {columnheader}}{title "<text>"} {{no}enhanced}
{{no}box { {linestyle | ls <line_style>}| {linetype | lt <line_type>}
{linewidth | lw <line_width>}}}
例如:
set key left %放在左边
set key outside %放在外边,但只能在右面的外边
set key 0.5,0.6 %将图例放在(0.5,0.6)的位置处
例3: 画y=x2的图像且图例插入在(1,1)处
set key 1,1
plot x*x lw 2
运行结果如图(15)所示
取消图例设置命令格式: unset key
1.4.4 线的类型、粗细度、颜色等各种参数的设置
set style line <index> default
set style line <index> {{linetype | lt} <line_type> | <colorspec>}
{{linecolor | lc} <colorspec>}
{{linewidth | lw} <line_width>}
{{pointtype | pt} <point_type>}
{{pointsize | ps} <point_size>}
{palette}
其中linetype(lt)和linecolor(lc)确定线的颜色,linewidth(lw)确定线的粗细度,pointtype(pt)确定点的颜色,pointsize(ps)确定点的大小。
图 15 图例演示图。
例4:画函数的图象
set xrange[0:0.5]
set style line
plot (x-sqrt(x))/(2*x-1) w lp lt 1 lw 2 pt 2 ps 2
运行结果如图(16)所示
图 16 线的类型演示图
unset style line %取消线类型的设置命令
show style line %显示当前线的类型
1.4.5 对坐标轴的设置
(1)对坐标轴加文字
例5:画函数的图象且加文字
set xlabel ‘x’ %x轴标为‘x’
set ylabel ‘DOS’ tc lt 3 %其中的tc lt 3表示’DOS’的颜色用第三种颜色
plot f(x)=sin(x)/exp(x),f(x) w l lt 5 lw 4
运行结果如图(17)所示
图17 给坐标轴插入文字演示图
(2)如果要改变坐标轴的刻度且每个刻度上再加小刻度,其命令格式为:
set tics {axis | border} {{no}mirror}
{in | out} {scale {default | <major> {,<minor>}}}
{{no}rotate {by <ang>}} {offset <offset> | nooffset}
{ format "formatstring" } { font "name{,<size>}" }
{ textcolor <colorspec> }
set tics {front | back}
例6:画函数的图象
set xtics 1.0 %x轴的主刻度的宽度为1.0
set ytics 1.0 %y轴的主刻度的宽度为1.0
set mxtics 3 %x轴上每个主刻度中画3个分刻度
set mytics 3 %y轴上每个主刻度中画3个分刻度
plot (1-x**2)/(1+x**2) w l lt 4 lw 2
运行结果如图(18)所示
同样可以为上边的x轴(称为x2)和右边y(称为y2)轴进行设置,即x2tics,
mx2tics,y2tics,my2tics。
unset tics %取消对坐标轴的设置
show tics %显示当前坐标轴的设置
图18 坐标轴刻度改过后的演示图
(3)改变边界属性命令格式为:
set border {<integer>} {front | back} {linewidth | lw <line_width>}
{{linestyle | ls <line_style>} | {linetype | lt <line_type>}}
例7:画函数的图象
set border 3 lt 3 lw 2 %设为第三种边界,颜色类型为3,线宽为2
set xrange[0.1:1]
plot 1/x-sqrt(1-x**2)
unset border %取消边界设置
show border %显示当前边界设置
运行结果如图(19)所示
图19 边界类型演示图
1.4.6 添加直线和箭头
在图中添加箭头和直线,其命令格式为:
set arrow {<tag>} {from <position>} {to|rto <position>}
{ {arrowstyle | as <arrow_style>}
| { {nohead | head | backhead | heads}
{size <length>,<angle>{,<backangle>}}
{filled | empty | nofilled}
{front | back}
{ {linestyle | ls <line_style>}
| {linetype | lt <line_type>}
{linewidth | lw <line_width} } } }
例8:画函数的图象且从(4.4,-0.222)处到(6.2,-0.2)处添加箭头
set arrow from 4.4,-0.222 to 6.2,-0.2 lt 3 lw 2 %这个箭头颜色类型为3,线宽类型为2
set xrange[0.1*pi/180:2*pi]
plot sin(x)/x w l lt 1 lw 2
运行结果如图(20)所示
图20 添加箭头演示图
利用nohead可以去掉箭头的头部,这就是添加直线的方法。
例9:给函数的图象添加直线
set arrow from 4.4,-0.222 to 6.2,-0.2 nohead lt 3 lw 2
set xrange[0.1*pi/180:2*pi]
plot sin(x)/x title‘f(x)’w l lt 1 lw 2
运行结果如图(21)所示
在gnuplot中,对于插入多个的label和arrow等等,系统会默认按先后顺序
分别对各个label或arrow进行编号,从1开始。即arrow,arrow 2…arrow n
如果要去掉某个label或arrow,那么只要用unset命令将相应的去掉即可。
例如:unset arrow 2 %将去掉第二个箭头
图21 添加直线演示图
1.4.7 图的大小和位置
(1)设置图形大小的命令格式为:
set size {{no}square | ratio <r> | noratio} {<xscale>,<yscale>}
例11:画函数的图象
set size square 0.5,0.5 %使图形是方的,且长宽均为方图的一半。
set xrange[0.1*pi/180:179*pi/180]
plot cos(x)/exp(x)
运行结果如图(22)所示
(2)设置图形位置的命令格式为:set origin <x-origin>,<y-origin>
例12:画函数的图象且设置图在图形面板中的位置
set origin 0.0,0.5
set xrange[0:2*pi]
plot f(x)=cos(x)/exp(x),f(x) w l lt 3 lw 2
运行结果如图(23)所示
图22 图形大小的演示图
图23 图形位置设置的演示图
(3)在一个图形面板中同时分别画多个图
设置多图模式命令格式为:
set multiplot { layout <rows>,<cols>
{rowsfirst|columnsfirst} {downwards|upwards}
{title <page title>}
{scale <xscale>{,<yscale>}} {offset <xoff>{,<yoff>}}
}
例13:在一个图形面板中同时分别画函数y=atan(x),y=ex,y=x2,y=x3的图象
set multiplot
set origin 0.0,0.0 %设置第一个图的原点的位置
set size 0.5,0.5 %设置第一个图的大小
plot atan(x)
set origin 0.0,0.5 %设置第二个图的原点的位置
set size 0.5,0.5 %设置第二个图的大小
plot exp(x) w l lt 2 lw 2
set origin 0.5,0.0 %设置第三个图的原点的位置
set size 0.5,0.5 %设置第三个图的大小
plot x**2 w l lt 3 lw 2
set origin 0.5,0.5 %设置第四个图的原点的位置
set size 0.5,0.5 %设置第四个图的大小
plot x**3 w l lt 1 lw 2
运行结果如图(24)所示
(4)如果要设置一张图的空白地方要多大就用set margin 命令来实现,其命令格式为:
set bmargin {{at screen} <margin>}
set lmargin {{at screen} <margin>}
set rmargin {{at screen} <margin>}
set tmargin {{at screen} <margin>}
show margin
例14:画复合函数h(t)的图象
set bmargin 1 %底空白地方的宽度
set lmargin 2 %左空白地方的宽度
set rmargin 3 %有空白地方的宽度
set tmargin 4 %上空白地方的宽度
f(t)=exp(-s*n*t)/sqrt(1.0-s*s)
g(t)=sin(n*sqrt(1.0-s**2)*t - atan(-sqrt(1.0-s**2)/s))
h(t)=1-f(t)*g(t)
n=1.0
set xrange [0:13]
set samples 50 %改变函数图形的形状
set dummy t %改变变量的名称
set key box
plot s=.1,h(t),s=.3,h(t),s=.5,h(t),s=.7,h(t),s=.9,h(t),s=1.0,h(t),s=1.5,h(t),s=2.0,h(t) lw 2
运行结果如图(25)所示
图24 在同一张图里同时画多个图的演示图
图25 一张图的空白地方的演示图
2.画空间图象
2.1 直角坐标空间内画图
在Gnuplot中另一个画图命令是splot,用于画三维图形。它的命令格式如下:
splot {<ranges>}
<function> | "<datafile>" {datafile-modifiers}}
{<title-spec>} {with <style>}
{, {definitions,} <function> ...}
例1:画函数f(x,y)=x2+y2的图象
set xrange[-4:4]
set yrange[-4:4]
splot f(x,y)=x**2+y**2,f(x,y) w l lt 1 lw 2
运行结果如图(1)所示
图1 抛物面
图2 相交的抛物面和平面
例2:相交的抛物面和平面
set border 0
unset xtics
unset ytics
unset ztics
set isosamples 50,20
splot x**2+y**2-50 lt 1,x+y lt 2
运行结果如图(2)所示
2.2 各种参数和选项的设置
2.2.1设置网络线的数目
在空间直角坐标系中可以控制图象的网络线数目,其命令格式为:
set isosamples <iso_1> {,<iso_2>}
show isosamples
例1:画函数的图象
set parametric
set urange[0:2*pi]
set vrange[0:pi/2]
set isosamples 50,20
splot cos(u)*cos(v),sin(u)*cos(v),cos(v)
运行结果如图(3)所示
图3 网络线演示图
2.2.2自动调整坐标轴的刻度
可以对每一个坐标轴分别设置,也可以对所有的坐标轴设置,其命令格式为:
set autoscale {<axes>{|min|max|fixmin|fixmax|fix} | fix | keepfix}
unset autoscale {<axes>}
show autoscale
例2:画函数的图象
set autoscale z
set angles degrees
set parametric
set samples 32
set isosamples 30,20
set urange [-180:180]
set vrange [-90:90]
splot cos(u),sin(u),v with lines lt 1
运行结果如图(4)所示
图4 自动调整坐标轴刻度演示图
2.2.3沿z轴拉伸或缩小图象
在直角空间坐标内可以设置一个图形的沿Z轴的大小,其命令格式为:
set ticslevel <frac>
set xyplane <frac>
set xyplane at <zvalue>
show xyplane
它们三个的效果是一样的,随便用一个就可以。
例3:画函数的图象
set ticsleve 0
set parametric
set isosamples 50,50
set urange [0:2*pi]
set vrange [0:2*pi]
splot cos(u)*u*(1+cos(v)/2),sin(v)*u/2,sin(u)*u*(1+cos(v)/2)
运行结果如图(5)所示
图5 沿Z轴调整好的图象
2.2.4对隐藏线的消除
画空间图时会出现影响图象效果的不易被看出的线,用set hidden3d命令可以消除这些线,其命令格式为:
set hidden3d {defaults} |
{ {{offset <offset>} | {nooffset}}
{trianglepattern <bitpattern>}
{{undefined <level>} | {noundefined}}
{{no}altdiagonal}
{{no}bentover} }
unset hidden3d
show hidden3d
例4:画函数的图象
set dummy u,v
set angles degrees
set parametric
set view 60, 136, 1.22, 1.26
set samples 64,64
set isosamples 13,13
set mapping spherical %设图的形状为球形
unset xtics
unset ytics
unset ztics
set border 0
set title "球形"
set urange [-90.0000:90.0000]
set vrange [0.00000:360.000]
set hidden3d
splot cos(u)*cos(v),cos(u)*sin(v),sin(u) notitle with lines lt 2 lw 2
运行结果如图(6)所示
图6 消除隐线后的球面
2.2.5改变三维图的视角
在空间直角坐标系中可以从不同的视角观察图象,其命令格式为:
set view <rot_x>{,{<rot_z>}{,{<scale>}{,<scale_z>}}}
set view map
set view {no}equal_axes
show view
例5:对函数设置视角
set parametric
set isosamples 20,20
set view 60, 30, 1, 1
set title "Parametric Torus"
set urange [0:2*pi]
set vrange [0:2*pi]
set zrange [-1:1]
splot (1-0.2*cos(v))*cos(u),(1-0.2*cos(v))*sin(u),0.2*sin(v) w l lt 1 lw 2
运行结果如图(7)所示
图7 三维图视角的演示图
2.2.6控制图象的弯曲部分
可以同时控制以三个坐标所确定的图象的弯曲部分,其命令格式为:
set tics {axis | border} {{no}mirror}
{in | out} {scale {default | <major> {,<minor>}}}
{{no}rotate {by <ang>}} {offset <offset> | nooffset}
{ format "formatstring" } { font "name{,<size>}" }
{ textcolor <colorspec> }
set tics {front | back}
unset tics
show tics
例6:画函数的图象
set border 4095
set palette color positive
set samples 50
set isosamples 50
set tics norotate nooffset
set xrange [-15:15]
set yrange [-15:15]
set zrange [-0.25:1]
splot sin(sqrt(x**2+y**2))/sqrt(x**2+y**2) notitle
图8 设置图象弯曲部分演示图
2.2.7给图象添加颜色
可以将图和曲线,以调色板里的颜色显示出来,其命令格式为:
set pm3d {
{ at <bst combination> }
{ interpolate <steps in scan>,<steps between scans> }
{ scansautomatic | scansforward | scansbackward | depthorder }
{ flush { begin | center | end } }
{ ftriangles | noftriangles }
{ clip1in | clip4in }
{ corners2color { mean|geomean|median|min|max|c1|c2|c3|c4 } }
{ hidden3d <linestyle> | nohidden3d }
{ implicit | explicit }
{ map }
}
show pm3d
unset pm3d
例7:给函数f(x,y)=x2-y2图象添加颜色
set pm3d
set hidden3d
set key below
unset xtics
unset ytics
unset ztics
set border 0
set isosamples 50,50
splot x**2-y**2
图9 给图象添加颜色演示图
2.3画漂亮的彩色图
例1:画螺旋线对应的函数的图象
set parametric
set hidden nooffset
set key below
unset xtics
unset ytics
unset ztics
set border 0
set isosamples 100,20
set urange [0:10*pi]
set vrange [0:2*pi]
set autoscale z
set pm3d depthorder
splot (1-0.1*cos(v))*cos(u),(1-0.1*cos(v))*sin(u), 0.1*(sin(v)+u/1.7-10) notitle w pm3d
图 10 螺旋线
例2:在同一个坐标系内画函数和
的图象
set parametric
set hidden
set key below
unset xtics
unset ytics
unset ztics
set border 0
set urange [-pi:pi]
set vrange [-pi:pi]
set isosamples 50,20
set pm3d depthorder
splot cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v) notitle w pm3d,
1+cos(u)+.5*cos(u)*cos(v),.5*sin(v),sin(u)+.5*sin(u)*cos(v) notitle w pm3d
图11 两个环的交叉图
总结
本文主要介绍了利用Gnuplot软件画图象的各命令格式及其参数的说明,用plo
展开阅读全文