Gnuplot简单实用方法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Gnuplot简单使用
2008年12月
一、最简单的例子
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) ,结果如下
可只设置x或y的范围,如只设置x范围gnuplot>plot [0:5] sin(x)
结果:
只设置y的范围
gnuplot>plot [] [0:1] sin(x)
结果:
只设置一边,如设置y为小于0 gnuplot>plot [] [:0] sin(x)
结果:
2、设置坐标轴名称
gnuplot>set xlabel ‘xlabel’ ; set ylabel ‘ylabel’ ; plot sin(x)可以在一行写多个命令,命令间用分号隔开;结果如下:
取消坐标轴名称设置
gnuplot>unset xlabel
gnuplot>unset ylabel
3、设置对数坐标
设置为对数坐标 set logscale
设置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 ,结果:
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)坐标处,结果:
将曲线标题放置到默认位置,使用命令:gnuplot>set key default
设置不显示曲线标题,使用名令unset key
可设置绘图标题框
gnuplot>set key box
效果如下
取消设置绘图标题框,使用命令set key nobox 8、添加网格
gnuplot>set grid;plot cos(x)
取消网格,使用命令 unset grid
三、设置输出
1、输出成图片
gnuplot>set terminal <类型>其中<类型>为图片的类型,如png、jpeg、gif 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
五、运算符
1、一元的运算符
- -a取反
! !a逻辑反(a为0,则结果为1,若a为非0,结果为0)! a!阶乘
$ $3调用在‘using’中的列
2、二元的运算符
** a**b乘方,a的b次方
* a*b乘,a乘b
/ a/b除,a除b
% a%b取模,结果为a除b的余数
+ a+b加
- a-b减
== a==b判断等于,若a与b相等,则返回1,否则返回0
!= a!=b判断不等,若a与b相等,则返回0,否则返回1
< a<b判断小于
<= a<=b小于或等于
> a>b判断大于
>= a>=b大于或等于
& a&b按位与
^ a^b按位异或
| a|b按位或
&& a&&b逻辑与
|| a||b逻辑或
3、三元的运算符
?: a?b:c若a为真(1),则结果为b,否则结果为c
举例:
f(x)=x>=0?-x:x
定义一个分段函数发f(x),当x>=0时,f(x)=-x;当x<0时,f(x)=x;
以此绘制一张图像
gnuplot> f(x)=x>=0?-x:x;plot f(x)
结果:
六、变量和函数
1、变量
在gnuplot中,变量一旦被赋值,它即存在了,如:
gnuplot>a=1
gnuplot>b=’hello’
命令show variables将显示已存在的变量,此命令可简写为 sh v 注意:变量名区分大小写
2、函数
1、内置函数
Gnuplot内置有一些有用的函数
数学库函数
函数 参数类型 返回
abs(x) any x的绝对值|x|;
abs(x) complex x的长度, sqrt( real(x)^2 + imag(x)^2 ) acos(x) any cos(x) 的反函数
acosh(x) any
cosh(x)的反函数
arg(x) complex the phase of x in radians
asin(x) any sin(x)的反函数
asinh(x) any sinh(x)的反函数
atan(x) any tan(x)的反函数
atan2(y,x) int or real
tan−1 (y/x) (inverse tangent)
atanh(x) any
tanh(x)的反函数
besj0(x) radians J0 Bessel function of x
besj1(x) radians J1 Bessel function of x
besy0(x) radians Y0 Bessel function of x
besy1(x) radians Y1 Bessel function of x
ceil(x) any 不小于x的最小整数
cos(x) radians 余弦
cosh(x) radians 双曲余弦
erf(x) any Erf(real(x)), error function of real(x)
erfc(x) any Erfc(real(x)), 1.0 - error function of real(x)
exp(x) any e的x次方
floor(x) any 不大于x的最大整数
gamma(x) any Gamma(real(x)), gamma function of real(x)
ibeta(p,q,x) any Ibeta(real(p,q,x)), ibeta function of real(p,q,x)
inverf(x) any inverse error function of real(x)
igamma(a,x) any Igamma(real(a,x)), igamma function of real(a,x)
imag(x) complex x的虚部,返回一个实数
invnorm(x) any inverse normal distribution function of real(x)
int(x) real 实数的整数部分,小数后面的部分被去除(非四舍五入)lambertw(x) real Lambert W function
lgamma(x) any Lgamma(real(x)), lgamma function of real(x)
log(x) any ln(x),x的自然对数 ( 基为e)
log10(x) any log(x),x的以十为基的对数
norm(x) any normal distribution (Gaussian) function of real(x)
rand(x) any Rand(real(x)), pseudo random number generator
real(x) any x的实部
sgn(x) any 符号函数,x>0返回1,x<0返回-1,x=0返回0
若x为复数,则x的虚部(imag(x))将被忽略
sin(x) radians x的正弦
sinh(x) radians x的双曲正弦
sqrt(x) any x的开方
tan(x) radians x的正切
tanh(x) radians x的双曲正切
字符串函数
gprintf(”format”,x) any string result from applying gnuplot’s format parser
sprintf(”format”,x,...) multiple string result from C-language sprintf
strlen(”string”) string int length of string
strstrt(”string”,”key”) strings int index of first character of substring ”key”substr(”string”,beg,end) multiple string ”string”[beg:end]
system(”command”) string string containing output stream of shell command word(”string”,n) string, int returns the nth word in ”string”
words(”string”) string returns the number of words in ”string”
其他函数
column(x) int column x during datafile manipulation.
defined(X) variable name [DEPRECATED] returns 1 if X is defined, 0 otherwise. exists(”X”) ”variable name” returns 1 if a variable named X is defined, 0 otherwise. stringcolumn(x) int content of column x as a string.
timecolumn(x) int timecolumn x during datafile manipulation.
tm_hour(x) int the hour
tm_mday(x) int the day of the month
tm_min(x) int the minute
tm_mon(x) int the month
tm_sec(x) int the second
tm_wday(x) int the day of the week
tm_yday(x) int the day of the year
tm_year(x) int the year
valid(x) int test validity of column(x) during datafile manip.
2、自定义函数
在gnuplot中可直接用f(x)=<expression> 来定义函数,<expression>为表达式,如:gnuplot>f(x)=x*x;plot f(x)
也可以定义二维函数,如:
gnuplot>p(x,y)=sin(x)*cos(y);splot p(x,y)
命令show functions可显示已定义的函数,如:
gnuplot> show functions
User-Defined Functions:
f(x)=x*x
p(x,y)=sin(x)*cos(y)
此命令也可简写为show fun 或sh fun或 sh fu 或其他
七、文件绘图
假设有文件 file.dat,内容如下:
10 0.781 0.857 0.963
20 0.891 1.053 1.023
30 0.950 1.037 1.025
40 0.984 0.999 1.014
50 1.013 0.998 1.018
60 1.000 1.000 1.000
使用命令
gnuplot>plot 'file.dat'
将自动使用第一、二列数据作为点的x、y坐标绘图,结果如下:
使用如下命令此时将各点连接起来
gnuplot>plot 'file.dat' with lines,结果:
使用如下命令将各点用符号标志
gnuplot>plot 'file.dat' with linespoints
使用如下命令将使用1、3列来绘图
gnuplot>plot 'file.dat' using 1:3 with linespoints,结果:
使用如下命令将使用第一列与第三列的二分之一绘图gnuplot>plot 'file.dat' using 1:($3/2) with linespoints
也可以对某一列使用函数,如:
使用命令
gnuplot>plot 'file.dat' using 1:(sin($2)) with linespoints 将使用第一列与第二列的正弦绘图,结果:
八、常用命令
对命令参数的说明
1、包括在花括号{} 中的参数是可选的;
2、以竖线|分开的命令为多选;
1、目录相关命令
1、pwd
present working directory当前工作目录
用途:确定你当前所在的目录,这有什么用?如果你工作的目录为数据文件所在目录,那么你在命令行上就不用输入数据文件的完整路径了。
举例:如果你要用文件file.data绘图,此文件在c:\data下,而你当前的工作目录为c:\gnuplot\bin(通过pwd命令知道)
如果你使用命令:
gnuplot> plot 'file.data'
就会提示找不到文件,你必须给出完整路径,如下:
gnuplot> plot 'c:\data\file.data'
2、cd ‘< directory >’(come directory)到某目录
用途: 将工作目录切换到某个目录,<directory>是想要到的目录名。
如上面的例子,如果你用命令:
gnuplot>cd ‘c:\data’
这时你使用下面命令绘图就不会出错了,因为你当前的工作目录为 c:\data,gnuplot可以在当前目录找到数据文件
gnuplot> plot 'file.data'
2、退出命令
exit和quit 用于退出gnuplot,也可简写为q,如下:
gnuplot> exit
其他退出命令:文件结束符(END-OF-FILE);在unix系统中为ctrl+D,在windows系统中为ctrl+Z;
3、print命令
语法:print <var>
其中<var>为变量名,例如:
gnuplot>a=1
gnuplot> print a
1
输出a的值,也可输出字符串,例如:
gnuplot> print ‘hello’
hello
输出字符串hello;可以用双引号代替单引号,有何区别?在上面的例子中输出是完全一样的,但下面的例子将显示其区别
gnuplot> print ‘hello\neveryone’
hello\neveryone
gnuplot> print “hello\neveryone”
hello
everyone
即单引号屏蔽转义符号 ’\’,而双引号则不会,它将 \n 看成换行,另外还有 \t 表示制表符。
4、条件判断命令if
语法:if (<condition>) <command-line> [; else if (<condition>) ...; else …]
<condition>为条件,<command-line>为如果条件满足要执行的语句,可以为多条语句,用分号隔开,但必须写在一行语句中(可以用‘\’断行);例如:
if(a>0) print a;a=a-1;
if后还可接else 、else if命令;也必须和if写在同一行
5、暂停命令pause
命令语法:
pause <time> {"<string>"}
pause mouse {<endcondition>}……{ <endcondition>} {"<string>"}
对于第一种用法,<time>是时间,单位为秒,<string>为暂停是打印的字符串。
对于第二种用法,如果当前终端支持鼠标,pause mouse执行后,点击鼠标和按下ctrl+C都会使暂停结束,<endcondition>可以是keypress, button1, button2, button3,any;keypress为按键动作,button1为鼠标的左键,button2为中键,button3为右键,any为任意动作,即按键动作或鼠标点击。
说明:对于pause <time> 若time为负值,则暂停直到点击Enter键(return键),若time为0,则不暂停;
举例:
pause 1 'pause 1 second'#暂停1秒,并打印 pause 1 second
pause 0 'no paue'#此命令功能与print 'no paue' 功能相同
pause -1'Hit return to continue '
pause mouse button1 #点击鼠标左键程序继续执行
pause mouse any "Any key or button will terminate"
#按任意键或点击鼠标程序将继续
6、replot命令
不带参数的replot命令将会重复上一次的绘图命令(plot和splot)。
此外,replot命令也可以绘另一个图形,举例:
gnuplot> plot sin(x)
gnuplot> replot x+1
7、程序调用命令
1、load命令
命令格式:load "<input-file>"
此命令读取输入文件的每一行并执行;
2、call命令
命令格式:call "<input-file>" <parameter-0> <parm-1> ... <parm-9>
call命令与load命令有相同的功能,但是call命令允许传入参数(最多10个);
例如有一个名为calltest.gp的文件,内容为:
print "argc=$# p0=$0 p1=$1 p2=$2 p3=$3 p4=$4 p5=$5 p6=$6 p7=x$7x"
在命令行调用该文件:
gnuplot>call ’calltest.gp’ "abcd" 1.2 + "’quoted’" -- "$2"
将会显示:
argc=7 p0=abcd p1=1.2 p2=+ p3=’quoted’ p4=- p5=- p6=$2 p7=xx
说明:$#的值为参数的个数 ,$n表示第(n+1)个参数,n为0-9的数字
3、reread命令
reread命令使当前gnuplot脚本文件被重新调用(即再从第一条命令开始执行),这将会忽略本次执行reread语句后的命令;利用这可以用if来做循环,见下面的例子;
假设有一个文件test.gp,内容为:
if($# != 1) print "usage:call 'test.gp' number";\
else a=int($0);load 'rereadtest.gp'
另一个文件rereadtest.gp,内容为
if(a>0) print a;a=a-1;reread;\
else if(a<0) print a; a=a+1;reread;
用如下命令执行:
gnuplot> call 'test.gp' 5
结果:
5
4
3
2
1
8、其他命令
clear清空输出的图像
9、帮助命令
help{<topic>},<topic>为帮助主题
如help set,也可以更具体help set output
其他帮助命令:?;问号命令与help命令效果是一样的;
对于windows系统也可以直接点击菜单栏的 Help按钮,然后根据索引寻找相关帮助。
九、脚本功能
如果你要连续运行多个命令,一条一条地输入显得很慢,而且如果你要修改中间某一条命令,你必须全部重新输入一遍,所以可以将一连串的命令存入一个文件中,然后用命令
call ‘filename’,其中filename即为包含一连串命令的文件,例如:
要画一张图,要求如下:
(1)包含函数x的三次方和x的平方
(2)存入gra.jpg的图像文件
(3)设置图像标题为test
(4)两条曲线的标题为x^3和x^2
命令如下
gnuplot>set ter jpeg
gnuplot>set out ‘gra.jpg’
gnuplot>set title ‘test’
gnuplot>plot x**3 title ‘x^3’,x*x title ‘x^2’
这样可以完成任务,但更好的方法是将上述命令存成文件,假设存为test.plt,内容为
#绘制x的三次方和x的平方的函数图
set ter jpeg #设置输出为jpeg图片
set out 'gra.jpg'
set title 'test'
plot x**3 title 'x^3',\
x*x title 'x^2'
使用命令load或cal调用脚本,如下
gnuplot>call ‘test.plt’
将会产生同样的效果,
注释:在文件中以“#”开头的语句为注释
长行可以使用 \ 断行写
十、命令行编辑
1、移动光标位置
说明:C-x ,表示同时按下ctrl键和x键(x为键值)
C-B光标后移一个位置;
C-F光标前移移一个位置;
C-A光标移动到本行开头;
C-E光标移动到本行末尾;
也可以用方向键移动光标,向左键为光标向左移动一个位置, 向右键为光标向右移动一个位置;HOME键为移动到本行开头,END键为移动到本行结尾
2、编辑命令
C-H删除光标之前前的字符;
C-D删除光标位置的字符;
C-K删除从当前位置一直到行尾的字符;
C-U删除整行;
C-W删除此单词从当前字符到单词开头
例如:
命令开始这样:
按下ctrl+W后就如下:
2、命令的简写
命令plot 'file.dat' with lines
可简写为plot 'file.dat' with l
进一步简写为plot 'file.dat' w l
同理:
命令plot 'file.dat' with linespoints
简写为plot 'file.dat' w lp
set terminal简写set term,进一步可简写为set ter、se t set output 简写 set out,进一步可简写为se o
show 简写为 sh
show variables简写为 sh v
其实许多命令都可以简写为“没有歧义”的形式。