Original article link
For those who work in Linux, If you often want to draw two-dimensional and simple 3D images, gnuplot is undoubtedly a good choice, not only beautiful graphics, but also easy to operate. Of course, if you need a higher quality 3D graph, please use some other professional drawing software. We recommend that you use gnuplot. This small software is usually provided by RedHat Linux, but its own version is 3.7. We recommend that you upgrade it to 4.0. The new version has many new features. The latest version can be downloaded from the http://www.gnuplot.info.
For some basic operations, please read the instructions. Here, I will summarize some of the problems I encountered during the use process and the solutions to help those who did not or were not skilled in getting started quickly and draw the desired graph, because the original manual is very long, it is difficult to quickly find the information you want. The simple summary here cannot cover all aspects, so do not complain that I have not completed the writing. For a more comprehensive understanding, please read the manual. There are also a lot of online materials. In fact, this is what we searched and found out from 1.1 on the Internet. I believe that after reading this article, the most common questions should be answered here. If you have found some tips and experiences that I have not written during use, or have other suggestions, please let me know and continue to improve this Summary. Thank you!
I. Basics:
Run the gnuplot command in the Linux Command Prompt to start it. Enter quit, Q, or exit to exit.
1. Plot command
Gnuplot> plot sin (x) with line linetype 3 linewidth 2 or
Gnuplot> plot sin (x) w l lt 3 LW 2% is drawn by line. The line type (including the color and dotted line type) is 3, and the line width is 2, graph the sin (x) Function
Gnuplot> plot sin (x) with point pointtype 3 pointsize 2 or
Gnuplot> plot sin (x) w p pt 3 Ps 2% point painting, the point type (including color and point type) is 3, the point size is 2
Gnuplot> plot sin (x) Title 'f (x) 'w lp lt 3 LW 2 PT 3 Ps 2% simultaneous point and Line Painting, Here title 'f (X) 'indicates that the legend is labeled 'f (x)'. If not, use the default option.
Gnuplot> plot sin (x) % all options use the default value. If one item is missing, the default value is used.
Gnuplot> plot 'a. dat 'U w l lt 3 LW 2% plot using the second and third columns in the data file a. dat
By the way, as shown in the first two examples, in gnuplot, if two words are in alphabetical order, the first few letters are the same and the latter are different, you only need to write the first different letter. For example, because there are no other words starting with W, W can be used instead of line or L.
2. Draw multiple curves at the same time
Gnuplot> plot sin (x) Title 'sin (x) 'w l lt 1 LW 2, cos (x) Title 'cos (X) 'W l lt 2 LW 2% two curves are separated by commas. Multiple curves are separated by commas.
In the above example, we plot the function. If we plot the data file, replace the function name with the data file name, but it should be caused by single quotation marks.
3. Legend location
The default position is in the upper right corner.
Place gnuplot> set key left % on the left, with left and right options
Place gnuplot> set key bottom % below. Only this option is available. The default setting is above.
Gnuplot> set key outside % is placed outside, but only on the right.
The preceding three options can be combined. For example:
Gnuplot> set key left bottom % indicates the left bottom edge
You can also use coordinates to accurately represent the legend position, as shown in figure
Gnuplot> set key 0.5, 0.6% place the legend in the positions of 0.5, 0.6
4. Axis
Gnuplot> set xlabel 'X' % x axis marked as 'X'
Gnuplot> set ylabel 'y' % Y axis marked as 'y'
Gnuplot> set ylabel 'dos 'TC lt 3% where TC lt 3 indicates the third color of 'dos.
Gnuplot> set xtics 1.0% X axis master Scale Width is 1.0, you can also define ytics for the Y axis
Gnuplot> set mxtics 3% draws three scores for each master scale on the X axis. You can also define mytics for the Y axis.
Gnuplot> set border 3 lt 3 LW 2% is set as the third boundary, color type is 3, line width is 2
You can also set the X axis (X2) and Y axis (Y2) on the right, that is, x2tics, mx2tics, y2tics, and my2tics.
Gnuplot> set xtics nomirror
Gnuplot> unset x2tics % the above two commands remove the scale of the above X2 axis
Gnuplot> set ytics nomirror
Gnuplot> unset y2tics % the above two commands remove the scale of the right Y axis
5. Insert text into the Graph
Gnuplot> set label 'sin (x) 'at 0.5, 0.5% Add the string 'sin (x)' to the coordinate (0.5, 0.5 )'.
When the output is a. ps or. EPS file, if enhanced is added to the set term statement, the upper and lower mark, Greek letter, and special symbol can be inserted. The method for inserting the upper and lower mark is the same as that in latex.
6. Add a line and an arrow to the graph.
Gnuplot> set arrow from 0.0, 0.0 to 0.6, 0.8% draw an arrow from (0.0, 0.0) to (0.6, 0.8)
Gnuplot> set arrow from 0.0, 0.0 to 0.6, 0.8 lt 3 LW 2% the arrow color type is 3, the line width type is 2
Gnuplot> set arrow from 0.0, 0.0 to 0.6, 0.8 nohead lt 3 LW 2% use nohead to remove the head of the arrow. This is the method for adding a straight line.
Note: In gnuplot, for inserting Multiple labels and arrow, the system will number each label or arrow in sequence by default, starting from 1. If you want to remove a label or arrow in the future, you only need to use the unset command to remove the label or arrow. For example:
Gnuplot> unset Arrow 2
The second arrow is removed.
7. Graph size and position
Gnuplot> set size 0.5, 0.5% long and width are half of the default width. We recommend that you use this value, especially when drawing a PS or EPS image.
Gnuplot> set origin 0.0, 0.5% sets the position of the bottom left corner of the graph in the graphic panel. The figure appears in the upper left corner.
8. Draw 3D images
Gnuplot> splot 'file name 'U % takes the second and fourth columns as the X and Y coordinates, and the fifth column is the zcoordinate.
Ii. improvement:
1. How to draw multiple images at the same time in the same region
Gnuplot> set multiplot % to multi-graph Mode
Gnuplot> set origin 0.0, 0.0% set the origin position of the first graph
Gnuplot> set size 0.5, 0.5% set the size of the first graph
Gnuplot> plot "a1.dat"
Gnuplot> set origin 0.0, 0.5% set the origin position of the second graph
Gnuplot> set size 0.5, 0.5% set the size of the second image
Gnuplot> plot "a2.dat"
Gnuplot> set origin 0.0, 0.0% set the origin position of the Third Graph
Gnuplot> set size 0.5, 0.5% set the size of the third Graph
Gnuplot> plot "a3.dat"
Gnuplot> set origin 0.0, 0.0% set the origin position of the fourth Graph
Gnuplot> set size 0.5, 0.5% set the size of the fourth Graph
Gnuplot> plot "a4.dat"
Of course, if the setting of a quantity in the next graph is the same as that in the previous graph, the setting of this quantity in the next graph can be omitted. For example, you can set the size of the second, third, and fourth graphs. The setting of a certain amount in the previous graph also takes effect in the next graph. If you want to cancel the function in the following figure, you must use the following command, for example, to cancel the label, use
Gnuplot> unset label
2. How to keep the unit length of the coordinate axes on both sides of a two-dimensional graph?
Gnuplot> set size square % make the image Square
Gnuplot> set size 0.5, 0.5% make the image the size you want
Gnuplot> set xrange [-A: A]
Gnuplot> set yrange [-A: A] % the scale range of the two axes is the same
Gnuplot> plot 'a. dat'
3. How to draw images using the Y axes on both sides of the same worker
Gnuplot> set xtics nomirror % remove the scale of the X 2 coordinate axis above
Gnuplot> set ytics nomirror % remove the scale of the right axis Y2
Gnuplot> set x2tics % to automatically generate the X2 scale of the preceding coordinate axis
Gnuplot> set y2tics % to automatically generate the scale of Y2 on the right axis
Gnuplot> plot sin (x), cos (x) axes x1y2 % cos (x) use the x1y2 coordinate, and axes x1y2 represents the x1y2 coordinate axis
Gnuplot> plot sin (x), cos (x) axes x2y2 % cos (x) are in the x2y2 coordinate, and axes x2y2 is in the x2y2 coordinate axis.
Gnuplot> set x2range [-20:20] % to set the range of X2 coordinates
Gnuplot> replot
Gnuplot> set xrange [-5:5] % to set the range of X coordinates
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 coordinate axis name color
4. How to insert Greek letters and special characters
Generally, the enhanced option can only be specified in the PS and EPS charts. Cannot be displayed on the X11 terminal (Display.
Gnuplot> set terminal postscript enhanced
Then the Greek letter can be entered through {/symbol. For example
Gnuplot> set label '{/symbol }'
For the input methods of various Greek letters and special symbols, see the ps_guide.ps file under the gnuplot-4.0.0/docs/psdoc directory in the installation package.
For more information, see:
Http://t16web.lanl.gov/Kawano/gnuplot/label-e.html#4.3
5. How to insert the Angstrom (e) symbol in gnuplot (a small circle above)
In the script, add
Gnuplot> set encoding iso_8859_1
This command can then be added through "{\ 305. If the abscissa is to be labeled as "K (1/second )":
Gnuplot> set xlabel 'K (1/{\ 305 })
In the multiplot mode, the command must be placed in
Gnuplot> set multiplot
.
If you want to insert other escape characters, add the following command after the characters are inserted:
Set encoding default
'E' in the table in the gnuplot-4.0.0/docs/psdoc/ps_guide.ps file in the installation package indicates that all symbols in that column are input using this method.
6. Draw a contour map using gnuplot
Gnuplot> splot 'file name. dat 'U w l % 3D Graph
Gnuplot> set dgrid3d 100,100% set the number of grids on the 3D chart.
Gnuplot> replot
Gnuplot> set contour % set to draw contour lines
Gnuplot> set cntrparam levels incremental-0.2, 0.01, 0.2% set the density and range of the contour lines, and draw a line every 0.2 between-0.2 and 0.01.
Gnuplot> unset surface removes the 3D image above
Finally, drag the image with the mouse and select a reasonable angle. Or you can directly set the angle of view (0, 0:
Gnuplot> set view 0, 0
Gnuplot> replot
Note that the data file for 3D Graph painting must be segmented, that is, each time x changes a value, y changes within the range of change for one week. In this case, take another X value, Y is changed for another week as the next data block, and so on. Use an empty row to split the block.
7. When the output is a PS or EPS graph, the following options are worth special attention.
Gnuplot> set term postscript eps enh solid color
In this example, the EPS option indicates that the output is in the EPS format. if you remove the option, the default PS format is used. The ENH option indicates that the upper and lower mark, Greek letter, and other special characters can be inserted. if you remove the option, the upper and lower mark cannot be inserted; the solid option indicates that all curves in the graph use solid lines, and different dotted lines are used for removal. The color option indicates that all curves in the graph are colored, and black and white are used for removal.
8. How to draw beautiful pm3d Images
Gnuplot> set pm3d % set pm3d Mode
Gnuplot> set isosamples 50, 50% Set grid points
Gnuplot> splot x ** 2 + y ** 2% 3D Graph
Gnuplot> splot x ** 2 + y ** 2 W pm3d % is painted in pm3d mode. Pay attention to the changes.
Gnuplot> set View sets the angle of view, (0, 0) will be projected to the end
Gnuplot> splot x ** 2 + y ** 2 W pm3d % re-painting, pay attention to the changes
Gnuplot> unset ztics % remove the number on the Z axis
Gnuplot> set isosamples 200,200% to fine-tune the grid
Gnuplot> replot % re-painting. Pay attention to the changes, mainly because the transition is smoother.
9. Avoid Repeated input using script files
Sometimes, after creating a chart for a data file, you may need to plot it next time, but some or some settings need to be slightly changed. At this time, you can write all the commands for the first drawing into a file, such as. PLT. After modifying the corresponding settings, the following command will automatically run all the commands in the file and finally get the graph you want:
Gnuplot> load 'a. PLT'
For example, if the file name is A. PLT, the content in the file is:
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
After starting gnuplot, you only need to run the following command:
Gnuplot> load 'a. PLT'
If you want to obtain only the. ps or. EPS graph, you can also directly run the following command at the Linux Command Prompt:
[Zxh @ theory zxh] $ gnuplot A. PLT
10. run Linux commands in gnuplot Mode
You can run the Linux Command at the gnuplot prompt, but you must add it before the corresponding command! . For example, if many parameters have been set, but a data file a. dat needs to be modified before drawing, you can use the following method:
Gnuplot>! Vi a. dat
In this way, all Linux commands can be run in the gnuplot environment.
In addition, you can enter shell at the gnuplot prompt to temporarily exit gnuplot and enter the Linux environment. After you finish the task, run the exit command and return to the gnuplot environment.
Gnuplot> Shell
[Zxh @ theory zxh] $ vi a. f
[Zxh @ theory zxh] $ f77 a. f
[Zxh @ theory zxh] $ A. Out (assuming a. dat data file is generated)
[Zxh @ theory zxh] $ exit
Gnuplot> plot 'a. dat 'W L