change box colors in bar graph

change box colors in bar graph

Post by mikegnubi » Fri, 12 Feb 2010 03:04:21


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?

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
 
 
 

change box colors in bar graph

Post by sfea » Fri, 12 Feb 2010 04:48:03


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


For this you would have to combine both of the above approaches.
Left as an exercise for the reader :-)

 
 
 

change box colors in bar graph

Post by mikegnubi » Fri, 12 Feb 2010 06:57:38


> If you need a larger palette of colors then it is better to use one pas>
> to fill in the colors from an arbitrary palette, and a second pass t>
> draw the errorbars>
> First pass: raw only the boxes, with solid fill variable color>
> Second pass: draw boxerrorbars with no fill>
> etting the previous color show >hr>ugh
>
> Here is an e>ample:
> blue = 0x0000ff # RGB components fo> pure blut
> cyan = 0x00ffff # RGB comp>nents for cyan
> red = 0xff0000 RG> c>mponents for red
>> >>set boxwidth 1.0
>
> plo> 'data' using >:2:($2>20 ? blue : $>>10 ? cyan : red) \
> with boxes fillstyl> solid lc rgb variable, \
> data' using 1:2:3 with>bo>e>rorbars fillstyle empty
>
> > Also, I would like to change the color o> >he error bars in a similar
> > fashion: if a given variable is greater>t>an, say, 50, the error bar
> > should>be>red, but black otherwise.
>
> For this you would have to combine >oth 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.
 
 
 

change box colors in bar graph

Post by sfea » Fri, 12 Feb 2010 08:05:16


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.
 
 
 

change box colors in bar graph

Post by mikegnubi » Fri, 12 Feb 2010 09:02:05

n Feb 10, 3:05m, sfeam < XXXX@XXXXX.COM > wrote:
> >> red = 0xff0000 RGB compone>ts>f>> red
>
> >> set b>xw>d>> 1.0
>
> >> plot 'data' >sing 1:2:($2>2> ? blue : $2>10 ? cy>n>> red) \
> >> with boxes fillstyle solid lc rgb >a>>able, \
> >> 'data' using 1:2:3 with boxerrorbars fill>ty>e>>m>ty
>
> >> > Also, I would like to change the color of the error bars i> >>s>milar
> >> > fashion: if a given variable is greater than, say, 50, th> >>r>r bar
> >> > should be red, but black>ot>e>>ise.
>
> >> For this you would have to combine both of the above >p>>oaches.
> >> Left as an exercise for the>re>d>r :-)
>
> > Thanks very much, sfeam. really liked your first >u>gestion,
> > regarding the box colors, but haven't tried to implement>t>e second
> > one yet. got a very strange result when I tried>y>ur first
> > suggestion. ven though all of the blue bars appeared corr>c>ly, some
> > of the cyan bars displayed as very slender lines (= zero-wi>t> boxes?)
> > instead of as normal-width boxes. o far I haven't b>e> able to
> > discern a pattern, however. ny idea what might be ca>s>ng this?
> > BTW, I forgot to mention in my first post that I'm using gn>p>ot 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? ou can eit>er provide
> an explicit width for each box, or specify a uniform width for a>l >r them:
>
> s<t box>idth >
> Stay away from "relative" boxwidths if you are doing mul>iple 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.
 
 
 

change box colors in bar graph

Post by sfea » Fri, 12 Feb 2010 10:30:37


I think we need to see the exact plot command you are using.
 
 
 

change box colors in bar graph

Post by mikegnubi » Fri, 12 Feb 2010 13:47:43


> > et 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 %C> > 20, the box is
plotted in a lighter color
 
 
 

change box colors in bar graph

Post by sfea » Fri, 12 Feb 2010 15:50:54


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) ...
 
 
 

change box colors in bar graph

Post by mikegnubi » Fri, 12 Feb 2010 16:12:43


> > -----------EOF--------->--> >>
> > Here is the input file (tmp.txt). t's not as large as the ac>u>l
> > file, but still generates similar out>ut> >>
> > "PID" Eotaxin mean" >">d" cv"
> > "0" .2>gt;1.7 141.4
> > "3.2">gt;.6 1.2 32.5
> > >16" 16.5 .0 6.> >> > "80" 111.1 10.2 gt;.>
> > "400" 99.9 1056>8>05.7
> > "2000" 4537.7>gt;3.9 .3
> > "10000" 6>6>4 7.1 .0
> > "QC-low" 4>.> .0 7.5
> > "QC-1" gt;2>2.2 215.9 106.8
> > ">C>2" 2445.8 78.0 11.4 >>>> "ID1" 79.0 12.7 .1
> > >ID>">3.8 8.0 8.2
> > --------------EO>->--------------
>
> > -the first>l>ne contains the column labels
> > -subse>u>nt lines contain data
> > -columns of data are delimited by tabs
> > >t>e first column of each data line con>a>ns a text identifier to be
> > used as a tic label on t>e>x-axis
> > -the second column contains the y-value to be plotted
> > ->h> third >o>umn contains the standard deviation (used for the>error
> > bar)
> > plotted in a lighter color

Explicitly specifying the column width worked. Thanks very much. I
really learned a lot.