现在在网上可以找的科学作图软件有不少,其中不乏优秀者,Win平台上有大名鼎鼎的Origin、Tecplot、SigmaPlot等,类UNIX上有LabPlot、gnuplot等。其他的常见的计算软件如Matlab、Mathematica、Maple、Scilab、IDL、Maxima等也都对科学作图有很好的支持,甚至于只要稍加努力使用MS的Excel也可以做出不错的图来。但是笔者最喜爱的工具还是gnuplot。gnuplot 是典型的UNIX 哲学的产物,小巧,灵活,擅长于与其他工具协同工作。本人使用gnuplot 已经有1年有余,积累了些经验,下面的内容主要是我1年多来使用gnuplot所做笔记的一个总结,当然,在成稿过程中也将gnuplot 的手册又来回翻了几遍。

gnuplot是一个命令行驱动的科学绘图工具,可将数学函数或数值资料以平面图或立体图的形式画在不同种类终端机或绘图输出装置上。它是由Colin Kelley 和 Thomas Williams于1986年开发的绘图程序发展而来的,可以在多个平台下使用。gnuplot既支持命令行交互模式,也支持脚本。

gnuplot 的主要作者有:Thomas Williams, Colin Kelley, Russell Lang, Dave Kotz, John Campbell, Gershon Elber, Alexander Woo 和许多其他人。

gnuplot 与自由软件基金会和GNU 项目没有任何直接的关系,因此将 gnuplot 称为 GNUplot 是不对的。gnuplot 是完全独立的设计和开发的,命名为 gnuplot 只是种妥协,当时Thomas 希望他们开发的程序叫做 "llamaplot" 而Colin 更喜欢 "nplot" 这个名字。后来他们都同意命名他们的程序为 "newplot",可是很快他们发现有个Pascal 语言写的程序叫这个名字,而且这个程序偶尔会被提到。最后 Thomas 想出了"gnuplot" 这个双方都可以接受的名字。

gnuplot 在互联网上的主页地址是:http://www.gnuplot.info/ , 源代码和可执行程序也可以到http://sourceforge.net/projects/gnuplot/上下载。

gnuplot有详细的使用手册,如果用户使用的是GNU/Linux操作系统,可以察看 /usr/share/doc/gnuplot目录下的gnuplot.html文件。需要PDF版本的使用手册可以到http://sourceforge.net/projects/gnuplot 下载。本文中所引用范例皆整理自 gnuplot demo。文中的图例亦引用自子目录 demo 中的范例。

快速入门

本文全文都以GNU/Linux 下的 gnuplot 4.0版本为例,如果读者使用的是其他版本的操作系统,个别操作细节请做相应调整。

在命令行模式下输入 gnuplot,即可看到如下的信息,其中省略号处省略了部分信息:

[plain] view plaincopy
  1. G N U P L O T
  2. Version 4.0 patchlevel 0
  3. last modified Thu Apr 15 14:44:22 CEST 2004
  4. System: Linux 32 bit
  5. ……
  6. Terminal type set to 'x11'
  7. gnuplot>

‘gnuplot>’即是 gnuplot 的命令行提示符,gnuplot 的所有命令都在这个提示符下输入。

2D 绘图

请看例 1:

[plain] view plaincopy
  1. plot sin(x)

产生图 2结果 ——以曲线绘出三角函数 sin(x)。

图 1 Plotting sin(x)

假设我们只想看到一个正弦曲线周期。我们通过限制图的默认 x 范围来完成此操作。使用表示法 [min:max] 来指定范围。要仅指定最小值,使用 [min:];要仅指定最大值,使用 [:max]。数学上称此为所谓的“闭”区间表示法。

例 2从 -pi 到 +pi 的 sin(x) 和 cos(x)

[plain] view plaincopy
  1. set xrange [-pi:pi]
  2. replot cos(x) with points pointtype 2

或者:

[plain] view plaincopy
  1. plot [-pi:pi] sin(x), cos(x) with points pointtype 2

我们刚才使用了 replot 命令,它执行先前的 plot 命令。当您绘制曲线图且需要不断对该图进行修改以添加想要的特征时,此命令会非常有用。另外,replot 使您可以添加更多的图。尝试输入 replot cos(x)。依照语法,该命令等同于 plot sin(x), cos(x)。replot 就是获取先前的绘图字符串,添加必要的逗号,然后附加输入给它的其余部分。

例 5 将数据文件中的数据画出:

[plain] view plaincopy
  1. plot sin(x), ‘1.dat’

其中1.dat 为一数据文件,每一行描述一点坐标位置。 内容如下,其中 # 后面的内容为注释:

[plain] view plaincopy
  1. # $Id: 1.dat,v 1.1.1.1 1998/04/15 19:16:40 lhecking Exp $
  2. -20.000000 -3.041676
  3. -19.000000 -3.036427
  4. -18.000000 -3.030596
  5. -17.000000 -3.024081
  6. -16.000000 -3.016755
  7. -15.000000 -3.008456
  8. ……

图 2 Plotting sin(x), data file – 1.dat

例 6命名图和坐标轴:

[plain] view plaincopy
  1. set title 'My first graph'
  2. set xlabel 'Angle, in degrees'
  3. set ylabel 'sin(angle)'
  4. plot sin(x)

现在,我们注意到 x 轴实际没有标记为度数,看起来不是很好。要修改此问题,可以通过调整 x 轴上的 tic 标记。

例 7 改变轴上 tic 并设置网格:

[plain] view plaincopy
  1. set title "My first graph"
  2. set xrange [-pi:pi]  # we want only one cycle
  3. set xtics ('0' 0, '90' pi/2, '-90' -pi/2, '45' pi/4,'-45' -pi/4,'135' 3*pi/4,'-135' -3*pi/4)
  4. set grid
  5. set xlabel 'Angle, in degrees'
  6. set ylabel 'sin(angle)'
  7. plot sin(x)

gnuplot 还允许您指定绘图的样式,以便获得进一步的控制。

例 8 多条曲线

[plain] view plaincopy
  1. plot sin(x) with linespoints pointtype 5, cos(x) w boxes lt 4

with 子句使您可以详细而精确地指定线的样式。在本例中,我们说明两种有用的样式。第一种样式 linespoints 通常在对数据绘图时非常有用,它在涉及的每个示例或数据点处标记一个点,并使用线性插值法连接连续的点。这里我们另外指定点类型为 5,它选择终端允许的第五种点。第二种样式 boxes 更适合绘制直方图数据。注意我们如何在 cos(x) 曲线中将 with 缩写成 w。类似地,lt 是 linetype 的缩写,是另一个特定于终端的设置,它选择终端可以绘制的四种线。不必说,您可以使用 pt 代替冗长的 pointtype。如果想在多条线中使用相同的绘图样式(在一个 plot 命令中或在多个 plot 命令中),可以使用 set 命令设置绘图样式:set style function linespoints。要更改用于绘制与函数相对的数据集合的样式,使用 set style data linespoints。

当绘制两条或多条曲线时,我们需要关键字或图例来对它们进行区分。默认情况下,关键字在右上角;但是如果它妨碍了图,可以将关键字放到其他位置 。如果愿意,甚至可以放到图外。

例 9 定制图的关键字或图例

[plain] view plaincopy
  1. set key top left
  2. set key box
  3. plot [-pi:pi] sin(x) title 'sinusoid' with linespoints pointtype 5, cos(x) t 'cosine' w boxes lt 4

上面,我们在同一图中绘制了正弦和余弦曲线。gnuplot 使您还可以绘制多个图,这样它们可以并排显示在同一输出屏幕或文件中。在某些排版系统中,以一个文件的形式包含两个图形比分别包含两个图形要更容易。

例 10 Multiplot 示例:

[plain] view plaincopy
  1. set xrange [-pi:pi]
  2. # gnuplot recommends setting the size and origin before going to multiplot mode
  3. # This sets up bounding boxes and may be required on some terminals
  4. set size 1,1
  5. set origin 0,0
  6. # Done interactively, this takes gnuplot into multiplot mode
  7. set multiplot
  8. # plot the first graph so that it takes a quarter of the screen
  9. set size 0.5,0.5
  10. set origin 0,0.5
  11. plot sin(x)
  12. # plot the second graph so that it takes a quarter of the screen
  13. set size 0.5,0.5
  14. set origin 0,0
  15. plot 1/sin(x)
  16. # plot the third graph so that it takes a quarter of the screen
  17. set size 0.5,0.5
  18. set origin 0.5,0.5
  19. plot cos(x)
  20. # plot the fourth graph so that it takes a quarter of the screen
  21. set size 0.5,0.5
  22. set origin 0.5,0
  23. plot 1/cos(x)
  24. # On some terminals, nothing gets plotted until this command is issued
  25. unset multiplot
  26. # remove all customization
  27. reset

3D绘图

例 11:

[plain] view plaincopy
  1. splot [-pi:pi] [-pi:pi] sin(x)*cos(y)

产生图 2结果 --- 以纵横各 10 条线组成的网格画出 sin(x)*cos(y) 的图形。

图 3 Plotting sin(x)*cos(y)

例 12 画等高线图:

[plain] view plaincopy
  1. set dgrid3d 100,100 #设置三维图表面的网格的数目
  2. set contour    #设置画等高线
  3. set cntrparam  levels  incremental -0.9,0.1,0.9   #设置等高线的疏密和范围,数据从   -0.2到0.2中间每隔0.01画一条线
  4. unset surface  # 去掉上面的三维图形
  5. set view 0,0
  6. unset key
  7. splot [-pi:pi] [-pi:pi] sin(x)*cos(y)

例 13 画pm3d图

[plain] view plaincopy
  1. set pm3d
  2. set isosamples 50,50
  3. splot x**2+y**2

[plain] view plaincopy
  1. set view 0,0 #设置视角,(0,0)将投影到底面上去
  2. unset ztics #把z轴上的数字给去掉
  3. unset surface
  4. splot x**2+y**2
  5. reset

输出文件

在启动时,您可能注意到终端类型设置为 X11。gnuplot采用标准设计,可以在多个终端设备上绘图。这包括直接打印到多种打印机中,包括 Epson、HP 和 Imagen 打印机。它甚至可以在伪设备中绘图,如 postscript 和 png。这主要涉及生成输出文件,而不是可查看文件或打印输出。这是将您的图表包含在其他报表中的一种技巧。

例 14 设置输出和终端类型

[plain] view plaincopy
  1. set terminal png     # gnuplot recommends setting terminal before output
  2. set output ‘output.png’  # The output filename
  3. plot sin(x)

现在,文件 output.png 中有了您刚才绘制的图。

所有终端在能力上都不相同。其中的一些(如 LaTeX)可能不支持文本的旋转,所以如果您像我们前面那样设置 ylabel,在不同的终端中可能显示不一样。换句话说,您可以在 LaTeX 的特定终端中使用 LaTex 命令。例如,set ylabel $sin(\\\\theta)$(注意我们使用两个反斜杠产生一个 LaTex 所需的反斜杠 —— gnuplot 在将双引号中所括的字符串传送到终端驱动程序之前,先对其进行反斜杠处理)。现在,您可以使用 \\input{output.tex} 将输出文件包含在 LaTeX 文件中。要在 PostScript 终端中获得相同的结果,使用相应的 PostScript 命令:{/Symbol q}。在 enhanced PostScript 和 LaTeX 中,您可以使用表示法 x^{superscript} 和 x_{subscript} 得到上标文字。还要注意缩写的终端和输出命令:

例 15  eps 驱动程序的能力

[plain] view plaincopy
  1. set term post enh    # enhanced PostScript, essentially PostScript with bounding boxes
  2. set out 'gplt.eps'
  3. set xlabel '{/Symbol q_1}
  4. set ylabel 'sin^2({/Symbol q_1})'
  5. plot sin(x)**2

其他

顺便说一下,我们提供的所有例子都可以在交互模式或批处理模式下运行。要以交互模式运行,在提示符中输入每个命令。要以批处理模式运行,将命令输入或添加到文本文件中,然后在 gnuplot提示符中使用 load 'filename' 读入文件,或者在 shell 提示符下将其作为参数提供给 gnuplot:$ gnuplot filename。使用第二种方法,gnuplot会在执行输入文件中的所有命令后退出,所以当直接绘图到文件中时,采用批处理模式执行特别有效。如果在屏幕上绘图时使用第二种方法,输出窗口一出现(当 gnuplot退出时)就会消失,所以您需要使用显式的“pause -1”命令(请参阅 help pause)来保持窗口。

在 X 环境下,gnuplot 如同其他 X client 程序一样,可接受许多参数。如

[plain] view plaincopy
  1. gnuplot -font 8x13bold      # 设定字形为 8x13bold。
  2. gnuplot -geometry 900x700  # 设定窗口的长为 900 pixel 宽为 700 pixel。
  3. gnuplot -bg black            # 设定背景颜色为黑色。

gnuplot 绘制数学函数或数值资料的步骤大体如:

定义常数及函数:定义常数及函数,使程式模组化及易于了解。

设定绘图环境:gnuplot 绘图之前已预先设定许多绘图参数。

绘图:在定义数学函数或设定绘图环境之后,接着就是绘出数学函数或数值资料的图形。gnuplot 提供操作方便的绘图指令——plot (2D) 或 splot (3D)。

产生输出结果:在终端上绘出结果后,可经由一些步骤而产生不同输出装置所需的输出。

依照上述过程,可快速地画出图形。剩下的步骤就是细心的调整绘图环境参数或修改函数方程式、常数等,即可得到满意的结果。

常量、操作符和函数

数字

gnuplot 表示数字可分成整数、实数及复数三类:

整数:gnuplot 与 C 语言相同,采用 4 byte 储存整数。故能表示 -2147483647 至 +2147483647 之间的整数。

实数:能表示约 6 或 7 位的有效位数,指数部份为不大于 308 的数字。

复数:以 {<real>,<imag>} 表示复数。其中<real>为复数的实数部分,<imag>为虚数部分,此两部分均以实数型态表示。 如 3 + 2i 即以 {3,2} 表示。

gnuplot 储存数字的原则为,若能以整数方式储存则以整数储存数字,不然以实数方式储存,其次以复数方式储存。例如在 gnuplot 执行

[plain] view plaincopy
  1. print 1/3*3
  2. print 1./3*3

分别得到 0 和 1.0 的结果。这是因前者使用整数计算,而后者采用实数计算的结果。执行

[plain] view plaincopy
  1. print 1234.567
  2. print 12345 + 0.56789
  3. print 1.23e300 * 2e6
  4. print 1.23e300 * 2e8

分别得到 1234.57、12345.6、2.46e+304 和 undefined value 的结果。这些例子是受到实数的有效位数和所能表现最大数字的限制。这是我们要注意的。

操作符

gnuplot 的操作符与 C 语言基本相同。 所有的操作均可做用在整数、实数或复数上。

表格 1 Unary Operators

Symbol

Example

Explanation

-

-a

unary minus

~

~a

one's complement

!

!a

logical negation

!

a!

factorial

表格 2 Binary Operators

Symbol

Example

Explanation

**

a**b

exponentiation

*

a*b

multiplication

/

a/b

division

%

a%b

modulo

+

a+b

addition

-

a-b

subtraction

==

a==b

equality

!=

a!=b

inequality

&

a&b

bitwise AND

^

a^b

bitwise exclusive OR

|

a|b

bitwise inclusive OR

&&

a&&b

logical AND

||

a||b

logical OR

?:

a?b:c

ternary operation

函数

在 gnuplot 中函数的参数可以是整数,实数或是复数。表格 3是 gnuplot 所提供的函数。

表格 3 gnuplot functions

Function

Auguments

Returns

abs(x)

any

absolute value of x, |x|; same type

abs(x)

complex

length of x, sqrt( real(x)^2 + imag(x)^2 )

acos(x)

any

1/cos(x) (inverse cosine) in radians

Acosh(x)

any

cosh−1 x (inverse hyperbolic cosine) in radians

arg(x)

complex

the phase of x in radians

asin(x)

any

1/sin(x) (inverse sin) in radians

asinh(x)

any

sinh−1 x (inverse hyperbolic sin) in radians

atan(x)

any

1/tan(x) (inverse tangent) in radians

atan2(y,x)

int or real

tan−1(y/x) (inverse tangent)

atanh(x)

any

tanh−1 x (inverse hyperbolic tangent) in radians

besj0(x)

int or real

J0 Bessel function of x

besj1(x)

int or real

J1 Bessel function of x

besy0(x)

int or real

Y0 Bessel function of x

besy1(x)

int or real

Y1 Bessel function of x

ceil(x)

any

smallest integer not less than x (real part)

cos(x)

radians

cos x, cosine of x

cosh(x)

radians

cosh x, hyperbolic cosine of x

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

exponential function of x

floor(x)

any

largest integer not greater than x (real part)

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

imaginary part of x as a real number

invnorm(x)

any

inverse normal distribution function of real(x)

int(x)

real

integer part of x, truncated toward zero

lambertw(x)

real

Lambert W function

lgamma(x)

any

Lgamma(real(x)),  lgamma function of real(x)

log(x)

any

ln(x), natural logarithm (base e) of x

log10(x)

any

log(x),  logarithm (base 10) of x

norm(x)

any

normal distribution (Gaussian) function of real(x)

rand(x)

any

normal distribution (Gaussian) function of real(x)

real(x)

any

Rand(real(x)),  pseudo random number generator

sgn(x)

any

real part of x

sin(x)

any

1 if x>0, -1 if x<0, 0 if x=0. imag(x) ignored

sinh(x)

radians

sin(x), sine of x

sqrt(x)

radians

sinh(x), hyperbolic sine x

tan(x)

any

sqrt(x),  square root of x

tanh(x)

complex

tan(x),  tangent of x

column(x)

int

column x during datafile manipulation.

defined(X)

variable name

returns 1 if a variable X is defined, 0 otherwise.

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.

下面举一些例子:

[plain] view plaincopy
  1. plot [0.5:20] besj0(x), besj1(x), besy0(x), besy1(x)
  2. plot [0:5] erf(x), erfc(x), inverf(x)

用户自定义函数和常量

在 gnuplot 中,用户可自定函数。函数可有 1 至 5 个自变量。 其定义函数的语法如下:

[plain] view plaincopy
  1. <function-name> ( <dummy1> {,<dummy2> {, ...}}) = <expression>

而用户定义常数的语法如下:

[plain] view plaincopy
  1. <variable-name> = <constant-expression>

下面举一些例子:

[plain] view plaincopy
  1. # 常数 w 为 2。
  2. w = 2
  3. # 常数 q 为小于但最接近 tan(pi/2 - 0.1) 的整数。
  4. q = floor(tan(pi/2 - 0.1))
  5. # 函数 f(x) 为 sin(w*x),其中 w 为常数。
  6. f(x) = sin(w*x)
  7. # 函数 sinc(x) 为 sin(pi*x)/(pi*x)。
  8. sinc(x) = sin(pi*x)/(pi*x)
  9. # 函数 delta(t) 为脉冲函数。
  10. delta(t) = (t == 0)
  11. # 函数 ramp(t) 当其小于零为零,当其大于零为斜率等于 1 的直线。
  12. ramp(t) = (t > 0) ? t : 0
  13. # 函数 min(a,b) 取两者中较小的数。
  14. min(a,b) = (a < b) ? a : b
  15. comb(n,k) = n!/(k!*(n-k)!)
  16. len3d(x,y,z) = sqrt(x*x+y*y+z*z)
  17. plot f(x) = sin(x*a), a = 0.2, f(x), a = 0.4, f(x)

gnuplot 已定义的常数仅有 pi (pi = 3.14159)。

绘图环境参数

如第二章所述,只要键入 plot sin(x), '1.dat' 即可得到图1 的结果。gnuplot 自动调整 X 轴、 Y 轴的显示范围,使图形显示在适当的位置并选择不同的颜色、图形,用以区别不同的函数或数据,也就是 gnuplot 自动调整其所需的绘图环境。若我们需要一些特别的绘图参数,如在 3D 中加入等高线、设定消去隐藏线、改变 X 轴、Y 轴的座标点名称等,可由改变绘图环境参数而改变之。 本章说明这些绘图参数设定的方法与功能。

Axis

绘图参数在设定坐标轴方面的参数可分为变量名称、数字格式、网格、显示范围、坐标轴显示方式与显示与否等六方面的设定:

变量名称设定

一般以 x 为横轴上的变量。可用 dummy 设定为其它的名称, 所绘函数的变量名称亦随之改变。如 set dummy t 将自变量改为 t,图8、图17、图20 均改变自变量名称。

数字格式设定

设定数字的显示方式与格式。由 format 此项参数设定显示格式,其语法为 :

[plain] view plaincopy
  1. set format {<axes>} {"<format-string>"}
  2. show format     # 显示各轴数字显示的型式

其中 axis 为 x、y、z、xy 或预设为xy。format-string 为描述数字格式的字符串,可接受如 C 语言中 printf 对数字的 f、e、g 三种格式化描述,亦可加入文字 (必须少于100 字)。以下举一些例子:

[plain] view plaincopy
  1. set format xy "%.2e"
  2. set format x  "%3.0f cm"

显示方式由 tics、xtics等设定。

xtics 是对 X 坐标轴上的格点做设定。如起始点、结束点、间隔或在轴上特定点放特定的名称。其语法为:

[plain] view plaincopy
  1. set xtics { {<start>, <incr>{, <end>}} |
  2. {({"<label>"} <pos> {, {"<label>"} <pos>}...)} }
  3. set noxtics   # 不标示任何 X 轴上的标点。
  4. show xtics    # 显示 X 轴标点的状况。

下面是三个改变格点的例子。

[html] view plaincopy
  1. # 每隔 2 格一个标点
  2. set xtics -10,2,10
  3. plot sin(x)

[plain] view plaincopy
  1. # 以文字作为标点
  2. set xtics ("low" -10, "medium" 0, "high" 10)
  3. plot sin(x)

[plain] view plaincopy
  1. # 在特定位置放上标点
  2. set xtics (-10,-9,-7,-3,0,pi/2,2*pi)
  3. plot sin(x)

xdtics 将 X 座标轴上标点名称依 0,1,…改为 Sun,Mon,…  Sat 等。 大于 7 的数目除以7 取其馀数。

[plain] view plaincopy
  1. # 将标点名称改为 Sun, Mon, ... Sat 等
  2. set xdtics
  3. plot [0 : 10] sin(x)

ytics, ymtics, ydtics, ztics, zmtics, zdtics 与 xtics, xmtics, xdtics 相似,不同点是作用在不同的轴上。

ticslevel 是在画 3D 图形时,调整 Z 轴的相对高度。语法为:

[plain] view plaincopy
  1. set ticslevel {<level>}
  2. show tics

网格设定

在 XY 座标平面上依刻度画上方格子。

[plain] view plaincopy
  1. # 设定变数为 t
  2. set dummy t
  3. # 设定 X 轴 Y 轴标点的格式
  4. set format xy "%3.2f"
  5. # 产生网格
  6. set grid
  7. plot  sin(t)

座标显示方式

分为线性与对数两种。一般为前者,若要改为对数方式,其语法为:

[plain] view plaincopy
  1. set logscale <axes> <base>
  2. set nologscale <axes>
  3. show logscale

其中 axes 为 X 轴、Y 轴、Z 轴的任意组合。base 预设为 10。

显示范围设定

改变各轴的显示范围。autoscale 参数设定后 gnuplot 自动调整显示范围。其余的如 rrange, trange, xrange, yrange, zrange 则是由使用者设定该轴的范围,以 xrange 为例,其语法为:

[plain] view plaincopy
  1. set xrange [{<xmin> : <xmax>}]

其中参数 <xmin> 与 <xmax> 代表 X 轴的起点与终点, 可以是数字或数学式子。如图7 中 set [0:10] sin(x) 设定 X 轴显示范围为 0 与 10 之间。此时可用

[plain] view plaincopy
  1. set xrange [0:10]
  2. plot sin(x)

使用 autoscale 参数调整显示范围,其语法为:

[plain] view plaincopy
  1. set autoscale <axes>
  2. set noautoscal <axes>
  3. show autoscale

其中 <axes> 为 gnuplot 欲调整的轴,可以是 x, y, z 或 xy,预设为所有的轴。

座标轴显示与否设定

设定是否要画出座标轴,以 X 轴为例:

[plain] view plaincopy
  1. set xzeroaxis     # 设定显示 X 座标轴
  2. set noxzeroaxis   # 设定不显示 X 座标轴
  3. show xzeroaxis    # 检查 X 座标轴显示与否

Label

gnuplot 除了绘出图形外,尚可加入注解做为辅助说明。这注解包括文字与线条两方面,其提供的设定有:

功能

绘图参数名称

线条

arrow

文字注解

key, label, time, title, xlabel, ylabel, zlabel

线条

在图上画一线段可以选择有无箭头。其语法为:

[plain] view plaincopy
  1. set arrow {<tag>} {from <sx>,<sy>{,<sz>}}
  2.  {to <ex>,<ey>{,<ez>}} {{no}head}
  3. unset arrow {<tag>}    # 删除一线条
  4. show arrow             # 显示线条使用情况

其中参数 <tag> 是给该条线条一个整数名称,若不设定则为最小可用整数。此线条由坐标 (sx, sy, sz) 到 (ex, ey, ez) (在 2D 中为 (sx, sy)到(ex, ey))。参数 nohead 为画没有箭头的线段,参数 head 或没有 nohead 为画有箭头的线段。图24 中使用没有箭头的线段作为辅助说明。以下为一些例子:

[plain] view plaincopy
  1. # 画一带有箭头的线条由原点到 (1,2)。
  2. set arrow to 1,2
  3. # 画一名为 3 的带箭头线条 由 (-10,4,2) 到 (-5,5,3)。
  4. set arrow 3 from -10,4,2 to -5,5,3
  5. # 改变名为 3 的线条起始点至 (1,1,1)。
  6. set arrow 3 from 1,1,1
  7. # 删除名为 2 的线条。
  8. unset arrow 2
  9. # 删除所有线条。
  10. unset arrow
  11. # 显示线条的使用情形。
  12. show arrow

文字注解

分为设定标题 (title),标示 (label) 与时间 (time) 三部份。标题设定为在图的正上方加上说明本图的文字。其语法为:

[plain] view plaincopy
  1. set title {"<title-text>"} {<xoff>}{,<yoff>}
  2. show title

设定参数 <xoff> 或 <yoff> 为微调标头放置的位址。 xlabel, ylabel, zlabel 的语法与 title 相同,其各自描述一坐标轴。

标示 (label) 为在图上任一位置加上文字说明,一般与线条一并使用。其语法为:

[plain] view plaincopy
  1. set label {<tag>} {"<label_text>"}
  2. {at <x>,<y>{,<z>}}{<justification>}
  3. unset label {<tag>}       # 删除一标示
  4. show label                # 显示标示使用情况

其中参数 <tag> 与 "线条" (arrow) 中 <tag> 意义相同,用以区别不同的 label。参数 <justification> 是调整文字放置的位置,可以是 left,right 或 center。举一些例子:

[plain] view plaincopy
  1. # 将 y=x 放在座标 (1,2) 之处。
  2. set label "y=x" at 1,2
  3. # 将 y=x^2 放在座标 (2,3,4) 之处,并命名为 3。
  4. set label 3 "y=x^2" at 2,3,4 right
  5. # 将名为 3 的标示居中放置。
  6. set label 3 center
  7. # 删除名为 2 的标示。
  8. set nolabel 2
  9. # 删除所有标示。
  10. set nolabel
  11. # 显示标示使用情形。
  12. show label

一般绘一图形后,gnuplot 将函数名称或图形名称置于右上角。 key 参数设定可改变名称放置位置。其语法为:

[plain] view plaincopy
  1. set key
  2. set key <x>,<y>{,<z>}
  3. unset key
  4. show key

其中参数 <x>, <y>, <z> 设定名称放置位置 。unset key 为不显示名称,若使用 set key 则再度显示名称。若使用 set key 0.2, 0.5 则显示函数名称于坐标 (0.2, 0.5) 之处。

[plain] view plaincopy
  1. unset key
  2. plot sin(x), cos(atan(x))

[plain] view plaincopy
  1. <p>set key at 2, 0.5
  2. plot [-pi/2:pi] cos(x), -( sin(x) > sin(x+1) ? sin(x) : sin(x+1))</p>

时间参数设定是将图产生的时间标在图上。其语法为

[plain] view plaincopy
  1. set time {<xoff>}{,<yoff>}
  2. unset time
  3. show time

设定参数 <xoff> 或 <yoff> 为微调时间放置的位址,正数表示向上或向右,负数为反方向,以字的长宽作为单位。

[plain] view plaincopy
  1. set title "sin(x)+sin(2*x)"
  2. set xlabel "X-axis"
  3. set ylabel "Y-axis"
  4. set arrow from  -2,1 to -2.5,0.4
  5. set label "Local max" at -2,1.1
  6. unset key
  7. set time
  8. plot [-5:5] sin(x)+sin(2*x)

转载自:

gnuplot 入门教程1   : http://blog.csdn.net/liyuanbhu/article/details/8502383

gnuplot 入门教程2   : http://blog.csdn.net/liyuanbhu/article/details/8502418

gnuplot 入门教程3   : http://blog.csdn.net/liyuanbhu/article/details/8502450

gnuplot 入门教程4   : http://blog.csdn.net/liyuanbhu/article/details/8502461

gnuplot 让您的数据可视化  : http://www.ibm.com/developerworks/cn/linux/l-gnuplot/index.html

使用 gnuplot 在网页中显示数据   : http://www.ibm.com/developerworks/cn/aix/library/au-gnuplot/index.html

gnuplot 入门教程相关推荐

  1. gnuplot入门教程

    gnuplot入门教程 绘制sin(x) 同时绘制sin(x)和x 设置.取消轴标签 设置.取消标题 设置X.Y轴范围 线画 点画 设置样点个数 图中添加.取消文字 在图中添加.取消箭头或直线 设置图 ...

  2. 【LaTeX】E喵的LaTeX新手入门教程(4)图表

    这里说的不是用LaTeX画图,而是插入已经画好的图片..想看画图可以把滚动条拉到底.前情回顾[LaTeX]E喵的LaTeX新手入门教程(1)准备篇  [LaTeX]E喵的LaTeX新手入门教程(2)基 ...

  3. python 分类_简单机器学习入门教程:用Python解决简单的水果分类问题

    在这篇机器学习入门教程中,我们将使用Python中最流行的机器学习工具scikit- learn,在Python中实现几种机器学习算法.使用简单的数据集来训练分类器区分不同类型的水果. 这篇文章的目的 ...

  4. Kafka入门教程与详解

    1 Kafka入门教程 1.1 消息队列(Message Queue) Message Queue消息传送系统提供传送服务.消息传送依赖于大量支持组件,这些组件负责处理连接服务.消息的路由和传送.持久 ...

  5. 【CV】Pytorch一小时入门教程-代码详解

    目录 一.关键部分代码分解 1.定义网络 2.损失函数(代价函数) 3.更新权值 二.训练完整的分类器 1.数据处理 2. 训练模型(代码详解) CPU训练 GPU训练 CPU版本与GPU版本代码区别 ...

  6. python tornado教程_Tornado 简单入门教程(零)——准备工作

    前言: 这两天在学着用Python + Tornado +MongoDB来做Web开发(哈哈哈这个词好高端).学的过程中查阅了无数资料,也收获了一些经验,所以希望总结出一份简易入门教程供初学者参考.完 ...

  7. python向量计算库教程_NumPy库入门教程:基础知识总结

    原标题:NumPy库入门教程:基础知识总结 视学算法 | 作者 知乎专栏 | 来源 numpy可以说是 Python运用于人工智能和科学计算的一个重要基础,近段时间恰好学习了numpy,pandas, ...

  8. mysql query browswer_MySQL数据库新特性之存储过程入门教程

    MySQL数据库新特性之存储过程入门教程 在MySQL 5中,终于引入了存储过程这一新特性,这将大大增强MYSQL的数据库处理能力.在本文中将指导读者快速掌握MySQL 5的存储过程的基本知识,带领用 ...

  9. python tensorflow教程_TensorFlow入门教程TensorFlow 基本使用T

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 TensorFlow入门教程 TensorFlow 基本使用 TensorFlow官方中文教程 TensorFlow 的特点: 使用图 (graph) 来 ...

最新文章

  1. vuepress 跑不起来?报错?空白页? res.getHeader is not a function?
  2. 百度宣布AI语音调用登顶中国第一,自研芯片+最新端到端模型颠覆传统语音识别算法...
  3. matlab toolbox下载_Matlab自动导出高质量无变形论文插图
  4. iOS:网络编程解析协议一:HTTP超文本传输协议
  5. float取整数部分_一步一步学Python3(小学生也适用) 第六篇: 变量及整数(int)类型...
  6. 【LeetCode从零单排】No19.RemoveNthNodeFromEndofList
  7. 全球化、文化和团队多样性
  8. 使用neo4j_知识图谱Task00:Neo4j安装配置
  9. Tech·Ed 2006中国 实况报道
  10. 下周开幕!给深圳的嵌入式和电子工程师准备的嘉年华来了
  11. python3callable使用_python --- Python中的callable 函数
  12. 前端、后台和连接前端后台的网络数据传输
  13. Python可视化工具Matplotlib 3.0版出炉,改进默认后端选择,饼图终于变圆了
  14. 股票大作手回忆录投机感悟
  15. win10系统优化小工具:Windows10系统优化辅助工具.bat(批处理)
  16. 产品思维30讲(梁宁)-- 整体
  17. 【SonarQube】安装、配置与使用 01
  18. linux权限后面的点,linux 文件权限后面一个点的含义
  19. html下拉加载实现原理,GitHub - sybiele/wxPull: 原生JS实现微信公众号或网页使用下拉加载和上拉刷新...
  20. 寒江独钓前辈的第一个例子的编译运行过程

热门文章

  1. Google Earth Engine(GEE)——基于Landsat的高级植被指数 (AVI)、裸土指数 (BSI) 和冠层阴影指数 (SSI) 的计算
  2. php递归函数实用吗,php递归函数怎么用才有效
  3. 红米手机使用应用沙盒动态修改imei信息
  4. 基于php的超市管理系统,基于PHP的超市管理系统
  5. 「算法基础」之二叉树的遍历和搜索
  6. 禁忌搜索算法实例——TSP
  7. IP、PV和UV的含义和他们之间区别与联系
  8. Linux で *.lzh
  9. bash zshrc ssh
  10. 解决VS调试运行输出窗口字体太小的问题