Previous post covers gnuplot basic 2D plot. This post covers the basics of gnuplot 3D plot.
Most of the 2D plot options apply to 3D plot, one should read the 3D plot post before reading this post.

gnuplot supports splot command for drawing 3D figures and graphs.

There are 3 options specific to 3D plot: isosamples, hidden3d, and surface.

isosamples: controls the number of grid points at which a function will be evaluated when using splot. It is therefore only relevant when plotting functions.

set isosamples {int:xlines} [,{int:ylines}]

hidden3d: use opaque surface rendering. It has the following format:

set hidden3d [offset {int:offset}] [trianglepattern {int:maks}]

  • offset: used to control the color and line type used for the bottom side of the surface.
  • trianglepattern: control which lines will be drawn to connect neighboring grid points.

surface: by default, the surface is shown. It can be switched off by using the command,

unset surface

or switch on with the command,

set surface

An Example

Below is a sample 3D plot script,

unset title

#unset key

#unset xtics

#unset ytics

#set format y "%.1t";

#set format x "%.1t";

set title "Test 3D" 0,1

set xlabel "X" -4,-2;

set ylabel "Y" 0,-2;

set zlabel "Z" 4,6;

#set border 0

set hidden3d offset 0

set surface

#set the view angle: one can adjust manually to get the proper value first

#and then modify the value here

set view 80,49 

#set xrange []

#set yrange []

#set zrange []

#set ticslevel 0

#splot '45050.data' u 1:2:3 with lines

splot '$0' using 1:2:3 title "data1" with linespoints, '$1' using 1:2:3 title "data2" with linespoints

#

set term push 

set term png size 800, 600

set output "$2"

replot

set output

set terminal pop

Suppose the file is saved as 3d.pg. And the two data files “3data.dat” and “3data2.dat” are listed below. Note that the data file must follow certain formats, otherwise the plot may not be expected. gnuplot supports two 3D data file formats, we only covers the grid format here. One can refer to reference 1 for more details.

The grid data format supported by gnuplot requires the follows,

  • each line must contain x and y coordinates and the z value.
  • data are organized into data blocks. And blocks are separated by a single blank line.
  • each block must contain all data points for a single row of data points parallel to x axis. Therefore, the x coordinate stays constant at each data block.
  • all data blocks must contain same number of data points. If the number is not the same, then gnuplot cannot draw a proper surface.

3data.dat

0 0 29.21

0 160 28.94

0 320 28.87

0 480 28.28

0 640 28.83

0 800 27.94

0 960 27.02

 

160 0 28.45

160 160 28.48

160 320 27.73

160 480 27.72

160 640 26.98

160 800 26.72

160 960 26.38

 

320 0 27.30

320 160 26.95

320 320 26.80

320 480 26.85

320 640 26.52

320 800 26.11

320 960 25.95

 

480 0 25.94

480 160 25.86

480 320 25.84

480 480 26.35

480 640 25.61

480 800 25.13

480 960 24.37

 

544 0 25.67

544 160 25.49

544 320 25.50

544 480 25.60

544 640 25.27

544 800 25.63

544 960 24.01

And 3data2.dat

0 0 17.67

0 160 17.67

0 320 17.67

0 480 17.67

0 640 17.67

0 800 17.67

0 960 17.67

 

160 0 17.67

160 160 17.67

160 320 17.67

160 480 17.67

160 640 17.67

160 800 17.67

160 960 17.67

 

320 0 17.67

320 160 17.67

320 320 17.67

320 480 17.67

320 640 17.67

320 800 17.67

320 960 17.67

 

480 0 17.67

480 160 17.67

480 320 17.67

480 480 17.67

480 640 17.67

480 800 17.67

480 960 17.67

 

544 0 17.67

544 160 17.67

544 320 17.67

544 480 17.67

544 640 17.67

544 800 17.67

544 960 17.67

One can execute the script using the command below,

gnuplot> call “./3d.pg” “./3data.dat” “./3data2.dat” “3test.png”

And the plotted figure will be as below,


Figure 1. a 3-D Plot using gnuplot

References:
1. gnuplot In Action

 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Set your Twitter account name in your settings to use the TwitterBar Section.