Also, I would like to change the color of the error bars in a similar
fashion: if a given variable is greater than, say, 50, the error bar
should be red, but black otherwise.
Thanks,
Mike
> I am relatively new to gnuplot and I'm trying to plot a bar graph with
> boxerrorbars such that the color of the box depends on the value of a
> certain variable. If the variable is greater than 20, for example,
> the bar will be blue, but if less than 20, the bar will be light
> blue. Is there any way to control the color of the bars in this
> fashion using boxerrorbars?
There is a variable color option, but unfortunately it is not part of the
boxerrorbars plot style. You would have to create this plot in two passes.
If you only need two colors it is probably easiest to do each color
in a separate pass:
plot 'data' using 1:($2>20 ? $2 : NaN):3 with boxerrorbars lc rgb "blue", \
'data' using 1:($2>20 ? NaN : $2):3 with boxerrorbars lc rgb "cyan"
If you need a larger palette of colors then it is better to use one pass
to fill in the colors from an arbitrary palette, and a second pass to
draw the errorbars:
First pass: draw only the boxes, with solid fill variable color.
Second pass: draw boxerrorbars with no fill,
letting the previous color show through
Here is an example:
blue = 0x0000ff # RGB components for pure blut
cyan = 0x00ffff # RGB components for cyan
red = 0xff0000 # RGB components for red
set boxwidth 1.0
plot 'data' using 1:2:($2>20 ? blue : $2>10 ? cyan : red) \
with boxes fillstyle solid lc rgb variable, \
'data' using 1:2:3 with boxerrorbars fillstyle empty
> Also, I would like to change the color of the error bars in a similar
> fashion: if a given variable is greater than, say, 50, the error bar
> should be red, but black otherwise.
For this you would have to combine both of the above approaches.
Left as an exercise for the reader :-)
Thanks very much, sfeam. I really liked your first suggestion,
regarding the box colors, but haven't tried to implement the second
one yet. I got a very strange result when I tried your first
suggestion. Even though all of the blue bars appeared correctly, some
of the cyan bars displayed as very slender lines (= zero-width boxes?)
instead of as normal-width boxes. So far I haven't been able to
discern a pattern, however. Any idea what might be causing this?
BTW, I forgot to mention in my first post that I'm using gnuplot 4.2.5
on Mac OS X 10.4.
I left out any preliminary commands that would control your box sizes.
I assume you know how you want that to work? You can either provide
an explicit width for each box, or specify a uniform width for all or them:
set boxwidth <whatever> absolute
Stay away from "relative" boxwidths if you are doing multiple passes,
because the neighboring boxes may belong to a different pass.
That makes sense, but doesn't seem to make a difference in this case.
I tried
set boxwidth 1 absolute
and still get "lines" instead of boxes.
I think we need to see the exact plot command you are using.
I'm sending everything, just in case the cause of the phenomenon is in
some other part of the command set.
Here is the plot file:
unset multiplot
reset
set terminal postscript landscape enhanced color font "ArialMT,16"
set style fill solid border -1
set boxwidth .8 absolute
set xtics border in scale 1,0.5 mirror rotate by 90 font "ArialMT,10"
set ytics font "ArialMT,16"
set key autotitle columnhead
set yrange [3.2:20000]
set logscale y
set grid xtics
set grid ytics
set format y "10^{%L}"
set offsets 1,1
set output "tmp.ps"
plot 'tmp.txt' u :($4<20 ? $2: NaN):3:xticlabels(1) w boxerrorbars t
"Eotaxin" lt 1 lw 1 lc rgb 'blue',\
'' u :($4<20 ? NaN : $2):3 w boxerrorbars notitle fs solid .5 lt 1 lw
1 lc rgb 'cyan'
set output
-----------EOF-------------
Here is the input file (tmp.txt). It's not as large as the actual
file, but still generates similar output:
"PID" "Eotaxin mean" "sd" "cv"
"0" 1.2 1.7 141.4
"3.2" 3.6 1.2 32.5
"16" 16.5 1.0 6.1
"80" 111.1 10.2 9.2
"400" 999.9 1056.8 105.7
"2000" 4537.7 13.9 0.3
"10000" 9656.4 97.1 1.0
"QC-low" 40.0 3.0 7.5
"QC-1" 202.2 215.9 106.8
"QC-2" 2445.8 278.0 11.4
"ID1" 179.0 12.7 7.1
"ID2" 63.8 18.0 28.2
--------------EOF----------------
-the first line contains the column labels
-subsequent lines contain data
-columns of data are delimited by tabs
-the first column of each data line contains a text identifier to be
used as a tic label on the x-axis
-the second column contains the y-value to be plotted
-the third column contains the standard deviation (used for the error
bar)
-the fourth column contains the %CV (if the %CV > 20, the box is
plotted in a lighter color
Weird. I don't know.
But I can give you a work-around while I think about it.
Add an additional column to each command, containing the boxwidth:
plot 'tmp.txt' u :($4<20 ? $2: NaN):3:(.8):xticlabels(1)...
'' u :($4<20 ? NaN : $2):3:(.8) ...
Explicitly specifying the column width worked. Thanks very much. I
really learned a lot.