I know, people think that Gnuplot is back-dated and can't be used for producing publication-quality images. However, I have a different opinion. Without underestimating other plotting software, I can say that the Gnuplot is as able as any other plotting software to produce high-quality images. It all depends on how well you know this classic package. Many of the images that you find on this website are plotted in Gnuplot!
Below, you may find some helpful snippets.
Gnuplot has the inbuilt capability to write any equation in Latex format. What it does is, it divides the image into two parts, in one part, it plots the required image/curve (in filename.ps format), and in the other part, it encodes the texts/titles/labels, etc. in filename.tex format. Once these two files are produced, you can go to the main latex page where the image is to be inserted and insert the gnuplot produced latex like adding some graphics (examples are given here). After you compile the main latex, you will see that the curve has been put with latex-type equations written on it.
To produce images in Gnuplot with a latex environment (which can write texts in latex format), follow the rules -
1) Set the terminal type to epslatex standalone -
set term epslatex color
set output "filename.tex"
2) Then add all the plot commands that one requires. Give a plot command to plot something or load a file that contains all the plotting commands (example: plot_filename.gn ). You can use the latex format while writing the text/ title etc., just put a double backslash ("\\") in front of the commands (like "\\int" instead of "\int" ). One slash is used by the gnuplot to print the next "\" in the output file. Also never forget to put the expressions within the "$$" symbol. Once done, you can just plot the script from the terminal by
gnuplot plot_filename.gn
3) The command should produce a filename "filename.tex". Now you should open the file with any Latex editor and compile. Once compiled successfully, you should get the final figure with all the latex symbols and equations that you wanted.
Example of an image from Gnuplot using the epslatex terminal
Code to plot the above image
#!/usr/bin/gnuplot
reset
set term epslatex standalone color font ",10"
set output "latex-test2.tex"
set macros
set multiplot layout 2,2
LMARGIN = "set lmargin at screen 0.15; set rmargin at screen 0.55;" # divide it equally
RMARGIN = "set lmargin at screen 0.55; set rmargin at screen 0.95;"
BMARGIN = "set bmargin at screen 0.15; set tmargin at screen 0.55;"
TMARGIN = "set bmargin at screen 0.55; set tmargin at screen 0.95;"
set xr[-10:10]
set yr [-2:2]
set xtics -10,5,10
set ytics -2,1,2
set mxtics 5
set mytics 5
set tics scale 1.5,1
# plot no 1,1
@LMARGIN; @TMARGIN;
set ytics format "$%1.1f$"
set xtics format ""
set ylabel "This is the $y$-axis" offset character 0,-5
plot sin(x+pi) with line linewidth 2 linecolor rgb "blue" title "$\\sin(x+\\pi)$",\
x/(sqrt(4+x*x)) with line linewidth 2 linecolor rgb "cyan" title "$\\frac{x}{\\sqrt{4+x^2}}$"
# plot no 1,2
@RMARGIN; @TMARGIN;
set ytics format ""
set ylabel ""
plot sin(x+pi) with line linewidth 2 linecolor rgb "blue" title "$\\sin(x+\\pi)$",\
x/(sqrt(4+x*x)) with line linewidth 2 linecolor rgb "cyan" title "$\\frac{x}{\\sqrt{4+x^2}}$"
# plot no 2,1
@LMARGIN; @BMARGIN;
set ytics format "$%1.1f$"
set xtics format "$%1.1f$"
set ytics -2.0,1.0,1.8
set xtics -10,5,9
plot sin(x+pi) with line linewidth 2 linecolor rgb "blue" title "$\\sin(x+\\pi)$",\
x/(sqrt(4+x*x)) with line linewidth 2 linecolor rgb "cyan" title "$\\frac{x}{\\sqrt{4+x^2}}$"
# Ornaments go here
set label 1 "M$_\\odot$ and $\\dot{M}_{out}$" at screen 0.97,0.4 rotate by 90
# Ornaments ends
# plot no 2,2
@RMARGIN; @BMARGIN;
set ytics format ""
set xtics -10,5,10
set xlabel "This is $x$-axis" offset character -10,0
plot sin(x+pi) with line linewidth 2 linecolor rgb "blue" title "$\\sin(x+\\pi)$",\
x/(sqrt(4+x*x)) with line linewidth 2 linecolor rgb "cyan" title "$\\frac{x}{\\sqrt{4+x^2}}$"
unset multiplot
unset output
unset multiplot
unset output
Gnuplot can plot 4D plots i.e. with usual 3D plotting, it can provide an independent color axis representing another dimension. For this to work, you have to have data files printed in a special way. Suppose you have 2D grid of coordinates (x1, x2, x3, ...) , (y1, y2, y3, ...), and a z-axis that is a function of x and y. Let us assume that the functional values in those points are (g00,g11,g10, ..), i.e. the data is arranged like
x y z variable
x0 y0 f(x0,y0) g00
x0 y1 f(x0,y1) g01
x0 y2 f(x0,y2) g02
x1 y0 f(x1,y0) g10
x1 y1 f(x1,y1) g11
x1 y2 f(x1,y2) g12
x2 y0 f(x2,y0) g20
x2 y1 f(x2,y1) g21
x2 y2 f(x2,y2) g22
Here, f(x,y) is the z-coordinate. It is better if this coordinate is unique. In the case of multiple values of z, the figure becomes too crowded. The gxy values you can put by hand or generated by a code. (Note that, this single-line spacing is very crucial for such plots). Now plot the data file using the command
set pm3d
sp "filename.dat" u 1:2:3:4 w pm3d .
Gnuplot has the capability to convert a set of images into a .avi movie file. For that, all you need is a set of images.
If you have a set of data files with orderly names, you can run a for loop over the data files to produce a set of high-quality images. Here is an example -
Suppose, you want to evolve a sin curve with different phases, you do
set term png size 1600,1200
do for [t=0:9]{
set output "image.0".t.".png"
p [0:6.28] sin(x+t*3.14/50.0) w l ti 't='.t.' second (say)'
}
This will create a set of .png files of form image.nn.png .
Now from the terminal, type
ffmpeg -f image2 -r 10.0 -i sin.%02d.png -pix_fmt yuv420p -vf pad="width=ceil(iw/2)*2:height=ceil(ih/2)*2" -qscale 1 out.mp4
where, the number after -r represents the frame rate per second, "sin.%02d.png" is the image filename and out.mp4 is the output video file. The syntax image.%02d.png specifies to use of a decimal number composed of three digits padded with zeroes to express the sequence number. It is the same syntax supported by the C printf function, but only formats accepting a normal integer are suitable. The option "-qscale" sets the quality of the video, this can take any value from 1(best) to 31(worst). The argument after -vf is to make sure that the pixel number of the image is divisable by 2. This is a technical requirement.
For more information on using ffmpeg, go to http://www.ffmpeg.org/ffmpeg.html. A more complicated but useful file with selecting filenames in a broad range can be found here.
Gnuplot has extended capability to write the mathematical symbols in enhanced postscript (.eps) files. You just have to write "{/Symbol a}" to get "α" or "{/Symbol r}" to get "ρ".
Apart from these simple symbols, Gnuplot can even write extensive mathematics symbols in postscript format. For that, all you need to have is the font files saved somewhere in your computer and add the filename/fontpath in your gnuplot script.
The font files can downloaded from this link of CTAN site. Download The whole package if you want . Untar it (tar -xvzf filename.tar.bz for eaxample) and it's done. Now, while specifying the terminal as postscript, add this line
set term postscript color enhanced fontfile add "/path to the folder amsfonts/pfb/style_file.pfb"
where this style_file.pfb is the file of your choice. A helpful document showing the use of the style files can be found in this document.
A simple example is test.gn and its output test.eps.