Search code examples
gnuplot

Color x-axis or line depending on y-value


I have data looking like this:

0.0 23.5 1
0.1 12.2 1
0.2 15.0 3
0.3 34.4 4
0.4 37.1 4

The 3rd column represents something like a category to which the data from 2nd column belongs. I'd like to plot 2nd column vs 1st column as a default line plot and represent the category in 3rd column through the color of the x-axis (or a line parallel to the x-axis somewhere in the plot).

There are 18 categories in total, I'd like to have categories encoded as follows 1 ... 6 shades of red 7 ... 12 shades of green 13 ... 18 shades of blue

How could this be achieved?


Solution

  • As I understand, you want a line graph and some horizontal line with some color depending on value in the 3rd column. So, why not plotting a bar graph? Check the example below which can be adapted to your special needs.

    Code:

    ### apply shades of colors from a column
    reset session
    
    # create some test data
    set print $Data
        do for [i=1:18] {
            print sprintf("%g %g %g", i/10., rand(0)*10+10, i)
        }
    set print
    
    set palette defined ( 0 "#aa0000", 1 "#ee0000", 1.99 "#ffaaaa", \
                          2 "#00aa00", 3 "#00ee00", 3.99 "#aaffaa", \
                          4 "#0000aa", 5 "#0000ee", 5.99 "#aaaaff" ) maxcolors 18
    
    set style fill solid 1.0
    set boxwidth 0.8 relative
    set xtics out
    set yrange[0:30]
    set cbrange[1:18]
    set cbtics ()
    do for [i=1:18] {
        set cbtics add (sprintf("%g",i) i/18.*17+0.5)
    }
    set key noautotitle
    
    plot $Data u 1:2:3 w boxes lc palette
    ### end of code
    

    Result:

    enter image description here

    Code: (alternatively, a line graph with color bars at the bottom)

    ### apply shades of colors from a column
    reset session
    
    # create some test data
    set print $Data
        do for [i=1:18] {
            print sprintf("%g %g %g", i/10., rand(0)*10+10, i)
        }
    set print
    
    set palette defined ( 0 "#aa0000", 1 "#ee0000", 1.99 "#ffaaaa", \
                          2 "#00aa00", 3 "#00ee00", 3.99 "#aaffaa", \
                          4 "#0000aa", 5 "#0000ee", 5.99 "#aaaaff" ) maxcolors 18
    
    set style fill solid 1.0
    set boxwidth 0.8 relative
    set xtics out
    set yrange[0:30]
    set grid x,y
    set cbrange[1:18]
    set cbtics ()
    do for [i=1:18] {
        set cbtics add (sprintf("%g",i) i/18.*17+0.5)
    }
    set key noautotitle
    
    plot $Data u 1:2 w lp pt 7 lc "black", \
            '' u 1:(0.5):3 w boxes lc palette
    ### end of code
    

    Result:

    enter image description here