gnuplot
- Details
- Last Updated: Monday, 12 October 2020 01:28
- Published: Friday, 21 September 2018 18:10
- Hits: 1007
gnuplot: It is an open source software to plot graphs of any function. It's one of the oldest and easiest plotting software to use. Although it has name gnu in it, it's not associated with GNU project. It doesn't have GNU license, it has it's own license. The source code is copyrighted, but freely distributed.
Official website is: http://gnuplot.info/
Very good documentation is available here in this pdf file, which is also distributed with the software: http://gnuplot.info/docs_5.0/gnuplot.pdf
This is a very large doc of 250 pages. Probably 100 or so pages are relevant. A concise tutorial is here: http://people.duke.edu/~hpgavin/gnuplot.html
Another one here: http://hirophysics.com/gnuplot/gnuplot.html
gnuplot can plot in 2D and 3D, and can save plots in many popular formats as jpeg, png, pdf, etc.
Installation:
Install gnuplot on CentOS by typing below cmd on terminal:
sudo yum install gnuplot => Once done, gnuplot executable should reside in /usr/bin/gnuplot
To see version of gnuplot, type on cmd terminal:
gnuplot --version => On my system, it shows "gnuplot 4.6 patchlevel 2". This is gnuplot 4, while doc above is for gnuplot 5. So, there may be some differences.
Alternatives to Gnuplot:
I've always used gnuplot in past, just because I found it installed on my Linux systems at work. It's very easy to use, and easy to install. Works flawlessly.
However, now there are many more plotting software available. Within python, you have multiple libraries that you can import to do 2D/3D plots.Some of the popular ones are:
1. pygnuplot: Within python, there is python gnuplot (calles as pygnuplot). We can use this to import gnuplot in python, and then use all the capabilities of gnuplot inside of python.
Offcial website: https://pypi.org/project/py-gnuplot/
2. matplotlib: matplotlib is another library in python, that can draw complex 2D/3D plots. It's based on matplot, and most of the cmds are similar. See in python matplotlib section.
syntax:
- gnuplot is case sensitive.
- All command names may be abbreviated as long as the abbreviation is not ambiguous.
- Any number of commands may appear on a line, separated by semicolons(;).
- Strings may be set off by either single or double quotes, although there are some subtle differences.
- # anywhere in the line indicates comment, and rest of the line is ignored
- Commands may extend over several input lines by ending each line but the last with a backslash "\".
comands:
type gnuplot on terminal to bring up gnuplot shell, where you can type gnuplot cmds. It shows up as "gnplot >" on the screen. This is interactive session. If you want gnuplot to run on a file in which you already have all gunplot cmds, you can use a "batch" session by typing "gnuplot input1.txt" where input1.txt has all gnuplot cmds.
There are a lot of cmds that are gnuplot specific that you can type to plot graphs, set var, make loops (for), conditional stmt (if-else), etc. Many of linux cmds (such as cd, history, clear, etc) are also supported in gnuplot. Part III of gnuplot doc (page 66 onwards) details these cmds, Following are the imp cmds you can use for basic working:
1. various output formats:
gnuplot supports many different graphics devices. Use set terminal to tell gnuplot what kind of output to generate. Gnuplot supports a large number of output formats (jpeg, gif, pdf, X11, windows, etc). These are selected by choosing the appropriate terminal type, possibly with additional modifying options. See page 193 of gnuplot pdf doc above.
The gui box where all plots are drawn is called the canvas. Plot is drawn on this canvas, and can be chosen to be of any size.
set term <terminal_type> size <XX>, <YY> => This specifies the o/p type and size of canvas
set size <XX> <YY> => scales the plot itself relative to the size of the canvas. By default, plot will fill the entire canvas. Scale values less than 1 will cause the plot to not fill the entire canvas.
Typing "set term" shows all available terminals for particular installed version of gnuplot. "show term" shows current terminal. By default, it shows x11 terminal. x11 is X11 windowing system for bitmap displays. More info in "X window system" section. So, the canvas that you see is in same X windows format as your desktop gui.
ex: set terminal jpeg => This generates output in jpeg format. PNG, JPEG and GIF images are created using the external library libgd. In most cases, PNG is to be preferred for single plots, and GIF for animations. Both are loss-less image formats, and produce better image quality than the lossy JPEG format.
NOTE: once we set terminal to something other than x11 (lower case x), then the X11 window no longer shows or updates, since the terminal is set to "jpeg" or whatever we set it to. In order to see plots interactively, we have to set term back to x11 by running cmd "set term x11".
Use set output to redirect that output to a file or device. If we don't do this, then o/p is generated in that format and by default, printed on gnuplot, which is basically garbage. only x11 o/p is what can be displayed on gnuplot. All other formats will need to be redirected to some file. Then we can open that file with a image reader for that format.
ex: Following 3 cmds typed on gnuplot sets the o/p type to "png" and directs the o/p to file "test.png". Now the 1st plot that we do following the "set output" cmd will be saved to "test.png". For any subsequent plots to be saved, we have to again do "set output" cmd with appr file name.
gnuplot > set terminal png size 600,400
gnuplot > set size 0.5,0.5
gnuplot > set output "test.png"
gnuplot > plot 3*x+2 #only this plot will be saved to test.png.
NOTE: by default, we don't need to use any of the above cmds in interactive mode, since we want to see the plots interactively. Only once we are done with interactive plotting, we can go ahead and save those plots by using cmds above (may be in batch session)
3. set => to assign values to variables which are predefined. We need to set few settings, before we plot 2D or 3D plots.
title: ex: gnuplot> set title "Some Math Functions" => This sets title on top of graph
output: ex: set output "my_fig.png" => This saves the plot with given name
gridlines: This is to set gridlines on the plot, so that it's easy to see the points
set ytics 10 # y scale marks and values will be at every 10th unit, So, on y axis, we'll see 10, 20, 30 and so on, and there will be a horizontal grid line at each of these
set xtics 2 # x scale marks and values will be at every 2nd unit, So, on y axis, we'll see 0, 2, 4 and so on, and there will be a vertical grid line at each of these
Now, we run "set grid" and above xtics and ytics will go into effect.
However once we have defined xtics and ytics, we can use below lines to further customize grid lines.
set grid ytics lt 1 lw 2 lc rgb "#bbbbbb"
set grid xtics lt 1 lw 2 lc rgb "#bbbbbb"
Where:
lt
means line type (0 for dashed line, 1 for solid line)
lw
means line width
lc
means line color
These settings can be in any order. So, for example, The following plot settings:
set grid ytics lc rgb "#bbbbbb" lw 1 lt 0
set grid xtics lc rgb "#bbbbbb" lw 1 lt 0
ex: following cmds customize plot and then we plot the graph
set title "Some Math Functions"
set xrange [-10:10] => sets range of x on X axis
set yrange [-2:2] => sets range of y on Y axis
set xtics 2; set ytics 10; set grid => If we have already plotted graphs before w/o the grid, we can just type replot. then all current plots will be replotted with grid.
2. plot/splot => plot/splot is the most used cmd. There are 4 plotting cmds:
- plot: to plot anything in 2D (most imp cmd)
- ex: gnuplot> plot (sin(2*x)/sin(x)) => plots graph of function given vs "x". x is std variable. If we use any var other than x, then it errors out.
- splot: to plot 3D. splot means "surface plot" which sows the surface of plot.
- ex: gnuplot> splot sin(x)*cos(y) => plots graph of func in z dir wih x and y as var on the 2 axis. Here, "x" and "y" are std var. Using any var other than x,y errors out.
- replot, refresh: These replot previous plot/splot cmds.
2D plots using plot: There are various settings that can be used to control how to show our 2D plot.
set samples 10000 => this is used to increase resolution by inc # of samples (default is 100). This is needed to make 2D plots smooth, but it does take longer to plot.
plot log10(abs(sin(10*x)/sin(x))) => plots bode plot (log(x) plots log with base e). Note: No parenthesis around plot cmd, as it's optional
Multiplot graphs on same plot by specifying more than one plot:
gnuplot > plot 3*x+5, x**2+8, 1/x + 8*x => parenthesis are optional, multiple graphs separated by comma
We can also define a function, so that we can plot it easily w/o rewriting it everytime. We can specify x range of plot, and y range is chosen automatically to fit the graph.
gnuplot> f(x) = exp(-x**2 / 2) => we can also define func with more var, i.e, f(x,a,b)=1/(1+a*exp(-(b*x))), then plot f(x,1,2)
gnuplot> plot [t=-4:4] f(t), t**2 / 16 => NOTE: we changed the var from x to t. We are plotting 2 graphs here with t ranging from -4 to +4
3D plots using splot: There are various settings that can be used to control how to show our 3D plot. Below settings are recommended to be set before using splot cmd, as they make 3D plots more meaningful.
gnuplot> set hidden3d #This causes the surface to be opaque. It's hard to look at 3D plots with transparent surface, so we make it opaque.
gnuplot> set pm3d #this makes 3D palette mapped, i.e diferent values on z-axis are shown with different colors, so it's easy to see how 3D plot values are changing with change in x,y.
gnuplot> set contour both #this options draws contour lines on base (i.e X,Y plane) and on surface too. contour lines are lines where z values are same across any x,y. So, another easy way to see how z values are changing
gnuplot> set isosamples 100 #this defines the sampling rate (similar to sampling rate in 2D using samples), i.e how often x,y are sampled. By default, they are sampled every 10 isolines per u,v axis, which produces very crude 3D surfaces. setting it to 100 makes these 3D graphs very smooth, though it will take longer to plot 3D graphs with higher setting of isosamples.
gnuplot> splot 1/(1+exp(-(x+y))) #Now, after setting above 4 settings, we can plot this 3D plot of sigmoid function.
gnuplot> f1(x,y,a,b)=1/(1+exp(-(a*x+b*y))) => This defines func f1 in var x,y,a,b.
gnuplot> splot 20*f1(x,y,1,100) + 4*f1(x,y,2,1) => Here we plot func sum of f1 with different constant a,b. Resulting plot is 3D plot in terms of x,y
4. plot from user provided file: Both 2D/3D plots can be plotted via user provided file.
plot "abc.data" => plots data from abc.data, where abc.data single column file, with each row containing data corresponding to that line number (the line number becomes "x" on X axis).
However, if we have multiple cols, and we want to plot data for all cols, we specify col number. col 0 is psuedo col that translates to line number (or sample number), col 1 is 1st col of your file and so on
plot "abc.data" using 2 => plots graph with line number on x axis and col values for 2nd col on y axis
If we want to plot multiple cols on same plot, we can repeat above cmd multiple times but that will create multiple plots, each being a plot for only 1 col. To resolve that, we can do this:
plot "abc.data" using 2, "" using 3, "" using 4 => this says plot the same file on same plot using col=2,3,4
We can also use a for loop to be efficient (usually for files having lot of cols)
plot for [col=1:4] "abc.data" using col => plots for all col 1 thru 4
To have lines instead of points, we can do
plot for [col=1:4] "abc.data" using col with lines
plot for [col=2:3] "retire.data" using 0:col with lines