Plot with Gnuplot——Gnuplot in Action前三章解读

来源:互联网 发布:c 语言入门自学视频 编辑:程序博客网 时间:2024/06/07 16:53

2010-11-30 wcdj

 

Gnuplot in Action
Understanding Data with Graphs
by Philipp K. Janert 2009

gnuplot 4.4 —— An Interactive Plotting Program   (23 September 2010 Version 4.42)

 


 

Introduction

 

Gnuplot is a portable command-line driven  graphing utility forlinux, OS/2,MS Windows, OSX, VMS, and many other platforms. The source code is copyrighted but freely distributed (i.e., you don't have to pay for it). It was originally created to allow scientists and studentsto visualize mathematical functions and data interactively, but has grown tosupport many non-interactive usessuch as web scripting. It is also used as a plotting engine by third-party applications like Octave. Gnuplot has been supported and under active development since1986.

一款移植性很好的基于命令行的画图工具。

Gnuplot supports many types of plots in either 2D and 3D. It can drawusing line, points, boxes, contours, vector fields, surfaces, and various associated text. It also supports various specialized plot types.Demos here( http://gnuplot.sourceforge.net/demo_4.2/ ).

使用plot绘制2D图像,splot绘制3D图像。

Gnuplot supports many different types of output: interactive screen terminals (with mouse and hotkey input), direct output to pen plotters or modern printers, and output tomany file formats (eps, fig,jpeg, LaTeX, metafont, pbm,pdf, png, postscript, svg, ...). Gnuplot is easily extensible to include new output modes. Recent additions include an interactive terminal based onwxWidgets and the creation of mousable graphs for web display using the HTML5 canvas element.

支持很多种文件格式。

 

What is Graphical Analysis?
The basic steps are always the same:
1. Plot the data.
2. Inspect it, trying to find some recognizalbe behavior.
3. Compare the actual data to data that represents the hypothesis from the previous step.
4. Repeat.

图形分析的几个步骤。

Why Graphical Analysis?
Graphical analysis is a discovery tool: we can use it to reveal as yet unknown information in data. In comparison to statistical methods, it helps us to discover new and possibly quite unexpected behavior. Moreover, it helps us to develop an intuitive understanding of the data and the information it contains. Since it does not require particular math skills, it is accessible to anyone with an interest and a certain amount of intuition.
Even if rigorous model-building is our ultimate goal, graphical methods still need to be the first step, so that we can develop a sense for the data, its behavior and quality. Knowing this, we can then select the most appropriate formal methods.

从图像中可以找到更直观的信息。

What is Gnuplot?
Gnuplot is a program to explore data graphically. Its  purpose is to generate plots and graphs from data or functions. It can produce highly polished graphs, suitable for publication, and simple throw-away graphs, when we are merely playing with an idea.
Gnuplot is command-line driven: you issue commands at a prompt, and gnuplot will redraw the current plot in response.
Gnuplot is also interactive: the output is generated and displayed immediately in an output window.
Although gnuplot can be used as a background process in batch-mode, typical use is highly interactive. On the other hand, its primary user interaction is through a command language, not through a point-and-click GUI interface.

使用起来非常方便。

Gnuplot is not GNU
To dispel one common confusion right away: gnuplot is not GNU software, has nothing to do with the GNU project, and is not released under the GNU Publlic License (GPL). Gnuplot is released under a permissive open-source license.
Gnuplot has been around a long time —— a very long time, in fact. It was started by Thomas Williams and Colin Kelley in 1986.

gnuplot和GNU是两码事。

 


 

Gnuplot is command-line oriented: after you start gnuplot, it drops you into an interactive command session, and all commands are typed at the interactive gnuplot prompt.

 

Essential operations


gnuplot> plot "data" using 1:2 with boxes

gnuplot> plot "data" using 1:2 with lines
gnuplot> plot "data" using 1:2 with linespoints

gnuplot> plot "data" using 1:2 with dots

 

gnuplot> set xlabel "x-axes description"
gnuplot> set ylabel "y-axes description"

 

gnuplot> set logscale

 

gnuplot> plot "data1" title "d1" with points, "data2" title "d2" with points, "data3" title "d3" with points, "data4" title "d4" with points

 

gnuplot> unset border
gnuplot> unset xtics
gnuplot> unset ytics
gnuplot> set size square
gnuplot> plot "data1" title "d1" with points, "data2" title "d2" with points, "data3" title "d3" with points, "data4" title "d4" with points

 

plot sin(x), x, x-(x**3)/6
plot [][-2:2] sin(x), x, x-(x**3)/6

 

plot "data" using 1:2

plot "data" using 1:2 with lines, "data" using 1:3 with linespoints

plot "data" u 1:2 w 1, "data" u 1:3 w lp

plot "data" u 1:2 w 1, "" u 1:3 w lp

 

Exporting graphs


For any graph we want to generate (using gnuplot or anything else), we need to specify two things: the format of the graph (GIF, JPG, PNG, and so on) and the output device (either a file or the screen). In gnuplot, we do this using the set command:
set terminal png                       # choose the file format
set output "mygraph.png"        # choose the output device

The complete sequence to export a graph from gnuplot and to resume working is shown in listing below.
The complete workflow to generate a PNG file from gnuplot
1. plot exp(-x**2)                     # some plot command
2. set terminal png                  # select the file format
3. set output "graph.png"       # specify the output filename
4. replot                                  # repeat the most recent plot command, with the output now going to the specified file.
5. set terminal x11                   # restore the terminal settings (这一步可以跳过,在Windows下使用windows选项)
6. set output                           # send output to the screen again, by using an empty filename

 

PS: 或者最后输入quit或exit也可生成文件。

 

Managing large data sets


[1] Multiple data sets per files: index

index { int:start } [ : { int:end } ] [ : { int:step } ]

plot "data" index 1 using 1:2 w linespoints

plot "data" index 2:5             # will plot four data sets total

plot "data" index 2:5:2          # will plot only the data in sets 2 and 4

So, in summary, the index directive lets us select consecutive sets of data form a file.

 

[2] Records spanning multiple lines: every
every { int:step } [ :: { int:start } [ :: { int:end } ] ]

plot "data" every 2 using 1:2 with lines

 

The index and every directives can be used to pick out certain parts from a data file. But what do we do if the data iteself is noisy or otherwise not fit to plot directly? That's the job of the smooth directive, which is the topic of the next section.

 

Smoothing and summarizing data


Gnuplot provides the smooth directive to the plot command as a simple tool to plot nosiy or otherwise messy data files.
The smooth directive takes an additional parameter, which must be one of the following:
unique, frequency, bezier, sbezier, csplines, acsplines
The first two are different from the rest—they provides means to summarize (or otherwise sanitize/clean) data from messy files. The last four provide smooth approximations to noisy data.

 

[1] Plotting unsorted data files

plot "data" u 1:2 smooth unique with linespoints (排序功能)

plot [1989:1994][25:34] "data" u 1:2 smooth uniq with linesp, "" u 1:2 with points (平均数功能)

 

[2] Smoothing noisy data
plot "data" u 1:2 with linesp, "" u 1:2 title "bezier" smooth bezier, "" u 1:2 title "csplines" smooth csplines

 

We see both the raw (noisy) data, as well as the Bezier curve and the spline approximation.
Note how the Bezier curve is a global approximation, providing a smooth represenation of the entire data set. In contrast, splines are local approximations, smoothly connecting adjacent segments individually.

 

Math with gnuplot


Not surprisingly, gnuplot can evaluate mathematical expressions and includes support for many mathematical functions. Gnuplot's syntax for mathematical expressions is straightforward and simialr to the conventions found in other programming languages.
[1] Mathematical expressions
[2] Built-in functions
[3] User-defined variables and functions

PS: gnuplot没有命令补全功能,使用的时候不是特别方便,但是它提供了缩写的机制,可以方便用户对命令的输入。

 

 

 

参考:

Home page: http://www.gnuplot.info/
Download from SourceForge: http://www.gnuplot.info/download.html
Demo scripts for gnuplot version 4.4: http://gnuplot.sourceforge.net/demo/
Official gnuplot documentation: http://www.gnuplot.info/documentation.html
Helping you with gnuplot: http://www.gnuplot.info/help.html
Not so Frequently Asked Questions: http://t16web.lanl.gov/Kawano/gnuplot/index-e.html (good)
Gnuplot in Action, Understanding Data with Graphs, Philipp K. Janert (good)
http://www.manning.com/janert/
http://download.csdn.net/source/1734738
Zhibin Wu, UNIX User Guide
http://www.winlab.rutgers.edu/~zhibinwu/html/unix.htm

 

 

原创粉丝点击