Reputation: 7005
I have 3 columns in my data. The first being a date. I want to plot the 2nd column but change its colour depending on value in 3rd column.
If 3rd column is:
Positive: Black
Negative: Red
zero: Yellow
This is what I am trying:
set palette model RGB defined ( 0 'yellow', 1 'red', 2 'black' )
plot "output2.txt" u 1:2:( $3 < 0 ? 1 : ( $3 > 0 ? 2 : 0 ) ) w lines palette axes x1y1
But I don't get Red ever, even though Column 3 has negative numbers.
EDIT 1
I also get it to work SOMETIMES, but here is a case where it does not:
$ cat t.txt
1 1 1000000
2 2 1000000
3 3 1000000
4 4 -1000000
5 5 -1000000
6 6 -1000000
7 7 -1000000
Here is my gnu script:
$ cat test.gnu
set terminal wxt size 1500,900
set style fill transparent solid 0.50 noborder
set key font ",6"
set palette model RGB defined ( 0 'yellow', 1 'red', 2 'black' )
plot "t.txt" u 1:2:( $3 < 0 ? 1 : ( $3 > 0 ? 2 : 0 ) ) w lines palette axes x1y1
This is how I call it from cygwin:
~/wgnuplot -e "load \"test.gnu\"" -persist
This is the result (The yellow line should be red) :
Upvotes: 3
Views: 2370
Reputation: 48390
The values given in the palette aren't absolute values, but the lowest number (here 0) is mapped to the minimum of the cbrange
and the highest number (here 2) is mapped to the cb-maximum.
The values given in the third column of the using
statement, however, are absolute color values and depend on the cbrange
. In your problematic case the autoscaling sets a cbrange
of [1:2]
, so that 1
gives yellow, 1.5
gives red and 2
gives black.
Just use set cbrange [0:2]
to fix the problem.
Upvotes: 1
Reputation: 309841
Rather than palette
, I would use linecolor variable
:
plot 'test.txt' u 1:2:($3 < 0 ? 2 : ( $3 > 0 ? 3 : 1 )) w lines lc variable
The numbers returned from your ternary operator expression (1, 2, 3) should correspond to line styles that you set.
In this case, I think it's:
set style line 1 lc rgb "yellow"
set style line 2 lc rgb "red"
set style line 3 lc rgb "black"
(tested with the following data):
1 2 3
2 2 3
3 2 0
4 2 0
5 2 12
6 2 -15
7 2 -23
Note that this works well if you have highly sampled data. Poorly sampled data can make for a funny looking plot since the line segment is colored based on the value of the data at the second of the endpoints encountered in the file.
For what it's worth, I get the same result if I use:
set palette defined (1 'yellow', 2 'red', 3 'black')
plot 'test.txt' u 1:3:($3 < 0 ? 2 : ( $3 > 0 ? 3 : 1 )) w lines lc palette z
(only with a colorbar and key this time).
Upvotes: 2