Gnuplot(command-driven interactive function plotting program)是linux下的一种绘图工具。
在命令行下输入gnuplot来启动, 然后输入plot “awk-file”。 awk-file中含有X
和Y轴的数据。
gnuplot的环境参数
参数的使用通过set来设定参数,用unset来取消设定, show来显示当前参数的设定。
坐标轴参数
功能 | 名称 |
坐标点设定 | xtics , ydtics |
网格设定 | grid |
坐标显示方式 | logscale |
显示范围 | autoscale , xrange , yrange |
是否显示坐标轴 | xzeroaxis , yzeroaxis |
具体用法:
1. xtics 对x坐标上起始点,结束点,间隔设定。 比如 set xtics -10, 1, 10, 为x轴显示间距为1的-10到10的数字。 取消设定用 unset xtics, 显示x轴标点状态用show xtics。
2. 显示网格 set grid
3. 坐标显示方式,一般为线性方式,如果要显示对数方式用set logscale <axes> <base>, 这里ases为X轴,Y轴,Z轴任意组合,base默认为10。 改回线性方式用 unset logscale
4.设定显示范围, 设定autoscale后会自动调整显示范围,也可以自定义。用set xrange [{<xmin>:<xmax>}], 比如 set xrange [0:10]是x轴只显示0到10
5. 是否显示坐标轴, set xzeroaxis显示X坐标轴, 取消用 unset xzeroaxis
标签(label)
用来加入注释说明, 注释包括线段和文字
功能 | 名称 |
线 段 | arrow |
文字 | key,label,title,xlabel,ylabel
|
1. 画线条(可选择有无箭头)
set arrow {<tag>} {from <sx>,<sy>{,<sz>}}
{to <ex>,<ey>{,<ez>}} {{no}head}
unset arrow {<tag>} # 刪除线段
show arrow # 线段状态
其中 <tag> 是給线 段加个名称,默认为一个最小可用整数。线 段由(sx, sy, sz)到(ex, ey, ez)(注:在2D中是 (sx, sy)到(ex, ey))。 nohead 不带箭头, head 或留空为带箭头。
例:
#原点到 (1,2) 的箭头。 gnuplot> set arrow to 1,2 #名称<tag>为3的线段由(0.4, 0.3 ) 到 (0.2, 0.1)。 gnuplot> set arrow 3 from 0.4, 0.3 to 0.2, 0.1 # 删除线段3 unset arrow 3 # 删除所有线段 unset arrow
2. 文字注释: 包含标题title和标签label两种
(1)标题(title) :图正上方加上說明文字。
set title {"<title-text>"} {<xoff>}{,<yoff>}
其中 <xoff> 或 <yoff> 为标题位置。 xlabel, ylabel与title相同,分别给X,Y加标题。
(2)标签 (label) :图中加入文字说明,与箭头一起使用。
set label {<tag>} {"<label_text>"} {at <x>,<y>{,<z>}}{<justification>}
unset label {<tag>} # 刪除一个标签
show label # 显示状态
其中 <tag > 与arrow 中 <tag > 相同 , 用以区分不同的 label 。 <justification> 用来调整位置,选项为 left ,right 或 center 。
例:
# 將 y=x 放在 (1,2) 。 gnuplot> set label “y=x”at 1,2 # y=x^2 放在 (2,3) ,命名为 3。 gnuplot> set label 3 "y=x^2" at 2,3 right # 名为 3 的标签居中 gnuplot> set label 3 center # 刪除名為3的標示。 gnuplot> unset label 3 # 刪除所有標示。 gnuplot> unsel label
key参数可以用来改变函数名或图形档案文件(比如 awk-file)的位置, 默认为右上角。
set key <x>,<y>{,<z>} # <x>, <y>, <z> 設定位置。
unset key #不显示
set key outside 函数名在曲线图右侧外部显示
set key box 函数名显示轮廓
图样(Style)
GNUPLOT以读入文件中的坐标值来绘制图案的,它提供9种显示图样, 分别是:
- lines : 相邻节点以线连接。 如
plot sin(x) with lines
- points : 每个点以一个符号画出。 如
plot sin(x) with points
- linespoints : 同时具有 lines 及 points 的功能。 如
plot sin(x) with linespoints
- impulses : 將每一點畫一垂直線至 X 軸。如
plot sin(x) with impulses
- dots : 每个点画一个小点。如 plot sin(x) with dots
- steps : 以阶梯形连接两点。如(x1,y1),(x2,y2)两点,以(x1,y1)到(x2,y1)和(x2,y1)到(x2,y2) 两线段连接。如 plot sin(x) with steps
- errorbars : 对每一点 (x,y),画一由 (x,ylow) 至 (x,yhigh) 的线段。並在线段两端做上 tic mark。如
plot sin(x) with errorbars
- boxes : 以X坐标为中心从X轴到Y坐标点画一个方格(直方图)。 如
plot sin(x) with boxes
- boxerrorbars : 組合 errorbars 與 boxes 兩者功能。如
plot sin(x) with boxerrorbars
boxes补充: The width of the box is obtained in one of three ways. If it is a data plot and the data file has a third column, this will be used to set the width of the box. If not, if a width has been set using the set boxwidth command, this will be used. If neither of these is available, the width of each box will be calculated automatically so that it touches the adjacent boxes
输出(Ootput)
把画出来的图存为GIF格式。
#把输出存成gif格式,內定为X11 terminal
gnuplot> set terminal gif
#把输出的文件名设为cbr_delay.gif
gnuplot> set output “cbr_delay.gif”
#把图重画一次
gnuplot> replot
然后就会产生cbr_delay.gif的图片
gnuplot> set output # set output back to default
gnuplot> set terminal x11 # ditto for terminal type, 可选项x11, wxt等
或
set terminal wxt
绘制多个曲线
多个数据来源以逗号分隔。
例
gnuplot> plot sin(x), cos(x)
gnuplot> plot sin(x) with boxes, cos(x)
with boxes
参考:
内容主要参考柯老师关于ns的网站,很感谢他分享给我们的教程。http://140.116.72.80/~smallko/ns2/tool.htm