Showing posts with label Gnuplot. Show all posts
Showing posts with label Gnuplot. Show all posts

Wednesday, March 11, 2015

Gnuplot Tips (1)



GNUPLOT Tips (1)

For people who work under Linux, if you often have to draw some simple two-dimensional drawings and three-dimensional figure, then, gnuplot is undoubtedly a very good choice, not only the beautiful graphics, and easy to operate. However, if you need higher-quality three-dimensional figure, please use a number of other professional graphics software. Suggest that you learn to use gnuplot. This small software is usually Redhat Linux comes, but comes with version 3.7, it is recommended to upgrade to 4.0, the new version has many new features. The latest version can go to http://www.gnuplot.info/

Some of the most basic operation please read the instructions. I just summarized the problems and solutions when I using gnuplot to help those who previously could not or unskilled user to get a quick start. This brief summary cannot cover everything, so please do not complain about it. I believe that after reading it, the answer of the most common problems can be found here. If everyone has some skills or experiences that not include in here, or have other suggestions, please leave me a comment to help me improve this summary, thank you!
Also, this book is helpful if you want to know more about gnuplot.
Gnuplot in Action: Understanding Data with Graphs

1.    Basics:

In the Linux command prompt, type gnuplot to start, type quit or exit to exit.

1.1. Drawing single curve

1.1.1. 
gnuplot> plot sin(x) #all options are the default values.

1.1.2. 
gnuplot> plot sin(x) with line linetype 3 linewidth 2
OR
gnuplot> plot sin(x) w l lt 3 lw 2 #using line to draw, line types (including types of color and dotted line) is 3, the line width is 2, drawing function sin (x)

1.1.3. 
gnuplot> plot sin(x) with point pointtype 3 pointsize 2
OR
gnuplot> plot sin(x) w p pt 3 ps 2 #using point to draw, point type (including color and point type) is 3, the point size is 2, drawing function sin (x)

1.1.4. 
gnuplot> plot sin (x) title 'f (x)' w lp lt 3 lw 2 pt 3 ps 2 #using both point and line to draw, the title 'f (x)' is superscript if ignore then use the default options

1.1.5. 
gnuplot> plot 'a.dat' u 2:3 wl lt 3 lw 2 #using the second and third columns of data file a.dat to draw
By the way, from 1.1.2 and 1.1.3, in gnuplot, commands have short form such as with can be replaced by w, pointtype can be replaced by pt.

1.2. Drawing multiple curves

gnuplot> plot sin(x) title 'sin (x)' wl lt 1 lw 2, cos(x) title 'cos(x)' wl lt 2 lw 2 #two curves are separated by a comma. Draw a number of curves, each curve are separated by commas.

In the example above is drawing a function, if drawing data file, just replace the function name to the data file name, but use single quotation marks like ‘a.dat’.

1.3. Location of the legend

The default location is on the upper right.

gnuplot> set key left #on the left, there are two options left and right
gnuplot> set key bottom #on the bottom, the only option; default is on the top
gnuplot> set key outside #make it outside, but only can set it outside of the right

These three options can be combined. Such as:
gnuplot> set key left bottom #means left bottom

You can also using coordinates to represent the location of the legend accurately, such as:
gnuplot> set key 0.5,0.6 #will position the legend on the 0.5,0.6

1.4. About the axis

gnuplot> set xlabel 'x' #x-axis labeled 'x'
gnuplot> set ylabel 'y' #y-axis labeled as 'y'
gnuplot> set ylabel 'DOS' tc lt 3 #tc lt 3 means the third color of 'DOS'
gnuplot> set xtics 1.0 #set the width of the main scale of x-axis to 1.0, the same as the y-axis can be defined by ytics
gnuplot> set mxtics 3 #separate main scale of x-axis into 3 parts , the same as the y-axis can be defined mytics
gnuplot> set border 3 lt 3 lw 2 #is set the border to the third type, color type is 3, line width is 2

The same things can be done to the top x-axis (called x2) and the right of the y-axis (called y2) axis, using x2tics, mx2tics, y2tics, my2tics

gnuplot> set xtics nomirror
gnuplot> unset x2tics #above two commands to remove the scale on the x2
gnuplot> set ytics nomirror
gnuplot> unset y2tics #above two commands to remove the scale on the y2

1.5. Insert text in the figure

gnuplot> set label 'sin (x)' at 0.5,0.5 #in the coordinates (0.5,0.5) adding the string 'sin (x)'

When export figures as .ps or .eps file, if add enhanced in set term statement, you can insert the upper and lower standard, the Greek letters and special symbols. Upper and lower insert is same as Latex

1.6. Adding lines and arrows

gnuplot> set arrow from 0.0,0.0 to 0.6,0.8 #from (0.0,0.0) to (0.6,0.8) draw an arrow
gnuplot> set arrow from 0.0,0.0 to 0.6,0.8 lt 3 lw 2 #color of this arrow type is 3, the line width of type 2
gnuplot> set arrow from 0.0,0.0 to 0.6,0.8 nohead lt 3 lw 2 #nohead can remove the head of the arrow, which is the way to add a straight line

Note that in gnuplot, after insert multiple label or arrow, the system will default according to the order of each label or arrow, respectively, starting from 1. If you later want to remove a label or arrow, as long as corresponding with the unset command can be removed. Such as:
gnuplot> unset arrow 2 #to remove the second arrow

1.7. Size and location of the figure

gnuplot> set size 0.5,0.5 #the length and width are half of the default width recommended to use this value, especially export as .ps or .eps
gnuplot> set origin 0.0,0.5 #set the location of the origin. This figure will appear in the upper left corner

1.8. Three-dimensional graph drawing

gnuplot> splot 'file name' u 2:4:5 #in the second and fourth column as x and y coordinates, and the fifth as z coordinates

1.9. Export the figure to file

In gnuplot, the format of the output file is controlled by the terminal. By default, all outputs send to the screen, that is, the terminal model X11. If you want to output to a file, you must set the terminal. To learn terminal type, you can type set
terminal, all supported the terminal mode (or file format) are listed. For me, always using .ps and .eps files format.
So we can run the following two commands before using plot or splot to draw:

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

The first command is to set the terminal mode, post is the postscript model, which is to output to ps or eps files. The following eps, color, solid and
enh options can be ignored, depending on your needs. eps means output a eps files, default is output a ps file; color means to output color figure, default is black and white; solid means all the output lines in the figure a solid line, default is only the first line are solid, others are different dotted line; use enh (enhanced) option can inserted subscript, Greek letters and special symbols into figure.

The second command is the output file name. Note, however, run these two commands is just define the output file name, in fact, figure still not be drawn to this file yet. Then run the following command:

gnuplot> plot sin (x) wl
Or
gnuplot> replot (if you have ran plot or replot command before)

At this time, the graphics will in the specified file above.
To export to other formats, the same should be set, such as jpg format, running the following command before drawing command:

gnuplot> set term jpeg
gnuplot> set output 'a.jpg'

If you want to go back to the other modes by the output to the screen, then run the following command:

gnuplot> set term X11





Gnuplot Tips (2)

GNUPLOT Tips (2)

How to draw the same graph in the same number of figure

gnuplot> set multiplot #is set to multi-graph mode
gnuplot> set origin 0.0,0.5 #set the first figure the location of the origin
gnuplot> set size 0.5,0.5 #set the first figure of the size of the
gnuplot> plot "a1.dat"
gnuplot> set origin 0.5,0.5 #set the second figure the location of the origin
gnuplot> set size 0.5,0.5 #set the size of the second graph
gnuplot> plot "a2.dat"
gnuplot> set origin 0.0,0.0 #set the origin of the third location figure
gnuplot> set size 0.5,0.5 #set the size of the third graph
gnuplot> plot "a3.dat"
gnuplot> set origin 0.5,0.0 #set the fourth position of the origin figure
gnuplot> set size 0.5,0.5 #set the size of the fourth figure
gnuplot> plot "a4.dat"

Of course, if a graph after a quantity of the same set and the previous one, then after a set amount of this can be omitted. For example, on the face of the second, third and fourth the size of the set plans. The previous figure set on a volume will play a role in the latter figure.If you want to cancel the role of the figure in the back, must use the following commands, such as canceling the label, use
gnuplot> unset label

For two-dimensional graphs, how to make both sides of the axis length of the unit length

gnuplot> set size square #the graph is square
gnuplot> set size 0.5,0.5 #the graph is the size you want
gnuplot> set xrange [-a: a]
gnuplot> set yrange [-a: a] #same as the two axes scale range
gnuplot> plot 'a.dat'

How to use the same graph in the left and right sides of the y-axis are drawing

gnuplot> set xtics nomirror #remove the top of the scale axis x2
gnuplot> set ytics nomirror #to remove the right side of the scale axis y2
gnuplot> set x2tics #above the axis to scale automatically generated x2
gnuplot> set y2tics #to the right side of the scale of the axes automatically generated y2
gnuplot> plot sin (x), cos (x) axes x1y2 #cos (x) with x1y2 coordinates, axes x1y2 x1y2 said axis with
gnuplot> plot sin (x), cos (x) axes x2y2 #cos (x) with x2y2 coordinates, axes x2y2 x2y2 said axis with
gnuplot> set x2range [-20:20] #set the range of coordinates x2
gnuplot> replot
gnuplot> set xrange [-5:5] #set the x coordinate of the range
gnuplot> replot
gnuplot> set xlabel 'x'
gnuplot> set x2label 't'
gnuplot> set ylabel 'y'
gnuplot> set y2label 's'
gnuplot> replot
gnuplot> set title 'The figure'
gnuplot> replot
gnuplot> set x2label 't' textcolor lt 3 #textcolor lt 3 or tc lt 3 set the color of the axis name

How to insert Greek letters and special symbols

Generally only in the ps and eps figure, and must specify the enhanced option. In the X11 terminal (ie display) can not be displayed.

gnuplot> set terminal postscript enhanced and Greek letters can, through {/ Symbol a} input. For example,
gnuplot> set label '{/ Symbol a}'

Greek letters and special symbols of various input methods please see the installation package in gnuplot-4.0.0/docs/psdoc directory ps_guide.ps file.
Another See also:
http://t16web.lanl.gov/Kawano/gnuplot/label-e.html # 4.3

Gnuplot how to insert the Angstrom (Egypt) The symbol (A top of a small circle)

Before you insert the script by adding
gnuplot> set encoding iso_8859_1
This command, and then can use the "{\ 305}" added. Abscissa should be labeled as "k (1 /?)":
gnuplot> set xlabel 'k (1 / {\ 305})
If multiplot mode, this command must be placed in
gnuplot> set multiplot
Front.
If the latter have to escape into other characters, but also added after the character in the insert the following command:
set encoding default
Installation package gnuplot-4.0.0/docs/psdoc/ps_guide.ps file table 'E' on behalf of all the symbols in that column are entered using this method.

Gnuplot drawing contour figures

gnuplot> splot 'file name. dat' u 1:2:3 wl #do three-dimensional figure
gnuplot> set dgrid3d 100,100 #three-dimensional figure the surface of the grid to set the number of
gnuplot> replot
gnuplot> set contour #draw contour set
gnuplot> set cntrparam levels incremental -0.2,0.01,0.2 #set spacing of contour lines and scope of the data at intervals from -0.2 to 0.2 0.01 to draw a line between
gnuplot> unset surface to remove the above three-dimensional graphics
Drop the mouse end graphics, you can choose the right angle. Or directly setting (0,0) perspective can also:
gnuplot> set view 0,0
gnuplot> replot

Note here, drawing three-dimensional figure data file to be partitioned, that is, transform a value for each x, y within the range of its change in a week, so as a piece, and then take an x value, y and then changes a week, as the next a block of data, and so on. Between the blocks to open with a blank line grid.

How to draw beautiful pm3d figure

gnuplot> set pm3d #set pm3d mode
gnuplot> set isosamples 50,50 #set grid
gnuplot> splot x ** 2 + y ** 2 #three-dimensional graph drawing
gnuplot> splot x ** 2 + y ** 2 w pm3d #drawn pm3d mode, note that comparing changes
gnuplot> set view 0,0 #set the view, (0,0) to the surface of the projector in the end
gnuplot> splot x ** 2 + y ** 2 w pm3d #redrawn, note the changes
gnuplot> unset ztics #figures to remove the z axis
gnuplot> set isosamples 200,200 #thinner to the grid
gnuplot> replot #redrawn, note the changes, mainly a more smooth transition

Using a script file to avoid duplication of input

Sometimes a data file of a good figure, the next may have to use this data file to draw, but one or some settings to make some subtle changes. At this time, the first time can be plotted all the commands written to a file, such as a.plt, the next as long as the appropriate setting to do modifications, with the following command will automatically run the file all the commands and the last received you want the figure:

gnuplot> load 'a.plt'

As an example, suppose the file name a.plt, the contents of which are:

set pm3d
set view 0,0
unset ztics
set isosamples 200,200
splot x ** 2 + y ** 2 w pm3d
set term post color
set output 'a.ps'
replot

So start gnuplot, simply run the following command on it:

gnuplot> load 'a.plt'

If we want only the. Ps or. Eps figure, it can also be directly in linux command prompt, run the following command:

[Abc @ Abc] $ gnuplot a.plt

In the linux command gnuplot mode

In the gnuplot prompt can run linux commands, but must be preceded by the corresponding command! Number. For example, suppose a lot of parameters have been set, but need to modify a data file a.dat after drawing, you can use as follows:

gnuplot>! vi a.dat

In this way, all the linux commands can be run in gnuplot environment.
Alternatively, you can enter in the gnuplot prompt shell, temporary exit gnuplot, into the linux environment, done things to do, run the exit command, and went back to gnuplot environment.

gnuplot> shell
[Abc @ Abc] $ vi af
[Abc @ Abc] $ f77 af
[Abc @ Abc] $ a.out (assuming the data file generated a.dat)
[Abc @ Abc] $ exit

gnuplot> plot 'a.dat' wl