- not so Frequently Asked Questions -

update 2007/08/21

Plotting Numerical Data in a Data File (No.2)

1 | 2 | 3 | 4

How do I plot several data sets in a single file ?

In order to plot several data those are stored in one file, use using and index. Here, "data" means a set of XY pairs. Gnuplot draws one line or prints the same symbol at each data-point.

When those data have the same X-values, prepare the data in a table format, and use the using option to specifiy Y-data. The next example file contains three Y-values at the same X-value.

#   X         Y1         Y2         Y3
   -1.0000    0.0000     0.0000     1.0000
   -0.9000    0.5700     1.1769     0.7150
   -0.8000    1.0800     1.4400     0.4600
   -0.7000    1.5300     1.4997     0.2350
   -0.6000    1.9200     1.4400     0.0400
   -0.5000    2.2500     1.2990    -0.1250
   -0.4000    2.5200     1.0998    -0.2600
   -0.3000    2.7300     0.8585    -0.3650
   -0.2000    2.8800     0.5879    -0.4400
   -0.1000    2.9700     0.2985    -0.4850
    0.0000    3.0000    -0.0000    -0.5000
    0.1000    2.9700    -0.2985    -0.4850
    0.2000    2.8800    -0.5879    -0.4400
    0.3000    2.7300    -0.8585    -0.3650
    0.4000    2.5200    -1.0998    -0.2600
    0.5000    2.2500    -1.2990    -0.1250
    0.6000    1.9200    -1.4400     0.0400
    0.7000    1.5300    -1.4997     0.2350
    0.8000    1.0800    -1.4400     0.4600
    0.9000    0.5700    -1.1769     0.7150
    1.0000    0.0000    -0.0000     1.0000
gnuplot> plot "test.dat" using 1:2 with lines,\
              "test.dat" using 1:3 with lines,\
              "test.dat" using 1:4 with lines

When the X-values are different, the whole data are separated into several blocks, and put two blank lines between the each block. (Note: here we use the term "block" for a set of data points in a general meaning, and this is different from the gnuplot's definition of "datablock". The datablock in gnuplot is a set of data points separated by a single blank line.)

#  X     Y     Yerror
   1.0   1.2   0.1
   2.0   1.8   0.1
   3.0   1.6   0.1

   1.1   0.8   0.2
   2.1   0.3   0.2
   3.1   1.0   0.2

   1.2   1.5   0.3
   2.2   2.3   0.3
   3.2   3.1   0.3
gnuplot> plot "test.dat" using 1:2 with lines
gnuplot> plot "test.dat" using 1:2:3 with yerrorbars

As you can see above, all the data in the different data blocks are drawn by the same line/symbol type. To make them different, specify a data block by index . The first data block has an index of zero, the second is one, and so on.

gnuplot> plot "test.dat" index 0 using 1:2:3 with yerrorbars,\
              "test.dat" index 1 using 1:2:3 with yerrorbars,\
              "test.dat" index 2 using 1:2:3 with yerrorbars

You can specify the range of index. For example, to use the same symbol for the first and second blocks, and change the symbol for the third block:

gnuplot> plot "test.dat" index 0:1 using 1:2:3 with yerrorbars,\
              "test.dat" index 2   using 1:2:3 with yerrorbars

I want to modify values in my data file when plotting.

You can make some simple calculations and change the data of the column which is specified by the using option. With the following data we show four cases simultaneously --- Y-values themselves, Y-values are doubled, squared, and logarithm is taken.

#  X     Y
   1.0   1.2
   2.0   1.8
   3.0   1.6
gnuplot> plot "test.dat" using 1:2          with points,\
              "test.dat" using 1:($2*2)     with points,\
              "test.dat" using 1:(sqrt($2)) with points,\
              "test.dat" using 1:(log($2))  with points

Specify the n-th column by $n , and make some operation. The expression should be put into parenthesis. An expression like using 1:sqrt($2) does not work. You can also make some calculations which need several columns. For example, using 1:2:($2*$3) makes a product of the second and third columns.

Sometimes this technique is useful when you have a data file which contains Y-value uncertainties, and the uncertainties are represented by a relative error (%). To draw error bars your data uncertainties should be absolute. To convert from relative uncertainties into absolute ones, using 1:2:($2*$3/100.0), where the second column is the Y-data, and the third column is the percent errors.

I want to put some plotting commands in a data file.

Gnuplot uses two files, one is the data and another is the control command. If your data are not so complicated, you can put the plot command into one file together with the data themselves. Basically it is the same as a usual command file, but the data file to be plotted is not an external one but it is just "-", and the numerical data follow. Gnuplot stops reading the data when a line begins with the letter "e".

set xrange [0:5]
set yrange [0:3]
plot "-" using 1:2:3 w yerrorbars
#  X     Y     Z 
   1.0   1.2   0.2
   2.0   1.8   0.3
   3.0   1.6   0.2
   4.0   1.2   0.2
pause -1

Prepare a control file which includes the data just like above, and give the name as a command line option when gnuplot is invoked.

 % gnuplot "test.plt"

By the way, you cannot use the replot command in this method. Gnuplot tries to read the data from the standard input again, because Gnuplot does not remember the data.

I want to eliminate some data points.

There are two ways to skip some data in a data file -- the first one is to edit the data file, alternatively you can use the every option. To skip some points in your data by editing the data file, put some letter like "?" just before the number. This letter is defined by the set missing command, but any kinds of letters which cannot be converted into numbers work too.

The next example is to draw graphs of (X,Y1) and (X,Y2).

test1.dat test2.dat
#  X     Y1    Y2
   1.0   1.2   1.5
  ?2.0   1.8   0.8
   3.0   1.6   1.1
#  X     Y1    Y2
   1.0   1.2   1.5
   2.0   1.8  ?0.8
   3.0   1.6   1.1

In the left drawing, the second X value has "?" mark, so that the second data points (2.0,1.8) and (2.0,0.8) are skipped. In the right drawing, the point (2.0,1.8) still is alive, but (2.0,0.8) is erased.

gnuplot> plot "test1.dat" using 1:2 with linespoints,\
              "test1.dat" using 1:3 with linespoints
gnuplot> plot "test2.dat" using 1:2 with linespoints,\
              "test2.dat" using 1:3 with linespoints

This function is useful if you want to alter the range of plot in a table formatted data. Let's plot the following data in log-scale, the second (Y1) and the third (Y2) columns are plotted simultaneously.

#  X     Y1    Y2
   1.0   0.0   0.1
   2.0   0.0   0.1
   3.0   0.0   0.1
   4.0   0.1   0.2
   5.0   0.6   0.4
   6.0   1.0   0.9
   7.0   1.2   1.7
   8.0   1.3   2.4

The Y1-values are zero in the X range of [1:3], so you cannot take log here. Gnuplot produces a vertical line at the lowest point which has a positive value (here, that is X=4).


To remove this vertical line, put "?" at Y1=0.0, like "?0.0". Then the Y1 data (the red line) are treated as that they begin at X=4. The same thing you can do with the plot command option every ::3 . See the next topic.


How do I plot a part of data in a file ?

To specify a range of the data to be plotted, use the every option in the plot command. To skip every two lines, say plot "test.dat" every 2


When the data file contains several data blocks those are separated by a single blank line, you can skip the data block by the every option. To skip every two block, try plot "test.dat" every :2

every I:J:K:L:M:N
I Line increment
J Data block increment
K The first line
L The first data block
M The last line
N The last data block
every 2 plot every 2 line
every ::3 plot from the 3-rd lines
every ::3::5 plot from the 3-rd to 5-th lines
every ::0::0 plot the first line only
every 2::::6 plot the 1,3,5,7-th lines
every :2 plot every 2 data block
every :::5::8 plot from 5-th to 8-th data blocks

Alternatively (if you are on the UNIX-like system), a part of your data file can be plotted by using the unix commands, "head" and "tail".

gnuplot> plot "< head -10 test.dat" using 1:2 with lines
gnuplot> plot "< tail -3 test.dat" using 1:2 with lines
gnuplot> plot "< head -5 test.dat" using 1:2 with lines,\
>        plot "< tail -5 test.dat" using 1:2 with points

The first "plot" command says to plot the first 10 lines in the data file "test.dat", and the second "plot" means to show the last 3 lines in the data file. The next lines are an example to draw a graph of the data file "test.dat" --- the first 5 points are shown by lines, and the last 5 points are by symbols.

from an Iwata-kun's question. Thanks !