Gnuplot
Table of Contents
1. Gnuplot 简介
Gnuplot is a portable command-line driven graphing utility. It was originally created to allow scientists and students to visualize mathematical functions and data interactively, but has grown to support many non-interactive uses such as web scripting. It is also used as a plotting engine by third-party applications like Octave.
参考:
Gnuplot Homepage: http://www.gnuplot.info/
Gnuplot reference card: http://www.gnuplot.info/docs_4.0/gpcard.pdf
Gnuplot Tutorial, By Dr R Lindebaum: http://physicspmb.ukzn.ac.za/index.php/Gnuplot_tutorial
使用 gnuplot 科学作图:http://www.phy.fju.edu.tw/~ph116j/gnuplot_tutorial.pdf
1.1. 交互和非交互方式启动 Gunplot
交互方式运行 Gunplot:
$ gnuplot
非交互方式(batch 方式)运行 Gnuplot:
$ gnuplot 1.gpl 2.gpl # 文件1.gpl和2.gpl为用户自定义的gnuplot命令脚本
1.2. 查找帮忙信息
可以在交互式提示符中输入 help
命令来查找相关帮忙信息。
如,查看 plot 命令的用法:
gnuplot> help plot `plot` is the primary command for drawing plots with `gnuplot`. It offers many different graphical representations for functions and data. `plot` is used to draw 2D functions and data. `splot` draws 2D projections of 3D surfaces and data. Syntax: plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>} Each plot element consists of a definition, a function, or a data source together with optional properties or modifiers: plot-element: {<iteration>} <definition> | {sampling-range} <function> | <data source> {axes <axes>} {<title-spec>} {with <style>} The graphical representation of each plot element is determined by the keyword `with`, e.g. `with lines` or `with boxplot`. See `plotting styles`. The data to be plotted is either generated by a function (two functions if in parametric mode), read from a data file, or read from a named data block that was defined previously. Multiple datafiles, data blocks, and/or functions may be plotted in a single plot command separated by commas. See `data`, `inline data`, `functions`. A plot-element that contains the definition of a function or variable does not create any visible output, see third example below. Examples: plot sin(x) plot sin(x), cos(x)
又如,查看支持哪些风格的输出:
gnuplot> help with Functions and data may be displayed in one of a large number of styles. The `with` keyword provides the means of selection. Syntax: with <style> { {linestyle | ls <line_style>} | {{linetype | lt <line_type>} {linewidth | lw <line_width>} {linecolor | lc <colorspec>} {pointtype | pt <point_type>} {pointsize | ps <point_size>} {fill | fs <fillstyle>} {nohidden3d} {nocontours} {nosurface} {palette}} } where <style> is one of lines dots steps errorbars xerrorbar xyerrorlines points impulses fsteps errorlines xerrorlines yerrorbars linespoints labels histeps financebars xyerrorbars yerrorlines surface vectors parallelaxes or boxes boxplot ellipses image boxerrorbars candlesticks filledcurves rgbimage boxxyerrorbars circles histograms rgbalpha pm3d
2. 实例
2.1. 第一个实例
Gnuplot 的默认输出终端是屏幕,可通过改变默认的输出终端来将输出图片保存到指定文件。
下面例子将画图 sin(x),保存到文件 gnuplot_sin.svg:
set terminal svg set output "gnuplot_sin.svg" # 设置输出文件名,文件名要用引号,否则提示错误。 plot sin(x)
在当前目录下将生成文件 gnuplot_sin.svg:
Figure 1: plot sin(x)
上面的图像比较朴素,我们用下面命令给它增加标题,X/Y 轴的标注等等:
set terminal svg set output "gnuplot_sin_1.svg" # 设置输出文件名,文件名要用引号,否则提示错误。 set title "This is my title XXXXXX" # 设置标题 set key off # 设置不显示右上角的图例 set xlabel 'This is x' # 设置 X 轴标注 set ylabel 'This is sin(x)' # 设置 Y 轴标注 set grid xtics # 设置显示 X 轴网格 set grid ytics # 设置显示 Y 轴网格 set xrange [0:10] # 设置 X 轴的显示范围为[0:10] plot sin(x)
生成的图像如下所示:
Figure 2: plot sin(x), another example
2.2. 实例:展示多个文件
假设有下面两个数据文件,每个文件有 10 行内容:
$ cat data1 0.9 1.5 1.6 1.7 1.9 2.4 3.8 6.8 6.9 7.2 $ cat data2 6.8 6.9 7.2 7.3 7.2 7.1 7.7 8.4 8.4 8.6
想把上面两个数据文件展示到一个图片中,每个数据文件的内容分别画成线。可以这样实现:
set terminal svg set output "gnuplot_two_data_files.svg" set xlabel 'This is xlabel' set ylabel 'This is ylabel' plot 'data1' title 'ABC' with linespoints, 'data2' title 'XYZ' with linespoints # 用with linespoints可连接点为线。
生成的图像如下所示:
Figure 3: Represent two data file
2.3. 实例:展示文件中的指定列
假设有下面数据文件:
$ cat data2 # time A B C D E 14:10:50 33 4 27 36 4.00 15:10:50 29 3 14 28 4.00 16:10:50 35 1 21 31 4.00 17:10:49 38 2 29 39 4.00 18:10:40 42 3 29 35 4.00
想以第 1 列为 X 轴,把第 2 列和第 4 列内容为 Y 轴展示。可以这样实现:
set terminal svg set output "gnuplot_two_fields.svg" set xdata time set timefmt "%H:%M:%S" set style data lines plot "data2" using 1:2 title "Field A", '' using 1:4 title "Field C"
生成的图像如下所示:
Figure 4: Represent two fields
说明 1:数据文件中以#开头的行会被忽略。
说明 2:"using 1:2"表示以第 1 列为 X 轴,第 2 列为 Y 轴。
参考:
Using gnuplot to display data in your Web pages: http://www.ibm.com/developerworks/aix/library/au-gnuplot/
2.4. 实例:展示前对列做算术运行
假设有下面数据文件:
$ cat data2 # time A B C D E 14:10:50 33 4 27 36 4.00 15:10:50 29 3 14 28 4.00 16:10:50 35 1 21 31 4.00 17:10:49 38 2 29 39 4.00 18:10:40 42 3 29 35 4.00
想以[10,20,30,40,50]为 X 轴,第 4 列和第 5 列的和为 Y 轴展示。可以这样实现:
set terminal svg set output "gnuplot_add_fields.svg" set style data linespoints plot "data2" using (($0+1)*10):($4+$5) title "Field C+D"
生成的图像如下所示:
Figure 5: Represent add fields
说明 1:进行算术运行时,可以用$1,$2 等来代表数据文件中各列。
说明 2:$0 是 Gnuplot 中内置的 Pseudo column,它表示序列[0,1,2,3,...],当在 using 中不指定 X 轴数据时,将默认使用$0 作为 X 轴。上面例子中明确要求 X 辆数据为[10,20,30,40,50],用($0+1)*10)即可达到要求。
2.5. 实例:过滤某些行(利用 awk)
假设有下面数据文件:
$ cat data3 10:15:8:6.06000000: 10:15:2:19.03400000: 10:20:8:63.50600000: 10:20:2:24.71800000: 10:25:8:33.26200000: 10:30:8:508.23400000: 20:15:8:60.06300000: 20:15:2:278.63100000: 20:20:8:561.18000000: 20:20:2:215.46600000: 20:25:8:793.36000000: 20:25:2:2347.52900000: 20:30:8:5124.98700000: 20:30:2:447.41000000:
想仅显示第 4 列数据,且满足条件第 2 列大于 15,第 4 列小于 500。
set terminal svg set output "gnuplot_filter_by_awk.svg" plot "< awk -F: '$2 > 15 && $4 < 500 {print $4}' data3" with linespoints
生成的图像如下所示:
Figure 6: Represent add fields
说明:Gnuplot 支持命令的标准输出作为源数据,用"< command"代替数据文件名即可。
参考:
How do I use UNIX commands inside gnuplot: http://folk.uio.no/hpl/scripting/doc/gnuplot/Kawano/datafile3-e.html
http://stackoverflow.com/questions/11187691/gnuplot-conditional-plotting-2-15-2-1-0-with-lines
2.6. 输出纯文本图像(set terminal dumb)
用 set terminal dumb 可以输出 ASCII 码组成的“图像”。如:
$ gnuplot gnuplot> set terminal dumb Terminal type set to 'dumb' Options are 'feed size 79, 24 aspect 2, 1' gnuplot> plot sin(x) 1 +-+--------------***---------------+---**-----------+--------**----+-+ + * * + * ** + * * + 0.8 +-+ * * * * sin(x) *******-+ | * * * * * * | 0.6 *-+ * * * * * * +-+ |* * * * * * * | 0.4 +*+ * * * * * *+-+ |* * * * * * * | 0.2 +*+ * * * * * *-+ 0 +-* * * * * * *-+ | * * * * * * *| -0.2 +-+* * * * * * +*+ | * * * * * * *| -0.4 +-+* * * * * * +*+ | * * * * * * * -0.6 +-+ * * * * * * +-* | * * * * * * | -0.8 +-+ * * * * * * +-+ + * * + ** * + * * + -1 +-+----**--------+-----------**----+--------------***--------------+-+ -10 -5 0 5 10
3. Gnuplot Tips
3.1. 指定数据分隔符
默认 Gnuplot 以空格作为分隔符处理数据文件,用 set datafile separator
可定制数据文件的分隔符,如:
set datafile separator "," # 设置逗号为记录分隔符