The default gnuplot command plot
(also only p
) plot dataset with columns, of the form of the data_set.dat
file below.
# Prototype of a gnuplot data set
# data_set.dat
# X - X^2 - 2*X - Random
0 0 0 5
1 1 2 15
1.4142 2 2.8284 1
2 4 4 30
3 9 6 26.46
3.1415 9.8696 6.2832 39.11
4 16 8 20
4.5627 20.8182 9.1254 17
5.0 25.0 10.0 25.50
6 36 12 0.908
As you can see you can write in your data set in floating point notation. Now everything is ready to make the data plot: by typing only
plot "data_set.dat"
gnuplot will produce a graph in your output
destination. The default settings will use the first two columns of your data file, respectively x and y.
To specify the columns to be plotted use the using
specifier
plot "data_set.dat" using 2:4
which means "plot the file using column 2 as X and column 4 as Y". In the case your data set is a tridimensional file just use splot
ad add the z-column
splot "data_set.dat" using 1:2:3
There are also different style (see gnuplot documentation or Selecting a plotting style for further infos) for plotting points. As said before, the default style is point
plot "data_set.dat" using 1:4 with point
which will plot the same as if you do not type with point
. An useful style for data plotting is linespoint
which is, obviously, "lines + points". E.G.:
plot "data_set.dat" using 1:4 with linespoint
# the abbreviated form is completely equivalent:
# p "data_set.dat" u 1:4 w lp
Forms of plotting iteration
In the case you have more columns and want to plot them all in the same graph just pass to the plot
function any argument you prefer, by separating them with a ,
:
p "data_set.dat" u 1:2 w lp,\
"data_set.dat" u 1:3 w lp,\
"data_set.dat" u 1:4 w lp
Anyway sometimes there could be too much columns to write one by one. In these case the for
iteration loop results very useful:
p for [col = 2:4] "data_set.dat" using 1:col w lp
which gives the output
Briefly the for
iteration increment the variable in the loop, in this case col
, with a decided steps (if not specified = 1). For example for [i = 0:6:2]
will increment i
from 0 to 6 in 2 steps: i = 0, 2, 4, 6
. All values (start, stop and increment) are casted to integer values.
*Grid
The grid is often useful when plotting a data set. To add a grid type
set grid