plotgnuplotgnuplot-iostream

gnuplot every command with different lines color and legend


Lets say I have this sample data file

1 2
2 3
3 4

1 5
2 6
3 7

1 8
2 9
3 10

Now in gnuplot if I run this command

pl 'test.dat' u 1:2 every :::0::2 w l

It plots three lines for each of the block in the data file, but there's no way to distinguish which line comes from which data block. I want those three lines to have three different colors and different legend labels. Can I do that in addition to the every command?


Solution

  • Sure, there are multiple ways to achieve that. If you insist on having a single empty line between the blocks and on using every, you can plot iteratively:

    plot for [i=0:*] 'test.dat' u 1:2 every :::i::i w l lc i

    Alternatively, if you separate your data block with two empty lines, you can use the index:

    1 2
    2 3
    3 4
    
    
    1 5
    2 6
    3 7
    
    
    1 8
    2 9
    3 10
    

    plot for [i=0:*] 'test.dat' index i u 1:2 w l lc i (shortcut i i instead of index i is also allowed, but difficult to read)

    Or without iteration, but using the pseudo-column -2 (which gives you the index number). Note that gnuplot doesn't draw continuous lines between points that are separated with empty lines, therefore the every command is not necessary.

    plot 'test.dat' u 1:2:-2 w l lc variable

    Automatically generated, different labels can be produced in the following way:

    plot for [i=0:*] 'test.dat' i i u 1:2 w l lc i t sprintf('This is block %d', i)

    enter image description here