Plotting XYZ Data Files with Gnuplot

Gnuplot has strong support for creating three dimensional plots for functions and for properly formatted data files. In gnuplot's case, 'properly formatted' means that the data is not arranged as sets of z values with their corresponding x and y values, but as z values whose x and y values are indicated by their relative position (the Gnuplot documentation calls this 'grid' format). Thus:


Table 1: Structure of z values in grid format

The following tables feature the same z values, but in grid or xyz formats:


Table 2: Grid Format


Table 3: XYZ Format

This demo shows some commands for producing projection plots and surface plots of three dimensional data.

Palette Mapped Projection View

Suppose we want a projection view of some data in xyz format (the data for the following samples can be downloaded here, it's just the first half of a sine wave along two axes, generated by a simple VBScript program). This can be obtained with the following:

set key off
splot 'xyz.tsv' using 1:2:3 with points palette pointsize 3 pointtype 7

Which produces the following plot. The key is turned off because it doesn't make much sense in this plot. Notice that the zero point of the z (vertical) axis is offset from the x and y axes. To remove this offset, use set ticslevel 0 before the splot command. To remove the colour-value palette box, use unset colorbox.

Figure 1: Projection of Z

Figure 1: Projection View of z Values

Palette Mapped Surface (Map) View

Converting this plot to a two-dimensional 'map' style view (like a plan view of the current plot, and sometimes known as a 'raster plot'), can be done with the set view map command (try it with the plot above, without changing the other commands). Also, the plot's appearance can be made a bit more pleasing to the eye with some additional commands.

The ratio refers to the height vs. width of the points, whilst the object commands generate a black background.

set view map
set size ratio .9

set object 1 rect from graph 0, graph 0 to graph 1, graph 1 back
set object 1 rect fc rgb "black" fillstyle solid 1.0

splot "xyz.tsv" using 1:2:3 with points pointtype 5 pointsize 1 palette linewidth 30

These commands generate the following plot:

Figure 1: Projection of Z

Figure 2: Surface View of z Values

The pointsize and linewidth properties overlap a bit in this instance; the same effect can be obtained using a pointsize of six and a linewidth of one. You might want to adjust the size of the points to either separate them or to create a continuous 'surface'.

Home About Me
Copyright © Neil Carter

Content last updated: 2011-06-22