Gnuplot change color of bars in histogram

Sven Hager picture Sven Hager · Sep 1, 2012 · Viewed 41.3k times · Source

is it possible to change the color of bars in a Gnuplot script dynamically? I have the following script

reset
fontsize = 12
set term postscript enhanced eps fontsize
set output "bargraph_speedup.eps"
set style fill solid 1.00 border 0
set style histogram
set style data histogram
set xtics rotate by -45
set grid ytics linestyle 1
set xlabel "Benchmarks" font "bold"
set ylabel "Relative execution time vs. reference implementation" font "bold"
set datafile separator ","
plot 'bm_speedup.dat' using 2:xtic(1) ti "Speedup" linecolor rgb "#00FF00"

which generates this plot:

Generated plot

Is it possible to make the color of the bars which are below zero red?

Thanks,
Sven

Answer

mgilson picture mgilson · Sep 3, 2012

You can mimic this behavior using the boxes style:

My test data:

zip 2
baz 2
bar -1
cat 4
foo -3

And then plotting with gnuplot:

set style line 1 lt 1 lc rgb "green"
set style line 2 lt 1 lc rgb "red"
set style fill solid
plot 'test.dat'  u (column(0)):2:(0.5):($2>0?1:2):xtic(1) w boxes lc variable
#                  #xval:ydata:boxwidth:color_index:xtic_labels