您的位置:首页 > 其它

gnuplot画图技巧总结

2014-11-27 18:20 344 查看


                                                                   ------------------------------------------------------------------------------------

                                                                                                    欢迎转载,请附上链接

                                                                         http://blog.csdn.net/iemyxie/article/details/41548583

                                                                   -----------------------------------------------------------------------------------

最近用gnuplot画了不少图,关键字神马的每次用到都要重新去查使用文档,简直神烦。所以干脆将常用的技巧总结成文,以便日后查阅。

一、gnuplot简介

可能我说了半天还有人不造gnuplot是个啥东西呢。gnuplot,轻量级画图神器。放上维基百科的解释:gnuplot是一套跨平台的数学绘图自由软件。使用交互式接口,可以绘制数学函数图形,也可以从纯文字档读入简单格式的座标资料,绘制统计图表等等。它不是统计软件,也不是数学软件,它纯粹只是一套函数/资料绘图软件。它可以产生PNG,SVG,PS,HPGL,……等等开放的图形档案格式的输出,供文书处理/简报/试算表/……等等软件汇入。

功能:

绘画二维或三维的图像

绘画数学函数

从其他文档读入数据,绘画统计图表

被外部程式(如GNU Octave)调用

点击下载gnuplot

二、画点

先放一张博主初次接触gnuplot画的第一张图。



附上部分代码:

set term post eps color enh solid //固定格式
set term pdfcairo lw 2 font 'Times_New_Roman,8'
datafile='AfterP-H-kmeans++-k=6.data'
set output 'AfterP-H-kmeans++-k=6.pdf'
set title 'AfterP-H-kmeans++-k=6'
set multiplot //允许重复画
set key outside vertical //将标识置于图外并保持垂直排列
plot datafile using 7:1 title 'cluster0' with points lc rgb 'pink'


a。

set :所有gnuplot设置均以set开头

• term :即terminal,终端,指示图片输入输出的格式。后面可以跟post eps,png,jpg等

• color :可以输出彩色图片和曲线

• enh :enhance,加强,可以输入各种符号,如希腊字母等。

• solid:可使用实体线。

• datafile=“XXX.data” 数据文件,文件可以没有后缀,或txt,或data都可以。

• set output “XXX.jpg”输出名为XXX.jpg的图片Œ

•using后的7:1意思是使用数据文件的第7列作为x轴,第1列作为y轴。注意x轴的值在前。

•“with points”即为画点。
b。

关于key的放置位置,可以根据不同需求进行调整。

官方手册上解释如下:

set key {on|off} {default}
{{inside | outside} | {lmargin | rmargin | tmargin | bmargin}
| {at <position>}}
{left | right | center} {top | bottom | center}
{vertical | horizontal} {Left | Right}
{{no}opaque}
{{no}reverse} {{no}invert}
{samplen <sample_length>} {spacing <vertical_spacing>}
{width <width_increment>}
{height <height_increment>}
{{no}autotitle {columnheader}}
{title "<text>"} {{no}enhanced}
{font "<face>,<size>"} {textcolor <colorspec>}
{{no}box { {linestyle | ls <line_style>}
| {linetype | lt <line_type>}
{linewidth | lw <line_width>}}}
{maxcols {<max no. of columns> | auto}}
{maxrows {<max no. of rows> | auto}}
unset key
show key

记住一些常用的就可以了:vertical:竖直排列;horizontal:水平排列;right top:右上角;left top:左上角;outside:画外放置;inside:画内放置等。

c。
如何运行?可以将代码保存为“XXX.gnu”,在命令行中输入:gnuplot XXX.gnu

或者保存为“XXX.plt”这样直接双击此文本文档即可得到结果,当然你的代码必须是正确的。。否则。你的图片大小会是0k。。

推荐第二种方法,比较方便。

二、画线

a。单条线

照例,先放一张自己用gnuplot画的图



很简单,一看代码你就明白了。

set term post eps color enh solid
set term pdfcairo lw 2 font 'Times_New_Roman,8'
datafile='realDuration'
set output 'realDuration-w1t1.pdf'
set xlabel 'Time(ms)' //设置横轴名称
set ylabel 'Duration(ms)' //设置纵轴名称
set grid //在图中显示网格
plot datafile using 6:4 notitle with lines
set output
b。画多条线及平滑处理

平滑选项指定如何使数据之间的连线平滑。为了使数据制图美观和合理, 根据数据绘制的曲线有不同的平滑算法。 

smooth {unique | frequency | cumulative | cnormal | kdensity| csplines | acsplines | bezier | sbezier}

autoscale 选项打开,将使得平滑曲线总体在图片边界内。

autoscale 没有开启, 并且平滑选项为 acspine 和 cspline, 曲线采样将穿过输入数据的 x 区域。

Acsplines 自然样条平滑。数据局部关于 x 单调, 曲线是分段函数。由系数符合输入权重的, 三次多项式构造。

权重由第三输入列表示。

Plot 'data-file' using 1:2:(1.0) smooth acsplines

输入权重的数量级将定性的影响曲线的分段数目。如果某个权重较大, 那么它所在点对曲线影响越大。

曲线逼近连接各个点的 3 次自然样条曲线。如果权重较小, 曲线由数个小片段组成, 总体上是平滑的。

极限情况是单个片段由加权线性最小二乘法产生, 逼近所有数据。数据误差可用作平滑权重。

例子:(以下两张图片来自@Ivan

命令:

plot "price.dat" using 1:2 with linespoints,"" using 1:2 title "bezier" smooth bezier,"" using 1:2 title "csplines" smooth csplines



plot [1975:1995][40:160] "price.dat" using 1:2 with points title "price.dat", "" u 1:2:(1) smooth acsplines title "1", "" using 1:2:(1/50.) smooth acsplines title "1/50", "" using 1:2:(50) smooth
acsplines  title "50", "" using 1:2:(1/10000.) smooth acsplines title "1/10000"



画多条线的方法也很简单:如上所示的,plot "xxx","xxx2",“xxx3"即可

三、线和点的种类



emf格式下,gnuplot有十五种颜色lt
1 至 lt 15。lt 16也为红色,但线形变为虚线。



emf格式下的前30种点、线

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息