Gnuplot的使用技巧

0. 链接

1. 输出格式

gnuplot> set terminal x11      %输出到屏幕(Linux)

gnuplot> set terminal postscript portrait noenhanced monochrome blacktext \
   dashed dashlength 1.0 linewidth 1.0 defaultplex \
   palfuncparam 2000,0.003 \
   butt "Helvetica" 14'
gnuplot> set output "test.ps"  %输出到PS文件, 以便插入到LaTeX文件中.

gnuplot> set terminal png small color picsize 640 480
gnuplot> set output "test.png"  %输出到图像文件, 以便插入到html文件中.

2. 基本绘图命令

gnuplot>  plot "test2d.dat" with lines   %绘制曲线图(二维图形)
gnuplot>  splot "test3d.dat" with lines  %绘制曲面图(三维图形)

test2d.dat 中的数据为两列, 第一列对应横坐标, 第二列为纵坐标(函数值)

test3d.dat 中的数据为三列, 第一,二列对应坐标, 第三列为纵坐标(函数值),
例如:
x1   y1    z11
x1   y2    z12
x1   y3    z13
                  //空一行!
x2   y1    z11
x2   y2    z12
x2   y3    z13
                   //空一行!
x3   y1    z11
x3   y2    z12
x3   y3    z13


gnuplot> unset surface  %不绘制表面图
gnuplot> set contour    %绘制等值线图
gnuplot> set cntrparam levels discrete .1,1/exp(1),.9  %定制等值线
gnuplot> set cntrparam levels auto 5 
gnuplot> set view 0,0



3. 一些例子

set hidden3d; set ticslevel 0; set parametric
set isosamples 28,14
set urange [0:2*pi]; set vrange [-pi:pi]
set size 0.8,1; set view 60,20,1,1
set ticslevel 0
splot cos(u)+.5*cos(u)*cos(v),sin(u)+.5*sin(u)*cos(v),.5*sin(v), \
     1+cos(u)+.5*cos(u)*cos(v), .5*sin(v), sin(u)+.5*sin(u)*cos(v)
set term table
set out "tori.table"
replot
!gnuconvf -s 200 -i tori.table -o tori.ppm -r 0.85  -X50 -Y-100 -Z50
!convert -transparency black tori.ppm tori.jpg
set samples 36
set isosample 36,36
set hidden3d
set ticslevel 0
splot sin(x)*sin(y)/x/y
set term table
set output "3dsurf.table"
replot
!gnuconvf -s 200 -i 3dsurf.table -o 3dsurf_p.ppm 
!display ./3dsurf_p.ppm

!gnuconvf -s 200 -i 3dsurf.table -o 3dsurf_f.ppm -f
!display ./3dsurf_f.ppm

!gnuconvf -s 200 -i 3dsurf.table -o 3dsurf_l.ppm -l
!display ./3dsurf_l.ppm
set hidden3d; set ticslevel 0
set parametric; set samples 30; set isosamples 30,30
set ticslevel 0; set size 0.7,1
set urange[0:3.1*pi]; set vrange[-pi:pi]
x(u,v)=u*(1+0.5*cos(v))*cos(u)
y(u,v)=0.5*u*sin(v)
z(u,v)=-u*(1+0.5*cos(v))*sin(u)
splot x(u,v),y(u,v),z(u,v)
set term table; set out "3dsurf.table"
replot
!gnuconvf -s 200 -i 3dsurf.table -o 3dsurf7.ppm  -c0.7,0.6,0.2
!ppmchange black cadetblue 3dsurf7.ppm > shell.ppm
!convert shell.ppm shell.jpg
set parametric; set hidden3d
set vrange[0:2*pi]
set isosamples 31,31;set ticslevel 0
a=1.0; b=4.0; u0=atan(a/b)
set urange[0:u0*3]
r(u)= u < u0 ? b*tan(u) : a
x(u,v) = r(u)*cos(v)
y(u,v) = r(u)*sin(v)
z(u) = u < u0 ? b : a/tan(u)
splot x(u,v),y(u,v),z(u)
set term table
set out "cylinder1.table"
replot
!gnuconvf -i cylinder1.table -o cylinder1_cnv.ppm -F -X80 -Y-80\
          -c0.0,0.7,0.7 -g -s 200
!convert cylinder1_cnv.ppm cylinder1_cnv.jpeg
!display cylinder1_cnv.jpeg
# 修改绘制图形的类型,包括线的宽度等,定义min和max函数。
min(x,y) = (x < y) ? x : y
max(x,y) = (x > y) ? x : y
 set term post port color 15
set output "penalty-plot2.eps"
set style line 1 linetype 1 linewidth 8
set style line 2 linetype 2 linewidth 8
set style line 3 linetype 3 linewidth 8
set style line 4 linetype 4 linewidth 8
set style line 5 linetype 5 linewidth 8
set style line 6 linetype 6 linewidth 8
set style line 7 linetype 7 linewidth 8
set yrange[0:100]
plot [x=0.1:10] 100./x+200*(max(0.0,x-5))**2 w l ls 1,\
                100./x+20*(max(0.0,x-5))**2 w l ls 2,\
                100./x+2*(max(0.0,x-5))**2  w l ls 3,\
                100./x+0.2*(max(0.0,x-5))**2 w l ls 4,\
                100./x+0.0*(max(0.0,x-5))**2  w l ls 5
# 改变“符号”的大小
set xrange[0:2*pi]
set pointsize 2
plot sin(x) with points 1 6
# 改变图形的“边框(border)”的类型
set border 3
set xtics nomirror
set ytics nomirror
# 绘制正态分布或高斯分布用于PDFLatex制作的幻灯片
set term wxt enhanced   #此处的"enhanced"用于插入希腊字符
set style line 1 linetype 1 linewidth 8
set style line 2 linetype 2 linewidth 8
pl exp(-(x-0.0)**2/2./2./2.)/2./sqrt(2.*pi) w l ls 1 t "{/Symbol s}=2",\
   exp(-(x-0.0)**2/2./3./3.)/3./sqrt(2.*pi) w l ls 2 t "{/Symbol s}=3"
set term post port solid color enhanced   20
set out "normal.eps"
replot
# 使用上标或下标
gnuplot> set terminal postscript eps enhanced 
gnuplot> set key spacing 1.3
gnuplot> set xrange [ 0 : pi ]
gnuplot> set yrange [ 0 : 1.5 ]
gnuplot> plot sin(x)**2 ti "sin^2(x)", \
         sin(x)**3      ti "sin^3(x)",\
         sqrt(sin(x))   ti "sin^{1/2}(x)"

希腊字符对照表.

ALPHABET SYMBOL ALPHABET SYMBOL alphabet symbol alphabet symbol
A Alpha N Nu a alpha n nu
B Beta O Omicron b beta o omicron
C Chi P Pi c chi p pi
D Delta Q Theta d delta q theta
E Epsilon R Rho e epsilon r rho
F Phi S Sigma f phi s sigma
G Gamma T Tau g gamma t tau
H Eta U Upsilon h eta u upsilon
I iota W Omega i iota w omega
K Kappa X Xi k kappa x xi
L Lambda Y Psi l lambda y psi
M Mu Z Zeta m mu z zeta

You can also specify various postscript characters by octal codes, for example, {/243} is a pound (L) mark, {/247} is a section mark. See ps_guide.ps which comes with gnuplot source distribution in detail.

The next example is to draw the linear function y=Alpha x+ Gamma and two Greek letters with those values in the figure.

gnuplot> set terminal postscript eps enhanced 
gnuplot> set xrange [ 0 : 5 ]
gnuplot> set label "{/Symbol a}=0.5, {/Symbol g}=0.2" at 2,0
gnuplot> plot 0.5*x-0.2 ti "y={/Symbol a}x-{/Symbol g}"
#  在一个框里绘制多个图形
set multiplot;     # get into multiplot mode
set size 1,0.5;  
set origin 0.0,0.5;   plot sin(x); 
set origin 0.0,0.0;   plot cos(x)