draki
draki

Reputation: 43

gnuplot vary colours of boxes by value

I hope you can help (I'll make my excuses in advance: I'm new to gnuplot and this is not my area of expertise!)

I'm using gnuplot to generate a graph of my download speed. I want to plot the values as a bar graph and colour the columns by speed: Green for above 190Mb/s, red for below 50 and orange for the intermediate values

I have had partial success referring to this: Color bars in different colors for some specific values in Gnuplot

I want it to plot MB/s values over 190 as green MB/s between 50 and 190 as orange MB/s values below 50 as red

It plots fine as long as I have at least one value less than 50 in the set, but if there are no values less than 50 it plots all the values less than 190 as red, when they should be orange

how it should look (with a value of less than 50) GOOD IMAGE

how it actually looks with no values less than 50 BAD IMAGE

this is the gnuplot script I'm using (there are probably extraneous expressions - I've been messing around with it quite a lot)

    #!/usr/bin/gnuplot
reset
set terminal png transparent truecolor

set xdata time
set timefmt "%Y-%m-%d, %H:%M"
set format x "%H"

set key off

#set xtics rotate
set xtics font "ubuntumono,6" textcolor rgb "white"
set xtics 3600

set ylabel "Mbit/s" font "ubuntu,8" offset 3,0,0 textcolor rgb "white"
set ytics font "ubuntu,8" textcolor rgb "white"
set yrange [0:*]
set y2range [0:40]

set grid ytics
set term png size 400, 135

set style data boxes
set boxwidth 3500 absolute
set style fill transparent solid 0.9 border rgb "black"
set palette model RGB defined (0 "red", 1 "orange", 2 "green")
set bmargin at screen 0
set lmargin 5
set rmargin 0.7
set tmargin 1

unset colorbox

set x2tics font "ubuntu,7" textcolor rgb "white"
set x2tics 43200, 43200, time(0)-21600 offset 0,-0.5,0
set x2data time
set format x2 "%a %H:%M"

unset border

plot 'test.dat' using (timecolumn(1)):4:($4>=190 ? 2 : $4>=50 ? 1 : 0) with boxes palette, \

and this is a sample of the data file

2016-03-02, 05:40, 20.373, 200.72, 12.02,
2016-03-02, 06:40, 21.749, 20.80, 12.01,
2016-03-02, 07:40, 20.72, 191.67, 11.99,
2016-03-02, 08:40, 20.12, 201.50, 11.98,
2016-03-02, 09:40, 20.153, 205.32, 11.92,
2016-03-02, 10:40, 21.361, 172.91, 11.97,
2016-03-02, 11:40, 20.402, 201.52, 11.98,

this is the offending expression: ($4>=190 ? 2 : $4>=50 ? 1 : 0)

I would think this should evaluate as: anything over 190; green, anything else over 50; orange, anything remaining red

I've also tried: (timecolumn(1)):4:($4<=50 ? 0 : $4<=190 ? 1 : 2) but that gives the same result

I'm sure I've made an elementary mistake, but would be very grateful for some help Thank you!

Upvotes: 2

Views: 1079

Answers (1)

Christoph
Christoph

Reputation: 48440

The autoscaled color range (cbrange) depends on your input data and the values given in the palette definition are relative to that range.

Use a fixed range:

set cbrange [0:2]

Since you have only three discrete color values you could also use linecolor variable, see e.g. Different coloured bars in gnuplot bar chart

Upvotes: 1

Related Questions