Search code examples
chartsgnuplot

How to use a column in gnuplot to separate plots


I have the following data to draw the CPU usage of running tasks at specific time stamps.

cpu timestamp load taskId
0 0 1.0 0
1 0 1.0 0
0 0 0.5 1
1 0 0.5 1
0 65 0.5 0
1 65 0.5 0
0 20094 0.0 0
1 20094 0.0 0
0 40042 0.0 1
1 40042 0.0 1

This is what I use to create the plot using gnuplot:

set key autotitle columnheader
set xtics rotate
#set xrange [-0.5:100000]
set ylabel "CPU Load"
set xlabel "Time"
set key right
set grid y x
#set yrange [-0.05:1.01]
plot 'test.dat' using (column(2)):3 with linespoints
pause -1 "Hit return to continue"

The result is:

wrong result

However, I would like to plot two lines one each for a task ID, using the fourth column. The result should be like:

correct result

How can I fix my graph? If I can also plot two graphs using the first column, that would be even better.


Solution

  • set datafile missing NaN
    set offset graph 0.05, graph 0.05
    set ylabel "CPU Load"
    set xlabel "Time"
    set key outside right
    set grid y x
    set xtics rotate
    set style data linespoints
    set autoscale noextend
    set border back
    
    set multiplot layout 2,1
    do for [i=0:1] {
        set title sprintf("CPU %d", i)
        plot 'data' using (column(1) == i ? column(2) : NaN) : 3 title "Task 0" pt 7 lw 2, \
             'data' using (column(1) == i ? column(2) : NaN) : 4 title "Task 1" pt 7 lw 2
    }
    unset multiplot
    

    enter image description here