Introduction to Scientific Computing

PHYS 27/193

Physics Department
University of the Pacific

3-D Plots with Gnuplot

You can plot many interesting mathematical functions f(x) with gnuplot such as you have been doing in the previous exercises.
Another powerful feature of gnuplot is the ability to plot functions of two variables as a surface z = f(x,y). There are some examples of what gnuplot can do at the top of this page.

To see what I mean, start gnuplot and type the following at the prompt:

gnuplot> splot x**2 + y**2

This will produce a simple rectangular parabolic "bowl" shaped surface (hence the command splot), which represents the function z = x2+y2.
Here we used splot (surface plot) instead of the previous plot command which is reserved for 2-d f(x) plots.

You should be able to "grab" the splot with your mouse, click and hold down the mouse button, and move the mouse.
This should rotate the graph so that you can see it from different angles, allowing you to explore the 3-dimensionality of the surface.

Mac Users:
It seems that the mouse interface of the Mac OSX version does not support this feature at the moment. Mac users can change the view by changing the view angles These are two variables which represent the angle of the viewer. The defaults are rot_x = 60 and rot_z = 30. This means that the viewer is looking from an angle rotated by 60 degrees about the x axis (rot_x), and rotated 30 about the z axis (rot_z).

You can see what the view angles are by typing:

gnuplot> show view

       view is 60 rot_x, 30 rot_z, 1 scale, 1 scale_z
If you want to change the view, say by rotating the plot counter clockwise as viewed from above (the z axis), you can type:
gnuplot> set view 60,20
gnuplot> replot
then do:
gnuplot> set view 60,10
gnuplot> replot
to rotate it again.
This will set the view to only 20 degrees rotation about z, then 10 degrees. Try it and see what happens.


Try another plot:

gnuplot> splot [0:pi][0:pi] sin(x*y)

You can see the interesting shape (can you grab it and look at it from different angles?).
Notice the plot ranges [0:pi] for both x and y.
(Remember that gnuplot recognizes the word "pi")

If you wanted to fix the plot ranges, so that you didn't have to keep typing them, you could type:

gnuplot> set xrange [0:pi]
gnuplot> set yrange [0:pi]
then do:
gnuplot> splot sin(x*y)
This will set the default ranges to [0:pi] (of course you could set any ranges you want. Sometimes this is a time saver.


Let's try to make the plot better.

First, let's make the surface non-transparent. Type this:

gnuplot> set hidden3d
gnuplot> replot

Now the surface is opaque, and the underside is a different color. Notice too that replot recalls the last plot.


Still the surface is a litle clunky--it's made of large plane "panels".
If these were smaller, the surface would be more smooth. Type:

gnuplot> set isosamples 20
gnuplot> replot

Much better. You could also have done set isosamples 35 or any other number.
The isosamples are the number of "samples" in each direction (x and y).

Note that if you are doing a 2-D plot, you would set samples to increase the sampling resolution,
not isosamples which are only for 3-D splots. That is:

2-Dplotset samples n
3-Dsplotset isosamples n


If you ever want to see what value a variable is currently set to, do

gnuplot> show isosamples
        iso sampling rate is 20, 20
(notice that you could set the isosample in x and y separately. If you give only one number, both x and y are set to the same value.)

It is generally true for any variable, that you can see what it's value is by typing show var
where var is something with a value like isosamples.


You can see all the variables (and classes of variables) that you are able to set by typing just show

gnuplot> show 
             ^
         valid set options:  [] = choose one, {} means optional

        'all', 'angles', 'arrow', 'autoscale', 'bars', 'border', 'boxwidth', 'clip',
        'cntrparam', 'colorbox', 'contour', 'datafile', 'decimalsign','dgrid3d',
        'dummy', 'encoding', 'fit', 'fontpath', 'format', 'functions', 'grid',
        'hidden', 'isosamples', 'key', 'label', 'loadpath', 'locale', 'logscale',
        'mapping', 'margin', 'offsets', 'origin', 'output', 'plot',
        'palette', 'parametric', 'pm3d', 'pointsize', 'polar', 'print', '[rtuv]range',
        'samples', 'size', 'style', 'terminal', 'tics', 'timestamp',
        'timefmt', 'title', 'variables', 'version', 'view',
        '[xyz,cb]{2}label', '[xyz,cb]{2}range', '{m}[xyz,cb]{2}tics',
        '[xyz,cb]{2}[md]tics', '{[xyz]{2}}zeroaxis', '[xyz,cb]data', 'zero'

This confuses gnuplot into asking you which variable you want to show, and it shows you all the options. You can also do: help set, followed by one of the options listed.

If you want, you can type show all which dumps everything.


We won't be doing anything with splot for a while, but I wanted to introduce you to it since it produces some cool plots.

For a bit more eye candy, have a look at some of the plots on the Surfaces and 3-D Plots on the Demo page on the Gunplot site:

Gnuplot Demos

You can make any of these plots with Gnuplot on your computer now! (you may need the data file from the Gnuplot site to reproduce some of them).

A really nice feature of the demo page is that the commands to produce the plots is also listed. Unfortunately, some of the plots require data files, and those are not so easy to get (I have them though, if you are really interested in reproducing one of the plots). Some of the plots use advanced features that we won't cover in this course, however you can always learn about these from the online documentation.


One more fun thing to do. Start gnuplot, type:

gnuplot> set isosamples 30
gnuplot> set hidden3d
gnuplot> set contour
gnuplot> splot exp(-(x**2+y**2)/50)*sin(x/3)*y**2

Do you see what happened? You produced "contour" lines on the "floor" of the plot. This can be useful when your surface is complicated and you want to understand its structure. The contour plot is like a "topo" map that you may have used when hiking.

If you are alert, you will notice that even though the surface is quite bumpy, there is a particular straight path which is at constant "height". This would be very useful to know if you were a Civil Engineer building a road through this terrain.

If you just want the contour map, you can do this:

gnuplot> set size square
gnuplot> unset surface
Then grab the plot and look at it "from the top" (as if you are looking down the z axis.

You can get your surface back by doing

gnuplot> set surface

Next we must learn to plot our own data from a file, which we might have taken from an experiment for example.
Before moving on to plotting data files however, try experimenting with surface plotting of some functions of your own.

Make up some functions, f(x,y), and plot them.

Next: Plotting Data from a File