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





No comments:

Post a Comment