Question

I'm trying to plot some measurements in a data file. Here is a simplified example of the data:

1  7 100
1 15 200
1 31 300
2  7 150
2 15 300
2 31 400

As you see, there are three columns. The second column should be the x axis, where the third column contains the value. But how do I make gnuplot make a new plot for every distinct value in column one? The example above should result in two plots, one called 1 and one called 2.

Was it helpful?

Solution

There are at least two different ways to go about this:

1) If you know how many entries there are for each set, you can use every. The following plots data points 0 to 2, and 3 to 5 separately (the first data point is zero in gnuplot) with the data style that you presented above:

plot "data" u 2:3 every ::0::2 title "First block", \
"data" u 2:3 every ::3::5 title "Second block"

If your data blocks were conveniently separated by one empty line, this could be done using data block units, rather than data points:

# Data separated by empty lines:
1  7 100
1 15 200
1 31 300

2  7 150
2 15 300
2 31 400

plot the above with:

plot "data" u 2:3 every :::0::0 title "First block", \
"data" u 2:3 every :::1::1 title "Second block"

2) If you don't know how many entries of each type you have, you could use a conditional plot, telling gnuplot that if column 1 equals 1 it should plot the point, otherwise it should be ignored (and the same for the second plot, but in that case column 1 must equal 2):

plot "data" u 2:($1 == 1 ? $3 : 1/0) title "First block", \
"data" u 2:($1 == 2 ? $3 : 1/0) title "Second block"

The condition is constructed like this:

$1 == 1 ? --> Is it true that column 1 equals one?

$3 : 1/0 --> If yes, then plot column 3, else plot 1/0, which in gnuplot means "ignore" data point

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top