0
votes

I know how to make a surface plot with a function defined in the script such as:

#!/usr/bin/env gnuplot
set terminal png
set output 'main.png'
set isosamples 7, 3
set urange [-3.0 : 3.0]
set vrange [ 1.0 : 3.0]
set hidden3d
set xyplane at 0
splot '++' using 1:2:($1**2) with lines palette

which gives:

enter image description here

but how can I achieve a similar plot is the data I want to plot is stored in a file?

What is the format of data that Gnuplot expects in this case?

I've seen some related documentation pages such as:

and I'm certain I'll end up finding a way to do it, but it felt a bit harder than necessary to extract a minimal working example from those pages, thus the question.

Tested on gnuplot 5.2 patchlevel 8.

2

2 Answers

0
votes

There are a few possible formats that you can use as shown in the following examples.

Explicit coordinates for every point

This format sets explicit x, y, z coordinates for every point. You have to separate different x values with a double newline.

explicit.gnuplot

#!/usr/bin/env gnuplot
set terminal png
set output 'explicit.png'
set hidden3d
set xyplane at 0
splot 'explicit.dat' with lines palette

explicit.dat

-3 1 9
-3 2 9
-3 3 9

-2 1 4
-2 2 4
-2 3 4

-1 1 1
-1 2 1
-1 3 1

0 1 0
0 2 0
0 3 0

1 1 1
1 2 1
1 3 1

2 1 4
2 2 4
2 3 4

3 1 9
3 2 9
3 3 9

Output:

enter image description here

so we see that this produces output identical to the formulas as desired.

Matrix format

The matrix format produces a file that is much smaller as it does not repeat the coordinates for every single point: the coordinates are just inferred from their position in the file as documented at: http://www.gnuplot.info/docs_4.2/node332.html

However, I haven't managed to find a way to set the coordinates in this format: How to plot 3D matrix CSV data in Gnuplot with splot using the first row and column as the x y coordinates?

matrix.gnuplot

#!/usr/bin/env gnuplot
set terminal png
set output 'matrix.png'
set hidden3d
set xyplane at 0
splot 'matrix.dat' matrix with lines palette

matrix.dat

9 4 1 0 1 4 9
9 4 1 0 1 4 9
9 4 1 0 1 4 9

Output:

enter image description here

So as mentioned, the problem with this output is that the axes automatically start at 0 instead of the desired -3, and I don't know how to set them to -3 from the .dat file itself.

Binary matrix format

This format is documented at: http://www.gnuplot.info/docs_4.2/node330.html According to the documentation, it is very similar to the ASCII matrix, but it does support setting coordinates inside the data itself.

Since this is a binary however format I didn't have the patience to come up with an example at the moment.

Tested on gnuplot 5.2 patchlevel 8.

0
votes
$MATRIX << EOM
9 4 1 0 1 4 9
9 4 1 0 1 4 9
9 4 1 0 1 4 9
EOM

set hidden3d
set xyplane at -3
splot $MATRIX matrix using 1:2:($3-3) with lines palette

enter image description here