gnuplot — Basic 3D Plot

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

gnuplot–Basic 2D Plot

This is the third project that I needed to use gnuplot, and I’m learning it for the third time. I know I’ll need to write a note for it. So here it is.

gnuplot is easy to start with. You can plot a nice graph after reading a tutorial for 5 minutes. This post summarizes some basic stuff for 2D plotting.

gnuplot supports both interactive mode and scripting mode. We’ll cover both.

A Simple Plot

The plot command is used to plot 2D figures and graphs. It’s very simple to use. For example, you want to plot a sine function and a y=x function. First, you enter the “gnuplot” command in the Linux command shell to go the gnuplot command interface.

And then you enter the command below,

gnuplot> plot sin(x), x

This gives you the figure below,

Figure 1. a sine function and a y=x function drawn by gunplot

 

Plotting for a Data File

gnuplot is easy to integrate with other programs. Another program can output a data file and gnuplot plot the graph based on the data file.

Suppose we have a data file named “data.dat” with content as below,

0.1 31.15

0.2 30.42

0.3 29.94

0.4 27.65

0.5 25.76

0.6 23.61

0.7 21.16

0.8 20.13

0.9 18.30

1.0 16.81

To plot the graph based on the data file, we can use the command below,

gnuplot>  plot “data.dat” using 1:2 title “1-3” with lines

And we get the figure below,

Figure 2. gnuplot plots figure based on a data file

Note that “using 1:2” maps the first column of the data to x axis and the second column of the data to y axis. title “1-3” specifies the legend title for the line. “with line” specifies the drawing styles. Other common options of styles include “points” and “linespoints”.

Also note that it is easy to draw two lines with data from the same file. Suppose the data.dat file contains the third column, then the following command can give us two lines.

gnuplot> plot “data.dat” using 1:2 title “1-3” with lines, “data.dat” using 1:3 title “1-3” with points

 

Plotting for Multiple Files

It is easy to plot from multiple data files too. Suppose we have another file “data2.dat” with the content as below,

0.1 17.67

0.2 17.67

0.3 17.67

0.4 17.67

0.5 17.67

0.6 17.67

0.7 17.67

0.8 17.67

0.9 17.67

1.0 17.67

gnuplot> plot “data.dat” using 1:2 title “1-3” with lines, “data2.dat” using 1:2 with linespoints

This gives us the figure below,

Figure 3. gnuplot plot from two data files

Export Current Configuration as Script

Once you finished plotting a figure, you may want to save the command and configuration for later usage. You can do it using the command below,

gnuplot> save “test.gp”

Suppose you just finished the example above, and the save command will give you a file as below,

#!/usr/bin/gnuplot -persist

#

#    

#        G N U P L O T

#        Version 4.2 patchlevel 6 

#        last modified Sep 2009

#        System: Linux 2.6.32-40-generic

#    

#        Copyright (C) 1986 - 1993, 1998, 2004, 2007 - 2009

#        Thomas Williams, Colin Kelley and many others

#    

#        Type `help` to access the on-line reference manual.

#        The gnuplot FAQ is available from http://www.gnuplot.info/faq/

#    

#        Send bug reports and suggestions to <http://sourceforge.net/projects/gnuplot>

#    

# set terminal wxt 0

# set output

unset clip points

set clip one

unset clip two

set bar 1.000000

set border 31 front linetype -1 linewidth 1.000

set xdata

set ydata

set zdata

set x2data

set y2data

set timefmt x "%d/%m/%y,%H:%M"

set timefmt y "%d/%m/%y,%H:%M"

set timefmt z "%d/%m/%y,%H:%M"

set timefmt x2 "%d/%m/%y,%H:%M"

set timefmt y2 "%d/%m/%y,%H:%M"

set timefmt cb "%d/%m/%y,%H:%M"

set boxwidth

set style fill  empty border

set style rectangle back fc lt -3 fillstyle  solid 1.00 border -1

set dummy x,y

set format x "% g"

set format y "% g"

set format x2 "% g"

set format y2 "% g"

set format z "% g"

set format cb "% g"

set angles radians

unset grid

set key title ""

set key inside right top vertical Right noreverse enhanced autotitles nobox

set key noinvert samplen 4 spacing 1 width 0 height 0 

unset label

unset arrow

set style increment default

unset style line

unset style arrow

set style histogram clustered gap 2 title  offset character 0, 0, 0

unset logscale

set offsets 0, 0, 0, 0

set pointsize 1

set encoding default

unset polar

unset parametric

unset decimalsign

set view 60, 30, 1, 1  

set samples 100, 100

set isosamples 10, 10

set surface

unset contour

set clabel '%8.3g'

set mapping cartesian

set datafile separator whitespace

unset hidden3d

set cntrparam order 4

set cntrparam linear

set cntrparam levels auto 5

set cntrparam points 5

set size ratio 0 1,1

set origin 0,0

set style data points

set style function lines

set xzeroaxis linetype -2 linewidth 1.000

set yzeroaxis linetype -2 linewidth 1.000

set zzeroaxis linetype -2 linewidth 1.000

set x2zeroaxis linetype -2 linewidth 1.000

set y2zeroaxis linetype -2 linewidth 1.000

set ticslevel 0.5

set mxtics default

set mytics default

set mztics default

set mx2tics default

set my2tics default

set mcbtics default

set xtics border in scale 1,0.5 mirror norotate  offset character 0, 0, 0

set xtics autofreq  norangelimit

set ytics border in scale 1,0.5 mirror norotate  offset character 0, 0, 0

set ytics autofreq  norangelimit

set ztics border in scale 1,0.5 nomirror norotate  offset character 0, 0, 0

set ztics autofreq  norangelimit

set nox2tics

set noy2tics

set cbtics border in scale 1,0.5 mirror norotate  offset character 0, 0, 0

set cbtics autofreq  norangelimit

set title "" 

set title  offset character 0, 0, 0 font "" norotate

set timestamp bottom 

set timestamp "" 

set timestamp  offset character 0, 0, 0 font "" norotate

set rrange [ * : * ] noreverse nowriteback  # (currently [0.00000:10.0000] )

set trange [ * : * ] noreverse nowriteback  # (currently [-5.00000:5.00000] )

set urange [ * : * ] noreverse nowriteback  # (currently [-5.00000:5.00000] )

set vrange [ * : * ] noreverse nowriteback  # (currently [-5.00000:5.00000] )

set xlabel "" 

set xlabel  offset character 0, 0, 0 font "" textcolor lt -1 norotate

set x2label "" 

set x2label  offset character 0, 0, 0 font "" textcolor lt -1 norotate

set xrange [ * : * ] noreverse nowriteback  # (currently [-10.0000:10.0000] )

set x2range [ * : * ] noreverse nowriteback  # (currently [-10.0000:10.0000] )

set ylabel "" 

set ylabel  offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90

set y2label "" 

set y2label  offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90

set yrange [ * : * ] noreverse nowriteback  # (currently [-10.0000:10.0000] )

set y2range [ * : * ] noreverse nowriteback  # (currently [-10.0000:10.0000] )

set zlabel "" 

set zlabel  offset character 0, 0, 0 font "" textcolor lt -1 norotate

set zrange [ * : * ] noreverse nowriteback  # (currently [-10.0000:10.0000] )

set cblabel "" 

set cblabel  offset character 0, 0, 0 font "" textcolor lt -1 rotate by 90

set cbrange [ * : * ] noreverse nowriteback  # (currently [-10.0000:10.0000] )

set zero 1e-08

set lmargin  -1

set bmargin  -1

set rmargin  -1

set tmargin  -1

set locale "C"

set pm3d explicit at s

set pm3d scansautomatic

set pm3d interpolate 1,1 flush begin noftriangles nohidden3d corners2color mean

set palette positive nops_allcF maxcolors 0 gamma 1.5 color model RGB 

set palette rgbformulae 7, 5, 15

set colorbox default

set colorbox vertical origin screen 0.9, 0.2, 0 size screen 0.05, 0.6, 0 front bdefault

set loadpath 

set fontpath 

set fit noerrorvariables

GNUTERM = "wxt"

plot "data.dat" using 1:2 title "1-3" with lines, "data2.dat" using 1:2 with linespoints

#    EOF

Next time you want to draw the figure again, simple execute “load” or “call” command,

gnuplot> load “test.pg”

or

gnuplot> call “test.pg”

The differences between load and call is that call allows one to pass up to 10 parameters, which corresponds to $0 – $9 in the script file.

Saving the Drawing as Pictures

gnuplot allows one to export the plot in many formats, including “png”, “svg”, “postscript” etc. One can check out all the formats supported by “set term” command.

Below is the commands that exports the plot as png file.

gnuplot> set term png
gnuplot> set output “test.png”
gnuplot> replot

The first command set the output device type, the second command sets the output file and the third command replot the plot on the output file.

A more general script can be written as below to export plot,

set terminal push   # save the current terminal settings

set terminal png    # change terminal to PNG

set output "$0"     # set the output filename to the first option

replot              # repeat the most recent plot command

set output          # restore output to interactive mode

set terminal pop    # restore the terminal

Assume the script file is “test.gp”, then we can execute the command below in gnuplot command line interface to generate the graph.

call  “test.gp” “test.png”

A Simple Script

Now we provide a simple script,

unset title

#unset key

#unset xtics

#unset ytics

#set format y "%.1t";

#set format x "%.1t";

set title "Testndata.dat VS. data2.dat" 0,1

set xlabel "X";

set ylabel "Y";

#set border 0

#set xrange []

#set yrange []

plot '$0' using 1:2 title "data.dat" with linespoints, '$1' using 1:2 title "data2.dat" with linespoints

#

set term push

set term png size 800, 600

set output "$2"

replot

set output

set terminal pop

This script accepts three input parameters, $0 and $1 are two data files, and $2 is the output png filename.

Suppose the script is named “test.pg”, and we still use the two data files “data.dat” and “data2.dat”, and we output to “test.png” file. Then we can enter the command below,

gnuplog> call “test.pg” “data.dat” “data2.dat” “test.png”

Then we get the figure below,

Figure 4. gnuplot plot with a script

Once we’re done with gnuplot, we can type “quit” or “exit” to exit from the gnuplot command line interface.