CiPoint
CiPoint

Reputation: 55

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?

Upvotes: 1

Views: 283

Answers (2)

theozh
theozh

Reputation: 25714

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

Upvotes: 1

Eldrad
Eldrad

Reputation: 743

Colour information can be easily read from the third column using either linecolor variable or linecolor palette z in combination with a predefined palette. I am not sure exactly how you want your shades of blue/green/red, but one possible way would be to go from the full colour to white:

set palette defined (1 'red', 6.9 'white', 7 'green', 12.9 'white', 13 'blue', 18.9 'white')
set cbrange [1:19]

The borders are deliberately at .9, so that no point will ever be truly white, but always have a hue of its original colour.

The data you provided only goes from 1 to 4, so I created some random data to cover the full colour range:

set samples 20
set table $DATA
plot '+' u 1:(rand(0)*30):(round(rand(0)*18)) w table
unset table

One way of combining a line plot with color information would be to add coloured points to the line plot:

plot $DATA u 1:2 w l lc "black", '' u 1:2:3 w p pt 5 ps 5 lc pal z

enter image description here

Upvotes: 1

Related Questions