Record and Summarize

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

一、Gnuplot Shell 基本介绍

1、plot命令

(1)画函数曲线:

gnuplot> plot sin(x) with line linetype 3 linewidth 2                              % 或
gnuplot> plot sin(x) w l lt 3 lw 2                                          % 用线画,线的类型(包括颜色与虚线的类型)是3,线的宽度是2,对函数sin(x)作图

gnuplot> plot sin(x) with point pointtype 3 pointsize 2                             % 或
gnuplot> plot sin(x) w p pt 3 ps 2                                          % 用点画,点的类型(包括颜色与点的类型)是3,点的大小是2

gnuplot> plot sin(x) title 'f(x)' with linespoints linetype 3 linewidth 2 pointtype 3 pointsize 2   % 或
gnuplot> plot sin(x) title 'f(x)' w lp lt 3 lw 2 pt 3 ps 2                           % 同时用点和线画,这里title ‘f(x)’表示图例上标'f(x)',如果不用则用默认选项

(2)根据数据文件在坐标系上画线或点

gnuplot> plot "a.dat" u 1:2 w l lt 3 lw 2    % 利用数据文件的第1列和第二列做图,各个点之间使用直线连接
gnuplot> plot "a.dat" u 1:2 w p pt 3 ps 2    % 利用数据文件的第1列和第二列做图,仅仅描绘出各个点

  其中数据文件 a.data的形式如下,第一列代表横坐标的值,第二列代表纵坐标的值

1 1
2 2
3 3
4 5
6 4
7 9

(3)关于命令的简略写法的说明

  在gnuplot中,如果某两个词,按字母先后顺序,前面某几个字母相同,后面的不同,那么只要写到第一个不同的字母就可以了。如with,由于没有其它以w开头的词,因此可以用 w 代替,line也可以用 l 代替。

2、在一个坐标系中画多条线或点

gnuplot> plot sin(x) title ‘sin(x)’ w l lt 1 lw 2, cos(x) title ‘cos(x)’ w l lt 2 lw 2 % 两条曲线是用逗号隔开的。画多条曲线时,各曲线间均用逗号隔开就可以了。

  以上例子中是对函数作图,如果对数据文件作图,将函数名称换为数据文件名即可,但要用单引号引起来。

3、图例的设置

(1)为图例加上边框

gnuplot> set key box

(2)设置图例的位置

gnuplot> set key left     % 放在左边,有left 和right两个选项
gnuplot> set key bottom     % 放在下边,只有这一个选项;默认在上边
gnuplot> set key outside     % 放在外边,但只能在右面的外边

  以上三个选项可以进行组合。如:  

gnuplot> set key left bottom % 表示左下边

  还可以直接用坐标精确表示图例的位置,如

gnuplot> set key center at 0.5,0.6     % 将图例的中心点放在0.5,0.6的位置处,即以图例的中心点定位

(3)把图例的title文字和图线示例调换位置

gnuplot> set key reverse

(4)调整图例边框宽度 width(或高度 height)

gnuplot> set key width 1

(5)调整 title文字对齐方式(Left 或者 Right,注意首字母大写)

gnuplot> set key Left

(6)调整图例行间隔

gnuplot> set key spacing 1.2

(7)调整图线示例长度

gnuplot> set key samplen 2

4、坐标轴的设置

gnuplot> set xlabel ‘x’   % x轴标记为‘x’
gnuplot> set ylabel ‘y’   % y轴标记为’y’
gnuplot> set xtics 1.0    % x轴的主刻度的宽度为1.0,同样可以为y轴定义ytics
gnuplot> set mxtics 3    % x轴上每个主刻度中画3个分刻度,同样可以为y轴定义mytics
gnuplot> set xrange[-10:10]   % 设置x轴刻度范围
gnuplot> set yrange[-5:5]     % 设置y轴刻度范围

  同样可以为上边的x轴(称为x2轴)和右边y(称为y2轴)轴进行设置,即x2tics,mx2tics,y2tics,my2tics。

gnuplot> set xtics nomirror
gnuplot> unset x2tics    
% 以上两条命令去掉上边x2轴的刻度
gnuplot> set ytics nomirror
gnuplot> unset y2tics     
% 以上两条命令去掉右边y2轴的刻度

5、在坐标系中插入文字

gnuplot> set label ‘sin(x)’ at 0.5,0.5  % 在坐标(0.5,0.5)处加入字符串’sin(x)’

6、在坐标系上画箭头

gnuplot> set arrow from 0.0,0.0 to 0.6,0.8              % 从(0.0,0.0)到(0.6,0.8)画一个箭头
gnuplot> set arrow from 0.0,0.0 to 0.6,0.8 lt 3 lw 2         % 这个箭头颜色类型为3,线宽类型为2
gnuplot> set arrow from 0.0,0.0 to 0.6,0.8 nohead lt 3 lw 2    % 利用nohead可以去掉箭头的头部,这就是添加直线的方法。

  注意箭头的属性是在创建箭头时同时被设置的,箭头通常用于在一个已经成型的图中连接两个点。

7、设置坐标系在背景图中的大小和位置

gnuplot>set size 0.5,0.5      % 长宽均为默认的一半,即总大小变为原来的1/4
gnuplot>set origin 0.0,0.5     % 设定图的最左下角的那一点在图形面板中的位置。这里图将出现在左上角。

8、画三维图

gnuplot>splot ‘文件名’ u 2:4:5   % 以第二和第四列作为x和y坐标,第五列为z坐标。

9、将图形输出到文件

  在gnuplot中,输出文件的格式是由terminal来控制的。默认的情况下,都是输出到屏幕,即终端模式为X11。如果想输出到文件,则必须对 terminal进行设置。要了解有那些终端类型,可以输入 set terminal后回车,所有支持的终端模式(或文件格式)都列出来了。就我来说,输出文件的格式用得最多的是ps和eps文件。这在运行画图命令 plot或splot前必须先运行如下两条命令:

gnuplot>set term post eps color solid enh
gnuplot>set output ‘a.eps’

  其中第一条命令为设置终端模式,post即为postscript模式,这是输出到ps或eps文件必须有的。后面的eps,color,solid以及 enh选项均可以有或没有,根据你的需要。eps表示输出为eps文件,没有就输出为ps文件;color表示输出图形为彩色,不用就会保存为黑白的;solid表示输出图中的所有线都用实线,不用则除了第一条线为实线外,其它的均用不同的虚线;使用enh(enhanced)选项可以在图中插入上下标、希腊字母和特殊符号,不用则不能实现这三种插入形式,但插入字符串仍然可以实现。后面的选项可以根据自己的需要选择一个或几个。

  第二条命令对output的设置表示要输出的文件的名字。但是请注意,运行完这条命令后,还仅仅是定义了输出文件的名字,实际上,图还没画到这个文件里去。因此运行这两条命令还只是进行了必须的设置。然后运行plot命令,例如:

gnuplot>plot sin(x) w l  % 或
gnuplot>replot      % 假如前面已经运行过plot或replot命令的话

  这时,图形就在上面指定的文件中了。

  要输出为其它格式,同样要进行这样的设置,比如要输出为jpg格式,则在运行画图命令前先运行如下命令:

gnuplot>set term jpeg
gnuplot>set output ‘a.jpg’

  如果要由其它模式再返回到输出到屏幕,则运行如下命令:

gnuplot>set term X11

 

二、Gnuplot的python库介绍

import Gnuplot
data1 = [[3, 0.03], [4, 0.02], [5, 0.017]]
data2 = [[3, 0.027], [4, 0.015], [5, 0.014]]

gp = Gnuplot.Gnuplot(persist = 1)
gp('set terminal x11 size 350,225') 
gp('set pointsize 2')
gp('set yrange [0.0:0.05]')
plot1 = Gnuplot.PlotItems.Data(data1, with_="linespoints lt rgb 'black' lw 6 pt 1", title="data1")
plot2 = Gnuplot.PlotItems.Data(data2, with_="linespoints lt rgb 'blue' lw 6 pt 8", title="data2")
gp.plot(plot2, plot1)

epsFilename='testLines.eps'
gp.hardcopy(epsFilename, terminal = 'postscript', enhanced=1, color=1) #must come after plot() function
gp.reset() 

 

posted on 2014-01-17 15:20  zhangjing327  阅读(2574)  评论(0编辑  收藏  举报