2

I'm using Gnuplot to generate a histogram but I need to color some of them in another color if the value are over/under a specific value. Eg if value < 10, color the specific histogram green. If value > 10, value < 20, color the specific histogram yellow. If value > 20, color histogram red.

So I want the graph to be like this:

x . y . color

1 . 4 . green

2 . 15 . yellow

3 . 40 . red

The values (x and y) comes from a database so I won't be able to tell Gnuplot which x-values I want to colorize as the values will change from time to time.

Am I able to accomplish this with Gnuplot (And php)?

Thanks!

Kevin
  • 321
  • 4
  • 21

2 Answers2

6

You can use the following gnuplot script:

set style fill transparent solid 0.5 noborder
set boxwidth 0.95 relative
set palette model RGB defined (0 "green", 1 "yellow", 2 "red")
plot 'path\to\your\file' using 1:2:($2<=10 ? 0 : $2<=20 ? 1 : 2) with boxes palette

The contents of my test file are

1 4
2 15
3 40

and the outcome I get is

enter image description here

halex
  • 16,253
  • 5
  • 58
  • 67
  • @mgilson Yes I tried it and you see my result at http://imgur.com/6ZsnvVt. I used the OP's data. My system is Win7, Gnuplot 4.6 – halex Apr 04 '13 at 12:07
  • right you are! I should have tried this on my other machine. On my laptop I'm still using gnuplot4.4 as the default install (even though I have 4.6 to play around with as well). In this case, I'll even upvote – mgilson Apr 04 '13 at 12:08
  • As a side note, this has inspired me to submit a [feature request](https://sourceforge.net/p/gnuplot/feature-requests/361/) – mgilson Apr 04 '13 at 12:29
  • @mgilson Thank you for the feature request info. I will definitely follow the progress. Is it possible to vote on this? – halex Apr 04 '13 at 12:34
  • I don't know. I think you could add a comment if you have a sourceforge account, but I don't think they have a meta.stackoverflow like upvoting system :) – mgilson Apr 04 '13 at 12:37
  • 1
    Thanks for the help! Made a similar approach: plot 'file.dat' using 1:($3>=20 ? $3 : NaN) with boxes lc rgb "red", 'file.dat' using 1:($3<20 ? $3 : NaN) with boxes lc rgb "yellow", 'file.dat' using 1:($3<10 ? $3 : NaN) with boxes lc rgb "green" – Kevin Apr 04 '13 at 14:49
  • Here's possibly a better explanation from Ethan Merrit himself: https://sourceforge.net/p/gnuplot/feature-requests/361/#b856 – mgilson Apr 04 '13 at 16:18
2

Given this datafile:

1 4  green
2 15 yellow
3 40 red

The following line works:

plot for [color in "green red yellow"] 'test.dat' using 1:(strcol(3) eq color ? $2:NaN):(0.95) with boxes lc rgb color
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • I have a small improvement for your code: In its current form the boxes are not colored with the right colors you have in the third column, but the one that should be green is red, what should be yellow is blue and what should be red is green :). You can change the colors appropriate by adding `lc rgb color` at the end of your code line. – halex Apr 04 '13 at 12:58
  • @halex -- Yes, that's what I meant to do all along... It seems I wasn't paying attention in my testing. Thanks. – mgilson Apr 04 '13 at 13:04
  • Finally I also give you a +1 for the different approach and because it's not clear whether OP has the color as a third column. – halex Apr 04 '13 at 13:17