Search code examples
gnuplot

How can I plot several diagrams that represent several subset rows from the dataset in one plot using Gnuplot?


I have a dataset has a name ('output.txt') and formatted as follows:

    1   ......
    2   ......
    4   .....
    6   ......
    7   .....
    10   ......
    1   ......
    2   ......
    5   .....
    6   ......
    7   .....
    1   ......
    3   ......
    4   .....
    6   ......
    7   .....
    10   ......
.
.
.
.
.
.
    2   ......
    4   .....
    6   ......
    7   .....
    10   ......

I want to plot each subset of rows (until I reach the value of x+1 is less than the value of x) in its own colored diagram, and all the diagrams in one plot. How can I do this using Gnuplot?


Solution

  • From your incomplete example data it looks like the x-values are not the same for each block and also even not the same amount of x-values per block. If it was regular, e.g. always 6 values for each block you could simply use plot for and every, check help plot for and help every. But it is not regular in your case.

    In order to plot each of your "blocks" with different color, you have to tell gnuplot where the blocks end. Usually this is done with one or several empty lines. Unfortunately, you don't have empty lines between your blocks, so you have to insert them yourself. Either by an external tool or by gnuplot itself (the latter will take a few extra lines of code). Here it is necessary to have the data in a datablock. How to get a file into there, see (gnuplot: load datafile 1:1 into datablock)

    A second approach (if you want to plot with linespoints) would be to plot twice once with points and a second time with lines where the color is changed every time when the current x-value is smaller than the previous value. In order to make a split between the blocks, the color is set to invisible (myLineC=-2). If you only want lines you can simply skip the plot with points. Depending on your exact needs (colors, key, etc. ) it maybe need to be further fine tuned.

    Code:

    ### split data based on x-values
    reset session
    
    $Data <<EOD
        1   0.11
        2   0.12
        4   0.13
        6   0.14
        7   0.15
        10  0.16
        1   0.21
        2   0.22
        5   0.23
        6   0.24
        7   0.25
        1   0.31
        3   0.32
        4   0.33
        6   0.34
        7   0.35
        10  0.36
        2   0.41
        4   0.42
        6   0.43
        7   0.44
        10  0.45
    EOD
    
    # first approach by inserting empty lines
    set title "Approach 1"
    set print $DataSplit
        x0=NaN
        do for [i=1:|$Data|] {
            x1 = real(word($Data[i],1))
            if (x1<x0) { print "\n" }
            x0 = x1
            print $Data[i]
        }
    set print
    plot $DataSplit u 1:2:(column(-1)+1) w lp pt 7 lc var notitle, \
    
    pause -1 "Press Enter to continue"
    
    # second approach by setting the color invisible to split
    set title "Approach 2"
    myColor = 1
    x0=NaN
    plot $Data u ((x0>$1 ? (myColor=myColor+1,myLineC=-2) : myLineC=myColor), x0=$1):2:(myLineC) w l lc var notitle, \
        tmp=(x0=NaN, myColor=1) $Data u (($1<x0 ? myColor=myColor+1 : 0), x0=$1):2:(myColor) w p pt 7 lc var notitle
    ### end of code
    

    Result: (twice the same result)

    enter image description here