Friday, December 8, 2017

GNUplot plotting examples

The following illustrates 2-dimensional plotting of functions of a single variable, f(x), i.e. plotting f(x) versus x:

============= 
#!/gnuplot
#               gnuplotex1.plt batch file   
set xlabel "x"
set ylabel "f(x)"
set title "gnuplotex1"
plot [0:2*pi] x,exp(-x),x*sin(x)
set term png
set output "./gnuplotex1.png"
replot
=============

And the output from this is:


One can see that the three plots are superimposed. The range of the independant variable, x, is set by the expression in square brackets, [0:2*pi] (we could have written [0:2*3.15159] had we so wished).


Next, we plot t he same functions in pseudo-3D, i.e.,
=============
#!/gnuplot
#               gnuplotex2.plt batch file
set xlabel "x"
set ylabel "f(x)"
set title "gnuplotex2"
splot [u=0:2*pi] u,exp(-u),u*sin(u)
set term png
set output "./gnuplotex2.png"
replot
=============


We add a line now, to change the plot completely:
=============
#!/gnuplot
#               gnuplotex3.plt batch file     
set parametric
set xlabel "X-axis"
set ylabel "Y-axis"
set zlabel "Z-axis"
set title "gnuplotex3"
splot [u=0:2*pi] u,exp(-u),u*sin(u)
set term png
set output "./gnuplotex3.png"
replot
=============
Example 2 with "set parameter" added.
The plot has changed dramatically, and is very hard to understand. Here are three views of the same example, this time oriented so as to aid understanding:
=============
#!/gnuplot
#               gnuplotex3a.plt batch file
reset
set term wxt  persist
set parametric
set xlabel "X->u"
set ylabel "Y->exp(-u)"
set zlabel "Z-axis->u*sin(u)"
set title "gnuplotex3a (approximately the Z-X plane)"
set view 90,0
splot [u=0:2*pi] u,exp(-u),u*sin(u)
set term png
set output "./gnuplotex3a.png"
replot
=============
Looking down the "Y" axis.

and
=============
#!/gnuplot
#               gnuplotex3b.plt batch file
reset
set term wxt  persist         
set parametric
set xlabel "X->u"
set ylabel "Y->exp(-u)"
set zlabel "Z-axis->u*sin(u)"
set title "gnuplotex3b (approximately the Z-Y plane)"
set view 90,90
splot [u=0:2*pi] u,exp(-u),u*sin(u)
set term png
set output "./gnuplotex3b.png"
replot
=============
Looking down the "X" axis.
This is a really weird plot, since one axis is not an axis actually, but another function.

Finally
=============
#!/gnuplot
#               gnuplotex3c.plt batch file
reset
set term wxt  persist           
set parametric
set xlabel "X->u"
set ylabel "Y->exp(-u)"
set zlabel "Z-axis->u*sin(u)"
set title "gnuplotex3c (approximately the X-Y plane)"
set view 0,90
splot [u=0:2*pi] u,exp(-u),u*sin(u)
set term png
set output "./gnuplotex3c.png"
replot
=============
Looking down the "Z" axis.

Actually, this last one is "upside-down" in that the vertical axis is reversed from the normal presentation mode.

I would like to actually have the plot rotate using the mouse, but to do this from a batch file seems impossible (see this discussion). What I've had to do is copy the text of the batch file, and then paste it into the terminal which is running gnuplot. Then, and only then, using the mouse one can orient the plot and decide about the angles to be used with the 'set view' command. If anyone can help clarify this, please write me.